What is the sum of the digits of the number 2^1000?

前端 未结 10 1837
别跟我提以往
别跟我提以往 2021-01-02 03:13

This is a problem from Project Euler, and this question includes some source code, so consider this your spoiler alert, in case you are interested in solving it yourself. It

相关标签:
10条回答
  • 2021-01-02 03:46

    Try using BigInteger type , 2^100 will end up to a a very large number for even double to handle.

    0 讨论(0)
  • 2021-01-02 03:47

    For calculating the values of such big numbers you not only need to be a good programmer but also a good mathematician. Here is a hint for you, there's familiar formula ax = ex ln a , or if you prefer, ax = 10x log a.

    More specific to your problem 21000 Find the common (base 10) log of 2, and multiply it by 1000; this is the power of 10. If you get something like 1053.142 (53.142 = log 2 value * 1000) - which you most likely will - then that is 1053 x 100.142; just evaluate 100.142 and you will get a number between 1 and 10; and multiply that by 1053, But this 1053 will not be useful as 53 zero sum will be zero only.

    For log calculation in C#

    Math.Log(num, base);
    

    For more accuracy you can use, Log and Pow function of Big Integer.

    Now rest programming help I believe you can have from your side.

    0 讨论(0)
  • 2021-01-02 03:47
     main()
     {
       char c[60];
      int k=0;
         while(k<=59)
          {
        c[k]='0';
       k++;
    
        }
           c[59]='2';
           int n=1;
         while(n<=999)
           {
           k=0;
         while(k<=59)
          {
            c[k]=(c[k]*2)-48;
            k++;
          } 
        k=0;
         while(k<=59)
            {
            if(c[k]>57){ c[k-1]+=1;c[k]-=10;   }
           k++;
             }
           if(c[0]>57)
            {
             k=0;
             while(k<=59)
               {
             c[k]=c[k]/2;
              k++;
               }
               printf("%s",c);
                 exit(0);
               }
    
                n++;
                }
              printf("%s",c);
                  } 
    
    0 讨论(0)
  • 2021-01-02 03:50

    Python makes it very simple to compute this with an oneliner:

    print sum(int(digit) for digit in str(2**1000))
    

    or alternatively with map:

    print sum(map(int,str(2**1000)))
    
    0 讨论(0)
提交回复
热议问题