reading large number

This is a discussion on reading large number within the C++ Programming forums, part of the General Programming Boards category; I'm doing a program that has to read in a rather large number. Actually, there are 2 numbers that I ...

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452

    reading large number

    I'm doing a program that has to read in a rather large number. Actually, there are 2 numbers that I want to read. One has 12 digits, and the other has 1000 digits. I'm stumped and don't know quite how to proceed. I thought of reading and storing each digit as a member of a vector. S0 1000 digits results in a vector of 1000 elements. Does anyone know a better way to store such large number? Any help is appreciated.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    Registered User
    Join Date
    Jan 2011
    Location
    Calcutta, India, India
    Posts
    18
    Thats where BigNum libraries come in
    Google for bignum libraries for c++.

    Miracl is a commonly used one

    Shamus Software Limited - Home --> Miracl .

    Best Regards
    KKR

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Reading into a vector is about all you can do. That's how all the arbitrary precision libraries handle large numbers internally, more or less (well, probably arrays, but it's the same idea). If you're concerned about space you can use a string and/or convert to base 256.

    And as a fan of GNU I'll link to an alternative to the library listed above.
    Consider this post signed

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Thanks guys for the reply. Here's what I've worked on, and it seems to work. I will certainly check out those lib. I wonder if the bignum lib can be found in repo for ubuntu; this would be sweet of it does.
    Code:
    int main()
    {
      using namespace std; 
      ifstream ifs;
      string temp;
      vector<string> string_vec;
      vector<int> int_vec;
      set<int,greater<int> > set_product;
      int product = 0;
      ifs.open("problem_8.txt");
      while( getline(ifs , temp))
        string_vec.push_back(temp);
      for(int i = 0 ; i<string_vec.size();++i)
        {
          temp = *(string_vec.begin()+i);
          for(int i = 0 ; i < temp.length() ; i++)
    	{
    	  //  cout << temp[i];
    	  int_vec.push_back(static_cast<int> (temp[i]) - 48 );
    	}
        }
    }
    Last edited by nimitzhunter; 01-20-2011 at 10:48 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by bernt View Post
    Reading into a vector is about all you can do. That's how all the arbitrary precision libraries handle large numbers internally, more or less (well, probably arrays, but it's the same idea). If you're concerned about space you can use a string and/or convert to base 256.
    Not mine. Mine stores the value in base 4294967296 i.e. it's stored in an array of 32-bit ints, wasting nothing, and built for speed of manipulation.
    It can be found on the useful classes section of my website. Some others work the same way.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 07:38 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. reading a number from a file
    By the_head in forum C Programming
    Replies: 2
    Last Post: 10-02-2003, 09:25 PM
  4. Reading files number by number, also getw()?
    By rmullen3 in forum C Programming
    Replies: 4
    Last Post: 01-03-2003, 12:22 PM
  5. Reading Large Files!!!
    By jon in forum Windows Programming
    Replies: 1
    Last Post: 09-09-2001, 11:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21