Plain and Recursive Factorials with Full Print Enhancements

GEB Recursive
Creative Commons License photo credit: gadl

This programming challenge is from a WiBit.net instructional video, which I studied/practiced this spring 2009. The end result itself is not terribly sexy, but I liked the algorithm construction process. My programming curriculum changed course during the first year, and they eliminated our Java classes. I was still curious about Java, so I took it upon myself to self-learn Java. This was one of my first products.

I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.

Challenge: Create a simple factorial calculator in Java, using the NetBeans IDE. The original project simply output something like “5! = 120″. I improved it to output each member integer so as to clarify the factorial process, so my program output something like “5! = 1 x 2 x 3 x 4 x 5 = 120″. I like to push projects a little farther than the original, just to satisfy myself or to learn more.

My solution:

package factorial;
/**
 * @author kirkster
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int n;
        int i;
        int factorial = 7;
        int result= 1;
        System.out.print(factorial + "! = ");
        for (i = 1; i <= factorial; i++) {
            System.out.print(i);
            if (i < factorial) {
                System.out.print(" x ");
            }
        }
        for (i = 1; i <= factorial; i++) {
            result = fact(i);
        }
        System.out.print(" = " + result);
        System.out.println("");
    }

    static int fact(int n) {
        int i;
        int result = 1;
        for (i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }
}

After this first factorial program, I did a WiBit video doing the same thing using a recursive algorithm. It accomplishes the same thing but in a computer-scientist-sexier way:

package recursivefactorial;
/**
 * @author kirkster
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println(factorial(n));
    }

    public static long factorial(int n) {
        if (n < 0)
            return -1;
        else if (n == 0)
            return 1;
        else
            return n * factorial(n -1);
    }
}

After doing both programs, I wanted to enhance the recursive version with the full step printouts like I did in the first plain version. It was a bit trickier to implement printing out each member integer like my previous improvement, and I could only print the member integers in backwards format (avoiding an extra loop) but this was my solution:

package recursivefactorial;
/**
 * @author kirkster
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int n = 7;
        System.out.print(n + "! = ");
        System.out.println(factorial(n));
    }

    public static long factorial(int n) {
        if (n < 0)
            return -1;
        else if (n == 0) {
            System.out.print(" = ");
            return 1;
        }
        else {
            if (n == 1)
                System.out.print(n);
            else
                System.out.print(n + " x ");
            return n * factorial(n -1);
        }
    }
}

Here is the output:

Output from a recursive factorial algorithm built in Java

Output from a recursive factorial algorithm built in Java

  • Share/Bookmark




Leave a Reply