# Factorial and Factorion, that is the question!

Printable View

• February 24th, 2011, 10:34 PM
Java Neil
Factorial and Factorion, that is the question!
Well I thought I'd share this one with the group. It's the coolest thing I've had to try and solve. But anyway, I have the chance to earn to extra credit with this assignment.

assignment #1.

List all factorials from 1 to 100- something interesting happens?

assignment #2

A self-generating integer is such that the sum of the factorials of the digits of a number equal the number. For example:
1=1!=1*1=1
2=2!=2*1=2
145= 1!+4!+5!=1+24+120=145
There is one more. Create a program to find it.

For the first assignments, this is what I have so far.

Factorial:
Code java:

```public class Factorial { // Evaluate n! public static long factorial( int n ) { if( n <= 1 ) // base case return 1; else return n * factorial( n - 1 ); }   // Test program public static void main( String [ ] args ) { for( int i = 1; i <= 100; i++ ) System.out.println( factorial( i ) ); } }```

Factorion:
Code java:

```import java.util.Scanner; public class Factorion { public static void main(String[] args) { int inputNumber; int number; int digit; int total; int factorial;   Scanner keyboard = new Scanner(System.in);   System.out.print("Please enter a positive integer (>=0):"); inputNumber = keyboard.nextInt();   while (inputNumber <= 0 || inputNumber == -0) { System.out.println("Sorry, the integer must be greater than or equal to 1."); System.out.print("Please enter a positive integer (>=0):"); inputNumber = keyboard.nextInt(); } total = 0; number = inputNumber; do { digit = number % 10;   factorial = 1; for (int i = 1; i <= digit; i++) { factorial *= i; }   total += factorial;   number /= 10;   } while (number > 0);   if (total == inputNumber) { System.out.println(inputNumber + " is a factorion"); } else { System.out.println(inputNumber + " is a not factorion"); } } }```

While this program works and does tell you if your number you entered is a factorion or not, I don't think it meets the prompt. I think what I should do here, is find a way to only print the 4 factorion numbers (boolean?) - 1, 2, 145, and 40585. The big part here is getting graded on the logic. I would think that if it's only printing out 4 numbers, it should at least be done with JOptionPane too.

Any thoughts on either of these?

Neil
• February 25th, 2011, 05:15 PM
dlorde
Re: Factorial and Factorion, that is the question!
The requirement says "There is one more. Create a program to find it." If your program does that, it satisfies the requirement.
• February 25th, 2011, 06:54 PM
Java Neil
Re: Factorial and Factorion, that is the question!
No...I was saying that if you enter in a factorion it would tell you if you're right. It was using user input.

I did figure that one out. It need to run through every number incrementally till it found it on it's own.

Feel free to help me slim it down if you see something. My teacher is always commenting on what he calls garbage code.
Code java:

``` import javax.swing.JOptionPane; public class Factorion { public static void main(String[] args) { int intNumber; int number; int digit; int total; int factorial;   intNumber = 0;   while (intNumber <= 40585){   total = 0;   number = intNumber;   do { digit = number % 10;   factorial = 1; for (int i = 1; i <= digit; i++) { factorial *= i; }   total += factorial;   number /= 10;   } while (number > 0);   if (total == intNumber) { JOptionPane.showMessageDialog(null, intNumber + " is a Factorion"); intNumber++; } else { System.out.println(intNumber + " is a not Factorion"); intNumber++; } } } }```

This is also the best solution I could come up with for the Factorial program.
Code java:

``` import java.math.BigInteger; public class Factorial { public static void main(String[] args) {   // BigInteger solution. BigInteger n = BigInteger.ONE; for (int i = 1; i <= 100; i++) { n = n.multiply(BigInteger.valueOf(i)); System.out.println(i + "! = " + n); } } }```

Any thoughts?