下面我有以下代码,我快速找到了最大Hailstone sequence的数字,介于1和99999之间。该程序最高可达35655,产生的钻石数为324.任何人都熟悉挑战将知道最大的序列由77031生成(给出354)。您可以从我的println()
语句输出中看到,if statement
由于某种原因,一旦超过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)
答案 0 :(得分:3)
您使用largest
表示最大尺寸,也表示产生最大尺寸的值。
您需要两个变量:largest
和startingValueForLargest
。
largest = i;
应该是
largest = size;
startingValueForLargest = i;
目前发生的事情是largest
被设置为35655,因此354没有击败它。