I do not understand this problem. I am trying to print out 2^1000. I am not entirely sure on how to implement the constructor. I currently just have x.push_back(n) as my constructors implementation. For the function double(), it is suppose print 2^10 as 1024 and the function addDigits() should print 2^10 as 7. Am I suppose to create a Long object with 2 as a parameter and then use double() to double it 10 times?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <vector>
#include <iterator>
usingnamespace std;
class Long
{
public:
Long(unsignedlonglongint n = 0);
voiddouble();
void print();
void addDigits();
private:
vector<int> x;
};
2^1000 has just over 300 (decimal) digits. I would assume that you need to store each digit separately in your vector. That is, each element in the vector will contain a value in the range 0 to 9.
If you follow that idea, then the constructor would need to decompose the parameter n into individual decimal digits, and store each one as required.
I'm talking in terms of decimal digits here, though often it is more efficient to store and manipulate numbers in binary, here the important requirement will be to print out the result, and storing the digits individually will make that a whole lot easier.
Am I suppose to create a Long object with 2 as a parameter and then use double() to double it 10 times?
I think you should use 1 as the parameter. Then double it 10 times.
20 = 1
21 = 2
22 = 4
23 = 8
etc.
Thanks for the explanation.
So initially my constructor should make 1 as an element in the vector x. And after doubling it 8 times to get 256. The vector then would contain the elements 6, 5, and 2 which then my print() function would print it out in reverse to show 2, 5, and 6. The addDigits() will then add the digits of 256 to get 13.
This is what I got so far. The double() function takes a vector for example: {1, 0, 2, 4} converts it to a string = "1024" and then converts back into int = 1024. Everything works fine until I get to around 2^32, it starts giving the same number 4294967294. It seems like I'm reaching the string size limit. I am having trouble trying to get the sum of digits of 2^1000 without storing it.
Yes, an unsignedint usually can handle up to 2^32, and type unsignedlonglong up to 2^64. (To be accurate, 2^32-1 since counting starts at zero).
Think of how you would handle this using pencil and paper. If the value after doubling a digit is greater than 9, store result%10 as the new digit, and use a temporary variable to hold the 'carry' which is result/10. That carry value gets added into the next column.