Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default How to make ad factorial table? With and without BigInteger.

    I need to make a chart, in two collumns, to calculate the factorial of every number, 0 to 30, without BigIntegers. The teacher said that after about 13, it will print it out wrong. Thanks guys.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to make ad factorial table? With and without BigInteger.

    Do you know how large 30! is? It's 265252859812191058636308480000000 and it's much larger than Integer.MAX_VALUE; it is even larger than the largest long number.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: How to make ad factorial table? With and without BigInteger.

    That is true, but do you have any idea on how to actually code the table?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to make ad factorial table? With and without BigInteger.

    Quote Originally Posted by zhider View Post
    That is true, but do you have any idea on how to actually code the table?
    Yep, but what is that rediculous requirement about (about not using BigIntegers)?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: How to make ad factorial table? With and without BigInteger.

    I am supposed to make one without BigIntegers, give a reason why it doesnt work, then make one with BigInts and explain why it does.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to make ad factorial table? With and without BigInteger.

    Well, you know why using ints (or longs) don't work (30! is larger than that) so build a version that uses BigInts. Tip: n! == n*((n-1)!)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: How to make ad factorial table? With and without BigInteger.

    The code that you put is giving alot of errors in Netbeans, would something like n= x*(x-1) work? any other ideas?

  8. #8
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: How to make ad factorial table? With and without BigInteger.

    This is what I have roght now, and I think it is far off. Right now it is printing one collumn of 0-30, like it should, but in the other collumn it is just printing 0s all the way down. Any ideas at all on how to fix?
    Java Code:
    public static void main(String[] args) {
    factorial();
        }
        public static void factorial (){
    int x=0, n=5;
    while (x<30){
        x=x+1;
    while (n>=1){
     n= x*(x-1);
    
    }
    System.out.println (x+ "   "+ n);
    }

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to make ad factorial table? With and without BigInteger.

    Quote Originally Posted by zhider View Post
    The code that you put is giving alot of errors in Netbeans
    Ah yes! The old "jam the code snippet someone gave me into a program without understanding it" trick.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to make ad factorial table? With and without BigInteger.

    Think about what the output would look like. Do you really need 2 loops? Here's how I imagine it:
    Java Code:
    loop {
        // all printing done on one line
        print n
        print space
        calculate factorial of n
        print factorial
    }

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to make ad factorial table? With and without BigInteger.

    Quote Originally Posted by Junky View Post
    Do you really need 2 loops?
    OK due to the lack of indenting I didn't notice they were nested loops which would be correct unless you calculated the factorial in another method.

  12. #12
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: How to make ad factorial table? With and without BigInteger.

    Srry about that. Ill read that page tonight (on code conventions), as soon as I get this done. Any ideas on what needs to be changed in the formula, to find the factorial of x, after it adds one to x? thanks.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to make ad factorial table? With and without BigInteger.

    Do you understand what factorial is? Try writing out the calculation for facortorial 4 on paper. Then factorial 5, factorial 6. Perhaps you can see a pattern. Then try converting it to code. Do not just grab a code snippet from someone without knowing what it does and expect it to work. Especially since you did not copy it correctly.

  14. #14
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: How to make ad factorial table? With and without BigInteger.

    I do realize what a factorial is and how to solve one on paper, I have been trying on this code for 3 days and cannot figure it out.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to make ad factorial table? With and without BigInteger.

    If you hadn't noticed the pattern here it is:

    4! = 1*2*3*4
    5! = 1*2*3*4*5
    6! = 1*2*3*4*5*6

    or
    4! = 1*2*3*4
    5! = 4! * 5
    6! = 5! * 6

    This shows that the best way to solve factorial is to use recursion and if you look at the snippet Jos gave you (and the bit you missed) there was another ! at the end of the equation. If you cannot use recursion then you need to calculate it the old fashion way using a loop. BTW why have you initialised n to 5?

  16. #16
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: How to make ad factorial table? With and without BigInteger.

    I didnt miss the !, I got an error when i put it. ')' expected. and ill look into recursion, thanks.

  17. #17
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: How to make ad factorial table? With and without BigInteger.

    And i was trying to test it with some numbers, and dont know how to set them in the actual methods

  18. #18
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: How to make ad factorial table? With and without BigInteger.

    When I use that code n! == n*((n-1)!); tells me the following ';' expected, Not a statement, ';' expected, Not a statement. I think it says ';' expected, because its looking for an operation between n and ! and (n-1) and !. How do I fix this?

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to make ad factorial table? With and without BigInteger.

    Quote Originally Posted by zhider View Post
    I didnt miss the !, I got an error when i put it. ')' expected. and ill look into recursion, thanks.
    Of course you got an error. The ! means factorial in Math but it is the not operator (as in a != b). Once again this goes back to just grabbing some code snippet without understanding what you are doing.
    Java Code:
    declare a total variable
    initialise it to 1
    declare a factor variable
    initialise it to 2
    while factor is less than target (whatever factorial you are calculating
        total multiplied by factor
        increment factor
    }

  20. #20
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: How to make ad factorial table? With and without BigInteger.

    Thank a ton for all your help so far, I know I can be... difficult to work with. What does increment factor mean? Is that where you use that code? how do you plug total and factor into that? thanks.

Page 1 of 2 12 LastLast

Similar Threads

  1. Factorial method bug
    By Mapisto in forum New To Java
    Replies: 20
    Last Post: 12-18-2011, 12:36 AM
  2. Factorial beginner
    By jk91 in forum New To Java
    Replies: 4
    Last Post: 11-14-2011, 10:52 PM
  3. Factorial in java
    By spidey32 in forum New To Java
    Replies: 18
    Last Post: 04-09-2011, 01:04 PM
  4. factorial sum in java
    By java157 in forum New To Java
    Replies: 9
    Last Post: 03-17-2011, 10:07 AM
  5. Factorial
    By Anindo in forum New To Java
    Replies: 4
    Last Post: 07-28-2009, 09:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •