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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Factorial and Factorion, that is the question!

  1. #1
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by Java Neil; February 24th, 2011 at 11:37 PM.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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.

  3. #3
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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?

Similar Threads

  1. [SOLVED] Is it possible to get factorial of negative number
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2011, 05:45 PM
  2. [SOLVED] Factorial of 1-20
    By pitchblack in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 16th, 2011, 01:33 PM