什么是数2 ^ 1000的数字的总和? [英] What is the sum of the digits of the number 2^1000?

查看:215
本文介绍了什么是数2 ^ 1000的数字的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 的问题从项目欧拉,这个问题包含了一些源代码,所以认为这你扰流警报,如果你有兴趣,你自己解决吧。这是气馁的解决方案分发到的问题,这不是我想要的。我只需要在正确的方向一点点微调和指导,真诚。

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 is discouraged to distribute solutions to the problems, and that isn't what I want. I just need a little nudge and guidance in the right direction, in good faith.

问题内容如下:

2 ^ 15 = 32768和其位数之和为3 + 2 + 7 + 6 + 8 = 26

2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

是什么的数目的数字的总和2 ^ 1000欧

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

我理解这个问题的前提和数学,但我只开始练C#一个星期前,所以我的编程是不稳固的最好的。

I understand the premise and math of the problem, but I've only started practicing C# a week ago, so my programming is shaky at best.

我知道的int,long和double是抱着300+(基数为10)无可救药不足2 ^ 1000精确数字,因此需要

的一些策略。我的策略是设置一个计算它得到的数字一个接一个,并希望编译器能想出如何计算每个数字没有像溢出一些错误:

I know that int, long and double are hopelessly inadequate for holding the 300+ (base 10) digits of 2^1000 precisely, so some strategy is needed. My strategy was to set a calculation which gets the digits one by one, and hope that the compiler could figure out how to calculate each digit without some error like overflow:

using System;
using System.IO;
using System.Windows.Forms;

namespace euler016
{
    class DigitSum
    {
        // sum all the (base 10) digits of 2^powerOfTwo
        [STAThread]
        static void Main(string[] args)
        {
            int powerOfTwo = 1000;
            int sum = 0;

            // iterate through each (base 10) digit of 2^powerOfTwo, from right to left
            for (int digit = 0; Math.Pow(10, digit) < Math.Pow(2, powerOfTwo); digit++)
            {
                // add next rightmost digit to sum
                sum += (int)((Math.Pow(2, powerOfTwo) / Math.Pow(10, digit) % 10));
            }
            // write output to console, and save solution to clipboard
            Console.Write("Power of two: {0} Sum of digits: {1}\n", powerOfTwo, sum);
            Clipboard.SetText(sum.ToString());
            Console.WriteLine("Answer copied to clipboard. Press any key to exit.");
            Console.ReadKey();
        }
    }
}



这似乎很好地工作了powerOfTwo< 34.我的计算器跑出上面显著的数字,所以我无法测试更高的权力。但跟踪程序,它看起来像没有溢出发生:数字逐渐计算的数量增加为powerOfTwo = 1000增加,数字也和(平均)随着powerOfTwo增加

It seems to work perfectly for powerOfTwo < 34. My calculator ran out of significant digits above that, so I couldn't test higher powers. But tracing the program, it looks like no overflow is occurring: the number of digits calculated gradually increases as powerOfTwo = 1000 increases, and the sum of digits also (on average) increases with increasing powerOfTwo.

有关实际计算我应该去执行,我得到的输出:

For the actual calculation I am supposed to perform, I get the output:

两种力量:1000总和数字:1189

Power of two: 1000 Sum of digits: 1189

但1189是不是正确的答案。什么是错我的程序?我愿意接受任何和所有建设性的批评。

But 1189 isn't the right answer. What is wrong with my program? I am open to any and all constructive criticisms.

推荐答案

普通的 INT 不能帮你这么大数量。甚至没有 。他们从来没有设计用于处理数字如此巨大。 INT 可存储大约10位数字(确切的最大: 2,147,483,647 )和周边的19位(确切的最大: 9,223,372,036,854,775,807 )。然而,快速计算,从内置的Windows计算器告诉我 2 ^ 1000年是一个数字的300余位。

Normal int can't help you with such a large number. Not even long. They are never designed to handle numbers such huge. int can store around 10 digits (exact max: 2,147,483,647) and long for around 19 digits (exact max: 9,223,372,036,854,775,807). However, A quick calculation from built-in Windows calculator tells me 2^1000 is a number of more than 300 digits.

(边注:可以从 int.MAX_VALUE的获得精确的数值和 long.MAX_VALUE 分别)

(side note: the exact value can be obtained from int.MAX_VALUE and long.MAX_VALUE respectively)

当你想要的精确的数字相加,甚至 浮动 类型将无法工作,因为他们只为少数显著数字存储到几十数字。 ( 7 数字浮法, 15-16 为两位数)。 这里阅读有关浮点表示的更多信息,双精度

As you want precise sum of digits, even float or double types won't work because they only store significant digits for few to some tens of digits. (7 digit for float, 15-16 digits for double). Read here for more information about floating point representation, double precision

不过,C#提供了一个内置的算术
BigInteger的 任意精度,应适合你(测试)的需要。即可以在任何数量的数字做算术题(从理论上当然,在实践中它是由您的物理机器真正的内存的限制,而且需要一定的时间过于依赖于你的CPU功率)

However, C# provides a built-in arithmetic BigInteger for arbitrary precision, which should suit your (testing) needs. i.e. can do arithmetic in any number of digits (Theoretically of course. In practice it is limited by memory of your physical machine really, and takes time too depending on your CPU power)

回到你的代码,我认为这个问题是在这里。

Back to your code, I think the problem is here

Math.Pow(2 ,powerOfTwo)

这溢出的计算。嗯,不是真的,但它是双击精确度没有精确地表示结果的实际价值,就像我说的。

This overflows the calculation. Well, not really, but it is the double precision is not precisely representing the actual value of the result, as I said.

这篇关于什么是数2 ^ 1000的数字的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆