使用整数和双精度时,不应该提供不同的答案 [英] Use of integers and doubles give different answers when they shouldn't

查看:159
本文介绍了使用整数和双精度时,不应该提供不同的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java解决项目欧拉问题14 。我不是要求帮助解决问题。我已经解决了,但是我遇到了一些我无法想象的东西。

I'm solving a Project Euler Problem 14 using java. I am NOT asking for help solving the problem. I have already solved it, but I ran into something I can't figure out.

问题是这样的:


为正的
整数定义了以下迭代序列:

The following iterative sequence is defined for the set of positive integers:

n = n / 2,if n是甚至

n = 3n + 1,如果n是奇数

n = n/2, if n is even
n = 3n + 1, if n is odd

使用上面的规则,从13开始,我们生成以下
序列:

Using the rule above and starting with 13, we generate the following sequence:

13 - > 40 - > 20 - > 10 - > 5 - > 16 - > 8 - > 4 - > 2 - > 1.这里链的长度为10个数字。

13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1. Here, the length of the chain is 10 numbers.

查找产生最长链的1,000,000以下的起始数。

Find the starting number below 1,000,000 that produces the longest chain.

所以我写了这段代码:

public class Euler014 {
    public static void main(String[] args){
        int maxChainCount = 0;
        int answer = 0;
        int n;
        int chainCount = 1;

        for(int i = 0; i < 1000000; i++){
            n = i;
            while(n > 1){
                if(n%2 == 0){       //check if even
                    n /= 2;
                }else{              //else: odd
                    n = 3*n + 1;
                }
                chainCount++;
            }
            if(chainCount > maxChainCount){ //check if it's the longest chain so far
                maxChainCount = chainCount;
                answer = i;
            }
            chainCount = 1;
        }       
        System.out.println("\n\nLongest chain: i = " + answer);     
    }
}

这给了我910107的答案,这是错误的。

This gives me the answer 910107, which is wrong.

但是,如果我将n变量的类型更改为 double n ,它运行并给出答案837799 ,这是对的!

HOWEVER, if i change the type of my n variable to double n it runs and gives me the answer 837799, which is right!

这真的让我感到困惑,因为我看不出有什么区别。我明白,如果我们使用 int 并做分区,我们可以在不打算的情况下结束四舍五入的数字。但是在这种情况下,我们总是检查一下,如果 n 是除以2,除以2.所以我认为使用整数是完全安全的。我看不到什么?

This really confuses me, as I can't see what the difference would be at all. I understand that if we use int and do divisions we can end up rounding numbers when we don't intend to. But in this case, we always check to see if the n is divisble by 2, BEFORE dividing by 2. So I thought that it would be totally safe to use integers. What am I not seeing?

如果您想自己看,这是代码整体,复制,粘贴和运行。它运行在几秒钟,尽管很多迭代。 =)

This is the code in its entirety, copy, paste and run it if you'd like to see for yourself. It runs in a couple of seconds despite much iteration. =)

推荐答案

你的问题是溢出的。如果您将 int n 更改为 long n ,您将得到正确的答案。

Your problem is overflow. If you change int n to long n, you'll get the right answer.

记住:序列中的数字可以是 真的很大 。这么大,他们溢出 int 的范围。但不是(在这种情况下) double long

Remember: The numbers in the sequence can be really big. So big they overflow int's range. But not (in this case) double's, or long's.

在链中的某个位置, n 827,370,449 ,然后您按 3n + 1 分支。该价值要成为 2,482,111,348 ,但是它的容量会超过 int (这是 2,147,483,647 在积极的领域),并带您到 -1,812,855,948 。事情从那里向南。 : - )

At one point in the chain, n is 827,370,449 and you follow the 3n + 1 branch. That value wants to be 2,482,111,348, but it overflows the capacity of int (which is 2,147,483,647 in the positive realm) and takes you to -1,812,855,948. And things go south from there. :-)

所以你的理论,你会很好的整数(我应该说整数)数字是正确的。但是他们必须有这个任务的能力。

So your theory that you'd be fine with integer (I should say integral) numbers is correct. But they have to have the capacity for the task.

这篇关于使用整数和双精度时,不应该提供不同的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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