# Factorial loop help

• March 20th, 2013, 06:40 PM
iamgonge
Factorial loop help
Ok so I need to find the smallest integer whose factorial is greater than 2000

current code:
public class FactorialInt {
public static void main (String args[]){

int value = 7, factorial = 1, temp= value;

while(temp > 0){
factorial *=temp;
temp--;
}
System.out.println("The factorial of " + value + " is " + factorial + ".");
}}

not sure how to change this code to solve problem. I can find the factorial for a int that i set but i how do i have it find the int that meets the requirements?
• March 20th, 2013, 07:23 PM
Norm
Re: Factorial loop help
Quote:

how do i have it find the int that meets the requirements?
Brute force? Start with 3 and go from there until you've found the int that meets the requirements.
• March 20th, 2013, 07:27 PM
iamgonge
Re: Factorial loop help
i did that and its 7, bit the program is to i guess run a loop that finds that number
• March 20th, 2013, 07:37 PM
Norm
Re: Factorial loop help
The loop needs to keep changing the number until the desired results are found.
• March 20th, 2013, 07:44 PM
iamgonge
Re: Factorial loop help
not sure how to make that happen since i have a loop going already, do i add another loop or does my current loop need to be changed?
• March 20th, 2013, 07:53 PM
Norm
Re: Factorial loop help
Have a loop that starts at 1 and changes the factor until the desired result.
Save the last factorial and use for next computation.
• March 20th, 2013, 08:02 PM
iamgonge
Re: Factorial loop help
public class FactorialInt {

public static void main (String args[]){
int input_number = 0;
int factorial = 1;

while (factorial < 2000){
input_number = input_number + 1;
factorial = factorial * input_number;
}
System.out.println( "The Factorial of " + input_number+ " is " + factorial );
}
}

this seems to work please check!
• March 20th, 2013, 08:07 PM
Norm
Re: Factorial loop help
Quote:

this seems to work
If you get the correct results, then that solves it.