long decimal interger

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>

using namespace std;

class Long
{
public:
	Long(unsigned long long int n = 0);
	void double();
	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.

Last edited on
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.
I think that sounds about right. The addDigits() and print() functions seem relatively straightforward. That just leaves the doubling to take care of.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "long.h"
#include <sstream>

Long::Long(unsigned long long int n)
{
	value.push_back(n);
}
void Long::print()
{
	for (vector<int>::const_reverse_iterator i = value.rbegin(); i != value.rend(); ++i)
	{
		cout << *i << " ";
	}
	cout << endl;
}
void Long::double()
{
	long long int x;
	stringstream s;
	vector<int> newVector(value);

	for(vector<int>::const_reverse_iterator i = newVector.rbegin(); i != newVector.rend(); ++i)
	{
		s << *i;
	}

	string ss = s.str().c_str();
	x = atoi(ss.c_str());

	while (!value.empty())
	{
		value.pop_back();
	}
	
	x = x * 2;

	for (; x; x /= 10)
	{
		value.push_back(x % 10);
	}
}
void Long::addDigits()
{
	int sum = 0;

	for (vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
	{
		sum += *i;
	}

	cout << "Sum of digits: " << sum << endl;
}
Last edited on
int sum = 0;

maxium value is 2^32. You can't use it as sum output. You need to store sum as vector and print it as vector.
Last edited on
Yes, an unsigned int usually can handle up to 2^32, and type unsigned long long 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.

Last edited on
Topic archived. No new replies allowed.