-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpe14_java.java
More file actions
70 lines (60 loc) · 1.57 KB
/
Copy pathpe14_java.java
File metadata and controls
70 lines (60 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class pe14_java
{
private static class CurrentResult
{
private long bestNum;
private long bestNumCount;
public CurrentResult(long bestNum, long bestNumCount)
{
this.bestNum = bestNum;
this.bestNumCount = bestNumCount;
}
public long getBestNum()
{
return bestNum;
}
public long getBestNumCount()
{
return bestNumCount;
}
}
private static long chain_count(long n)
{
long curr_val = n;
long count = 0;
while( curr_val != 1 )
{
if( (curr_val % 2) == 0 )
{
curr_val >>= 1;
}
else
{
curr_val = curr_val * 3 + 1;
}
count++;
}
return count;
}
private static CurrentResult collatz(long minR, long maxR)
{
CurrentResult result = new CurrentResult(0, 0);
for( long i = minR; i <= maxR; i++ )
{
long n = chain_count(i);
if( n > result.getBestNumCount() )
{
result = new CurrentResult(i, n);
}
}
return result;
}
public static void main(String[] args)
{
CurrentResult result = collatz(1, 1000000);
System.out.println("Resulting number is "
+ result.getBestNum()
+ ", max count is "
+ result.getBestNumCount());
}
}