#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    41
    Rep Power
    2

    Factorial help please!


    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?

    any help would be appreciated
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    It would be easier if you would start with 1 and move up to calculate the factorial. That way you would know when you exceed the limit.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    41
    Rep Power
    2
    yes i tried that but how do i get it to loop since i already have a loop going? I dont get how to loop until it hits the requirement
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Code:
    input_number := 0
    factorial := 1
    while factorial <= 2000 :
    	input_number = input_number + 1
    	factorial = factorial * input_number
    print "The smallest natural number n with n! > 2000 is " + input_number
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo