How to store very very large value in C/C++

Sanyam Khurana

Sanyam Khurana

@sanyam-Nl7Zqc Oct 23, 2024
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..

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Sanyam Khurana

    Sanyam Khurana

    @sanyam-Nl7Zqc Sep 9, 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

    Jeffrey Arulraj

    @jeffrey-xA7lUP Sep 9, 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

    Sanyam Khurana

    @sanyam-Nl7Zqc Sep 10, 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

    rahul69

    @rahul69-97fAOs Sep 10, 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

    Sanyam Khurana

    @sanyam-Nl7Zqc Sep 10, 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

    simplycoder

    @simplycoder-NsBEdD Sep 10, 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

    simplycoder

    @simplycoder-NsBEdD Sep 10, 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

    Sanyam Khurana

    @sanyam-Nl7Zqc Sep 10, 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

    simplycoder

    @simplycoder-NsBEdD Sep 10, 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

    Sanyam Khurana

    @sanyam-Nl7Zqc Sep 10, 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

    simplycoder

    @simplycoder-NsBEdD Sep 11, 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

    Sanyam Khurana

    @sanyam-Nl7Zqc Sep 11, 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

    Jeffrey Arulraj

    @jeffrey-xA7lUP Sep 11, 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?