Thread: Stuck with huge number

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    28

    Stuck with huge number

    Hi,
    I'm trying to read in a number containing 1billion digits.
    The number is stored in a simple txt file (~950mb) and since it's the only value, which needs to be read in I pipe it using ./bla <number.txt.
    However, the prog quits while it's still reading it in.
    The code of the program itself is fine.
    Any suggestions on what to do?

  2. #2
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49

    Lightbulb

    Maybe there is no type that have limit up to 1 billion digits?

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    38
    what are you needing to do with this number... cause if u just need to read it into your program you could split it up into a vector of strings, each element holding the max characters a string can hold?

    Hope this helps

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    28
    I'm using GMP and the variable is of type mpf_class.
    Even if it was too big, shouldn't it assign 0 to it and then exit the prog(since the number has to be bigger than 1),
    as it does if you cin 22222222222222 for example for an int?

    I'd like to calculate its primefactors.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Post your "reading it in" code. cin's probably freaking out. 1 billion digits. Sheesh...
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    28
    mpf_class n;
    cin >> n;
    that's all.

  7. #7
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Perhaps, read up to a certain point. Store that in a temp container, read some more, then append , repeat until the end. It might work...
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    probably there is some buffer overflow in the mpf_class's operator>>
    try to wrap the >> in the try/catch block and see it...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    well, mpf_class needs a parameter for the precision you want, have you done that?

    You'd need to set it to over 3.3 billion bits of precision to represent it exactly.

    Of course, you could just always read in the first 20 digits, discard the rest, and declare it accurate enough
    Last edited by Cat; 01-22-2007 at 12:00 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    28
    I've tried setting the precision to 3.3billion as suggested, but then it just segfaults, so I tried it with 3.3million.
    I've also wrapped >> in a try/catch, but it still just quits, doesn't print an error.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    > simple txt file (~950mb)

    You do have got well over 1GB of RAM on you computer, right?

  12. #12
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    If you can factor a number that large, these guys will probably pay you quite handsomely.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    28
    Quote Originally Posted by joni
    > simple txt file (~950mb)

    You do have got well over 1GB of RAM on you computer, right?
    2GB DDR2 and 2GB swap.
    If that's not enough for the calculations itself, that's fine, but it should be enough to read it in.

  14. #14
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Out of curiosity, how did you create a text file with so many numbers? Did you use an infinite loop then write that? Or did you copy and paste in notepad or something?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    28
    I generated the first digit randomly from 1-9 and for the rest a loop generating random numbers from 0-9,
    cout'ed every single one and tee'd it into number.txt. Not very efficient but simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM