How to store very very large value in C/C++
Hello,
I was writing a code to find out the number 2^1000.
Which is very very large, and no data type can handle this..
Actually I have to use the answer I get, and print the sum of it's digits.
So, the question is how can I really save this number, or there's another way without saving this.?
PS:- The code I have written is this..
I was writing a code to find out the number 2^1000.
Which is very very large, and no data type can handle this..
Actually I have to use the answer I get, and print the sum of it's digits.
So, the question is how can I really save this number, or there's another way without saving this.?
PS:- The code I have written is this..
#include#include #include void main() { clrscr(); long unsigned int res; //ignore the data types as of for now.. int i,sum; sum=0; res=pow(2,1000); cout< 0) //I know this loop will work, but just need to store that big value in { // variable res first.. i=res%10; res/=10; sum+=i; } cout< If I go for a double data type, then the loop would not work, because of the floating point values..
Therefore, I need a way to do this...
Thanks..
Replies
-
Sanyam KhuranaJust a thought, I may be wrong..
I'm not sure, but could this be done through structures..(may be)?
we may define a new data type, but again the question comes, how to store that large number...breaking it into smaller parts would be tedious..
Isn't it...?
or is it feasible? -
Jeffrey Arulraj
I may be wrong But Is not structures a way of grouping pre existing basic data typesSanyam KhuranaJust a thought, I may be wrong..
I'm not sure, but could this be done through structures..(may be)?
we may define a new data type, but again the question comes, how to store that large number...breaking it into smaller parts would be tedious..
Isn't it...?
or is it feasible?
I am not sure other thing But structures can't be used to do what you have just suggested -
Sanyam Khurana
I thought this as structure is a way to define a new data type, it is actually meant for creating a user defined data type.Jeffrey SamuelI may be wrong But Is not structures a way of grouping pre existing basic data types
I am not sure other thing But structures can't be used to do what you have just suggested
What I was thinking, is making a struct and define different arrays of highest data type which can be handled in this program ie unsigned long int, then I can divide that large value (2^1000) in those arrays, but this seems difficult to me, so I was asking if there's a better way to do this..
And I believe there would be a much better way.. -
rahul69
Actually the approach u are using is wrong, see that pow() function will not give accurate values for such large values (as it will fit values to double size etc.), so it will be better to write your own power function with your custom data type as parameters,Sanyam KhuranaHello,
I was writing a code to find out the number 2^1000.
Which is very very large, and no data type can handle this..
Actually I have to use the answer I get, and print the sum of it's digits.
...
If I go for a double data type, then the loop would not work, because of the floating point values..
Therefore, I need a way to do this...
Thanks..
As for custom datatypes I think arrays will suffice, use arrays of integer type and do multiplication using them, so u will have your result as "each digit of number as each cell inside the resultant array" then u may add all elements of the array to get the sum u need.
Hope it is clear, looking forward for ur feedback ๐ -
Sanyam Khurana
Ok I'll try to do this and get back to you soonrahul69Actually the approach u are using is wrong, see that pow() function will not give accurate values for such large values (as it will fit values to double size etc.), so it will be better to write your own power function with your custom data type as parameters,
As for custom datatypes I think arrays will suffice, use arrays of integer type and do multiplication using them, so u will have your result as "each digit of number as each cell inside the resultant array" then u may add all elements of the array to get the sum u need.
Hope it is clear, looking forward for ur feedback ๐
But I am not sure if I can have a very big array like this... -
simplycoderAs I see this, this is the 16th problem from project-euler.
There is a big integer library, check for boost.
I would have stored it in an array. -
simplycoder
2^1000 wouldn't require a very big array.Sanyam KhuranaOk I'll try to do this and get back to you soon
But I am not sure if I can have a very big array like this...
Can you approximate size of the array required? -
Sanyam Khurana
Yes, it's from Project Euler...simplycoderThere is a big integer library, check for boost.
I would have stored it in an array.
But what do you mean by this big integer library and boost..?๐
I think approx 10k...๐simplycoder2^1000 wouldn't require a very big array.
Can you approximate size of the array required?
I'm not sure...๐ -
simplycoderI won't step into the mathematics of finding the number of digits,
but there would be significantly less than 1000 digits.
pow(2,n) => Number Of Digits.
pow(2,1) => 1
pow(2,2) => 1
pow(2,3) => 1
---------------
pow(2,4) => 2
pow(2,5) => 2
pow(2,6) => 2
---------------
pow(2,7) => 3
...
Just taking a wild guess, pow(2,1000) would contain maximum of 1000/3 = 333 digits approximately.
Now lets take some math into consideration.
To find number of digits,
Assume pow(2,n)=pow(10,x)
Taking base10-logarithms on both side,
x approx to (n*0.3010).
Also note one thing, a number of power 2 would not be equal to power of 10 as power of 2 would end only in 2,4,6,8.
Number of digits in pow(2,n) =>d= floor(n*0.3010)+1
So when n=1000,
d=floor(1000*0.3010)+1
d=302. -
Sanyam Khurana
Couldn't understand this stuff, could you make it more clear please..?simplycoderNumber of digits in pow(2,n) =>d= floor(n*0.3010)+1
So when n=1000,
d=floor(1000*0.3010)+1
d=302.
Thanks... -
simplycoder
Its just a simple substitution when n=1000.Sanyam KhuranaCouldn't understand this stuff, could you make it more clear please..?
Thanks...
You can try for various values of n -
Sanyam KhuranaI just want to know, how you arrived to this thing..
pow(2,n) =>d= floor(n*0.3010)+1
Just how? -
Jeffrey ArulrajMathematical Induction is one of the reasons we are having this equation mate \
No serious logic behind this just proved that the Kth term satisfies this condition and so we took this for an universal standard
Sanyam Khuranapow(2,n) =>d= floor(n*0.3010)+1
Just how?
You are reading an archived discussion.
Related Posts
"Since childhood I am told only one thing "Don't dare to dream
big. Study well to get a good job". And I betrayed that
teaching. I lived the last decade...
I am Tomato, it's really great to be here... an excellent place for all the Engineers I want to share my knowledge to everyone and also get some knowledge from...
Just read up an article on Wikipedia about the largest capacity USB drive in existence. Was expecting about 512 GB, but found out that flash drives offering 1TB (1000 GB)...
I have a database table with following cols
Id, Name, Date (FORMAT: Y-m-d H:i:s)
Now I want to retrieve data of following form
Count Year
3 2013
5 2014
It...
I am looking for educational videos that can be downloaded from Youtube. Like Megafactories, Megastructures. Do you have any suggestions?