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

前端 未结 10 1895
别跟我提以往
别跟我提以往 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:44

    I used bitwise shifting to left. Then converting to array and summing its elements. My end result is 1366, Do not forget to add reference to System.Numerics;

    BigInteger i = 1;
             i = i << 1000;
            char[] myBigInt = i.ToString().ToCharArray();
            long sum = long.Parse(myBigInt[0].ToString());
            for (int a = 0; a < myBigInt.Length - 1; a++)
            {
                sum += long.Parse(myBigInt[a + 1].ToString());
            }
            Console.WriteLine(sum);
    

提交回复
热议问题