Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Factorial and Factorion, that is the question!

1. ## 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:
```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:
```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  Reply With Quote

3. ## 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.  Reply With Quote

4. ## 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.
```   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.
```   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?  Reply With Quote