CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@Sanyam Khurana • 09 Sep, 2013 • 1 like
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..

#include<iostream.h>
#include<conio.h>
#include<math.h>
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<<res<<endl;
while(res>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<<sum;
getch();
}
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..
@Sanyam Khurana • 09 Sep, 2013 Just 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 • 09 Sep, 2013
Sanyam Khurana
Just 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 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
@Sanyam Khurana • 10 Sep, 2013
Jeffrey Samuel
I 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
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.

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 • 10 Sep, 2013
Sanyam Khurana
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.
...
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..
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,
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 • 10 Sep, 2013
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,
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 😀
Ok 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...
@simplycoder • 10 Sep, 2013 As 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 • 10 Sep, 2013
Sanyam Khurana
Ok 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...
2^1000 wouldn't require a very big array.
Can you approximate size of the array required?
@Sanyam Khurana • 10 Sep, 2013
simplycoder
There is a big integer library, check for boost.
I would have stored it in an array.
Yes, it's from Project Euler...

But what do you mean by this big integer library and boost..?😐

simplycoder
2^1000 wouldn't require a very big array.
Can you approximate size of the array required?
I think approx 10k...😐

I'm not sure...😕
@simplycoder • 10 Sep, 2013 I 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 • 11 Sep, 2013
simplycoder
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.
Couldn't understand this stuff, could you make it more clear please..?

Thanks...
@simplycoder • 11 Sep, 2013
Sanyam Khurana
Couldn't understand this stuff, could you make it more clear please..?

Thanks...
Its just a simple substitution when n=1000.
You can try for various values of n
@Sanyam Khurana • 11 Sep, 2013 I just want to know, how you arrived to this thing..

pow(2,n) =>d= floor(n*0.3010)+1

Just how?
@Jeffrey Arulraj • 11 Sep, 2013 Mathematical 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 Khurana
pow(2,n) =>d= floor(n*0.3010)+1

Just how?
5.8k views

Related Posts

@Satya Swaroop Dash · Nov 16, 2017

WhatsApp, the messaging service that has become a necessity in the age of smartphones shows no hints of slowing down. WhatsApp tries to integrate features that it sees on other...
7.7k views

@Ankita Katdare · Jun 26, 2015

Nikesh Garera comes from a strong background in computer science engineering. Having did his graduation from University of Mumbai (B.E.) in the year 2002, he went on to pursue M.S....
4k views

@Ankita Katdare · Sep 2, 2015

Yesterday on Tata Sky, we were facing signal related issue. The weather was not at all cloudy. In fact, it was really sunny all day. Me and many of my...
227.4k views

@Neeraj Subhedar · Feb 21, 2012

Hi, I am doing B.Tech. in Biomedical Engineering and wish to pursue masters in the same field later. How many seats are alloted in IITs for M.tech. in biomedical engineering,...
3.7k views

@Kaustubh Katdare · Nov 16, 2014

WhatsApp has decided to let people disable the double blue ticks - that let the sender know when their message was read. WhatsApp (owned by Facebook) had recently introduced a...
3.6k views