Results 1 to 5 of 5

Thread: Factorial

  1. #1
    Anindo is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Question Factorial

    Hello everyone
    I have just write a code for finding nth factorial number it works well upto 15
    but when i input 25.it works abnormally such as 17,20,24 factorial value shows
    "-". i did not solve the bug . can anyone help me to solve this
    my code is this
    public int factorial(int n)
    {
    if(n<=1)
    {
    return 1;
    }
    else
    {
    return n*factorial(n-1);
    }
    }
    in tester class the code is this
    int x=s.nextInt();
    for(int j=0;j<=x;j++)
    {
    int fact=r.factorial(j);
    System.out.println(j+"!"+"="+" "+fact);
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Its because your numbers are getting bigger than the maximum value allowed by int. Try using long instead:
    Java Code:
    public class Factorial {
    
        public static void main(String[] args) {
            new Factorial();
        }
    
        public Factorial() {
            test();
        }
    
        public long factorial(long n) {
            if (n <= 1) {
                return 1;
            } else {
                return n * factorial(n - 1);
            }
        }
    
        public void test() {
            for (long j = 0; j <= 20; j++) {
                long fact = factorial(j);
                System.out.println(j + "!" + "=" + " " + fact);
            }
        }
    }
    produces:
    0!= 1
    1!= 1
    2!= 2
    3!= 6
    4!= 24
    5!= 120
    6!= 720
    7!= 5040
    8!= 40320
    9!= 362880
    10!= 3628800
    11!= 39916800
    12!= 479001600
    13!= 6227020800
    14!= 87178291200
    15!= 1307674368000
    16!= 20922789888000
    17!= 355687428096000
    18!= 6402373705728000
    19!= 121645100408832000
    20!= 2432902008176640000

  3. #3
    Anindo is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Question factorial

    Thanks quad64bit for the solution but i think still there is a bug .the code do well up to 20 but when i have given input 25 then for 21,24,22 it shows "-" value.and for input 50, as far i think , value for 26-30 and 36,50 it shows "-"
    how it will be fixed .
    Thanks

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    That is not a bug, its the same problem as before. The resulting number is bigger than the datatype can hold. Perhaps BigInteger will help, try this:
    Java Code:
    import java.math.BigInteger;
    
    /**
     * @author Steffen Gates
     */
    public class Factorial {
    
        public static void main(String[] args) {
            new Factorial();
        }
    
        public Factorial() {
            test();
        }
    
        public BigInteger factorial(BigInteger n) {
            if (n.compareTo(new BigInteger("1"))<=0) {
                return new BigInteger("1");
            } else {
                return n.multiply(factorial(n.subtract(new BigInteger("1"))));
            }
        }
    
        public void test() {
            for (long j = 0; j <= 200; j++) {
                BigInteger fact = factorial(new BigInteger(""+j));
                System.out.println(j + "!" + "=" + " " + fact);
            }
        }
    }
    Produces:
    0!= 1
    1!= 1
    2!= 2
    3!= 6
    4!= 24
    5!= 120
    6!= 720
    7!= 5040
    8!= 40320
    9!= 362880
    10!= 3628800
    11!= 39916800
    12!= 479001600
    13!= 6227020800
    14!= 87178291200
    15!= 1307674368000
    16!= 20922789888000
    17!= 355687428096000
    18!= 6402373705728000
    19!= 121645100408832000
    20!= 2432902008176640000
    21!= 51090942171709440000
    22!= 1124000727777607680000
    23!= 25852016738884976640000
    24!= 620448401733239439360000
    25!= 15511210043330985984000000
    26!= 403291461126605635584000000
    27!= 10888869450418352160768000000
    28!= 304888344611713860501504000000
    29!= 8841761993739701954543616000000
    30!= 265252859812191058636308480000000
    31!= 8222838654177922817725562880000000
    32!= 263130836933693530167218012160000000
    33!= 8683317618811886495518194401280000000
    34!= 295232799039604140847618609643520000000
    35!= 10333147966386144929666651337523200000000
    36!= 371993326789901217467999448150835200000000
    37!= 13763753091226345046315979581580902400000000
    38!= 523022617466601111760007224100074291200000000
    39!= 20397882081197443358640281739902897356800000000
    40!= 815915283247897734345611269596115894272000000000
    41!= 33452526613163807108170062053440751665152000000000
    42!= 14050061177528798985431426062445115699363840000000 00
    43!= 60415263063373835637355132068513997507264512000000 000
    44!= 26582715747884487680436258110146158903196385280000 00000
    45!= 11962222086548019456196316149565771506438373376000 0000000
    46!= 55026221598120889498503054288002548929616517529600 00000000
    47!= 25862324151116818064296435515361197996919763238912 0000000000
    48!= 12413915592536072670862289047373375038521486354677 760000000000
    49!= 60828186403426756087225216332129537688755283137921 0240000000000
    50!= 30414093201713378043612608166064768844377641568960 512000000000000
    51!= 15511187532873822802242430164693032110632597200169 86112000000000000
    52!= 80658175170943878571660636856403766975289505440883 277824000000000000
    53!= 42748832840600255642980137533893996496903437883668 13724672000000000000
    54!= 23084369733924138047209274268302758108327856457180 7941132288000000000000
    55!= 12696403353658275925965100847566516959580321051449 436762275840000000000000
    56!= 71099858780486345185404564746372494973649797888116 8458687447040000000000000
    57!= 40526919504877216755680601905432322134980384796226 602145184481280000000000000
    58!= 23505613312828785718294749105150746838288623181811 42924420699914240000000000000
    59!= 13868311854568983573793901972038940634590287677268 7432540821294940160000000000000
    60!= 83209871127413901442763411832233643807541726063612 45952449277696409600000000000000
    61!= 50758021387722479880085681217662522722600452898803 6003099405939480985600000000000000
    62!= 31469973260387937525653122354950764088012280797258 232192163168247821107200000000000000
    63!= 19826083154044400641161467083618981375447736902272 68628106279599612729753600000000000000
    64!= 12688693218588416410343338933516148080286551617454 5192198801894375214704230400000000000000
    65!= 82476505920824706667231703067854962521862585513454 37492922123134388955774976000000000000000
    66!= 54434493907744306400372924024784275264429306438879 8874532860126869671081148416000000000000000
    67!= 36471110918188685288249859096605464427167635314049 524593701628500267962436943872000000000000000
    68!= 24800355424368305996009904185691715810473992013553 67672371710738018221445712183296000000000000000
    69!= 17112245242814131137246833888127283909227054489352 0369393648040923257279754140647424000000000000000
    70!= 11978571669969891796072783721689098736458938142546 42585755536286462800958278984531968000000000000000 0
    71!= 85047858856786231752116764423992601028858460812079 62358864307633885886803780790176972800000000000000 00
    72!= 61234458376886086861524070385274672740778091784697 32898382301496397838498722168927420416000000000000 0000
    73!= 44701154615126843408912571381250511100768007002829 05015819080092370422104067183317016903680000000000 000000
    74!= 33078854415193864122595302822125378214568325182093 49711706119268354112357009715654592508723200000000 00000000
    75!= 24809140811395398091946477116594033660926243886570 12283779589451265584267757286740944381542400000000 0000000000
    76!= 18854947016660502549879322608611465582303945353793 29335672487982961844043495537923117729972224000000 000000000000
    77!= 14518309202828586963407078408630828498374037922420 83588467815746880619913491564200800652078612480000 00000000000000
    78!= 11324281178206297831457521158732046228731749579488 25199004896282566883532523420076624508621317734400 0000000000000000
    79!= 89461821307829752868514417153983165206980821677957 19072138680632278379906935018605333618108410101760 00000000000000000
    80!= 71569457046263802294811533723186532165584657342365 75257710944505822703925548014884266894486728081408 0000000000000000000
    81!= 57971260207473679858797342315781091054123572447316 25958745865049716390179693892056256184534249745940 480000000000000000000
    82!= 47536433370128417484213820698940494664381329406799 33286171609340767439947348991486130071318084791671 19360000000000000000000
    83!= 39455239697206586511897471180120610571436503407643 44627522435752836975156299662933487959194010377087 0906880000000000000000000
    84!= 33142401345653532669993875791301312880006662862420 49487118846032383059131291716864129885722968716753 156177920000000000000000000
    85!= 28171041143805502769494794422606115948005663433057 42064051019127525600261597959334510402864523409240 18275123200000000000000000000
    86!= 24227095383672732381765523203441259715284870552429 38175083876449672016224974245027678946463490131946 5571660595200000000000000000000
    ...
    Last edited by quad64bit; 07-28-2009 at 10:39 PM.

  5. #5
    Anindo is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

Similar Threads

  1. [SOLVED] overflow when calculate factorial
    By ravinda in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 06:07 PM

Posting Permissions

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