Thread: store vaue which is more than 25 digits

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    1

    store vaue which is more than 25 digits

    hello i am just starting in cpp
    i am trying to find factorial of 99
    i used unsigned long to store it but it shows the result as zero can any one tell me what sort of data type i should use to get the proper result
    thanz in advance

  2. #2
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Try using floats or doubles. They behave quite differently from ints since they are floating point datatypes but i think it's the easiest way to accomplish what you are trying to

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    99! has 156 digits:

    Code:
    933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000
    And I don't know of any datatype able to store THAT many.

    It comes out as zero because unsigned long on a 32-bit machine is 4294967296 and 99! % 4294967296 == 0

    Code:
    99! == 4294967296 (ie 2 ^ 32) *
    217292028115932253845267088283474325413620025535071318663174668083008378728362535147325750384844183656977717580337132695715840000000000000000000000

    Even on a 64-bit machine:
    Code:
    99! == 18446744073709551616 (ie 2 ^ 64) *
    50592242767087242995683822846849059083876205965660353811265590619996278250674376065685821321058129811887097912919040000000000000000000000

    Option #1: Use double, but forego accuracy.
    Option #2: Use a bignum library like GMP or Apfloat.
    Option #3: Accept the fact that n! for n > 33 will always return 0
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not returning correct results
    By drty2 in forum C Programming
    Replies: 4
    Last Post: 01-19-2009, 12:39 PM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  4. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  5. magic square
    By dizzyhippy in forum C Programming
    Replies: 1
    Last Post: 03-11-2002, 12:36 PM