gpt4 book ai didi

java - Java 中的 Collat​​z/Hailstone 序列 - 意外行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:48 24 4
gpt4 key购买 nike

我在下面有以下代码,我将它们快速组合在一起以找到最大的数字 Hailstone sequence ,介于 1 和 99999 之间。该程序在 35655 之前运行良好,生成的冰雹计数为 324。熟悉该挑战的任何人都知道最大的序列是由 77031 生成的(给出 354)。您可以从我的 println() 语句输出中看到,出于某种原因,if 语句 在超过 33655 后就停止求值。另一个 println( ) 语句验证我的 generateSequence() 方法似乎工作正常。关于这里出了什么问题有什么想法吗?

下面的代码。

import java.util.ArrayList;


public class Hailstone {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Hailstone hs = new Hailstone();
}

public Hailstone () {

System.out.println("Size of sequence for 35655 is: " + generateSequence(35655).size());
System.out.println("Size of sequence for 77031 is: " + generateSequence(77031).size());
calcLargestSeqLength(100000);
}

/**
*
* finds the number with the largest hailstone sequence between 1 and aNum
*/
private void calcLargestSeqLength(int aNum) {

int largest = 0;
for (int i = 1; i < aNum; i++)
{
int size = generateSequence(i).size();
if(size > largest)
{
System.out.println("if statement evaluated to true for: " + i + " / " + size);
largest = i;

}
}
System.out.println("Number with the largest sequence is: " + largest);
}

private ArrayList<Integer> generateSequence(int aNum) {

ArrayList<Integer> hSeq = new ArrayList();

int x = aNum;
hSeq.add(x);

if (x != 1)
{
while (x != 1)
{
if ((x % 2) == 0)
{
x = x/2;
hSeq.add(x);
}
else
{
x = (3 * x) + 1;
hSeq.add(x);
}
}
}
return hSeq;
}

NetBeans 的输出:

Size of sequence for 35655 is: 324
Size of sequence for 77031 is: 351
if statement evaluated to true for: 1
if statement evaluated to true for: 2
if statement evaluated to true for: 3
if statement evaluated to true for: 5
if statement evaluated to true for: 6
if statement evaluated to true for: 7
if statement evaluated to true for: 9
if statement evaluated to true for: 11
if statement evaluated to true for: 14
if statement evaluated to true for: 15
if statement evaluated to true for: 18
if statement evaluated to true for: 19
if statement evaluated to true for: 25
if statement evaluated to true for: 27
if statement evaluated to true for: 31
if statement evaluated to true for: 39
if statement evaluated to true for: 41
if statement evaluated to true for: 47
if statement evaluated to true for: 54
if statement evaluated to true for: 55
if statement evaluated to true for: 62
if statement evaluated to true for: 63
if statement evaluated to true for: 71
if statement evaluated to true for: 73
if statement evaluated to true for: 82
if statement evaluated to true for: 83
if statement evaluated to true for: 91
if statement evaluated to true for: 94
if statement evaluated to true for: 95
if statement evaluated to true for: 97
if statement evaluated to true for: 107
if statement evaluated to true for: 108
if statement evaluated to true for: 109
if statement evaluated to true for: 110
if statement evaluated to true for: 129
if statement evaluated to true for: 313
if statement evaluated to true for: 35655
Number with the largest sequence is: 35655
BUILD SUCCESSFUL (total time: 1 second)

最佳答案

您使用 largest 表示最大尺寸,也表示生成最大尺寸的值。

您需要两个变量:largeststartingValueForLargest

largest = i;

应该是

largest = size;
startingValueForLargest = i;

目前正在发生的事情是 largest 被设置为 35655,所以 354 没有超过它。

关于java - Java 中的 Collat​​z/Hailstone 序列 - 意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32555023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com