-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution14.java
64 lines (57 loc) · 1.36 KB
/
Solution14.java
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
//Problem 14 - Project Euler
/*
GOAL:
(Collatz Problem)
Do the following with the numbers below 1,000,000:
n → n/2 (n is even)
n → 3n + 1 (n is odd)
Till you reach 1
LOGIC:
STEP1:
Initially start with 1, apply the rules till you reach 1, on each application of the rule have a counter
STEP2:
Move to the next number, repeat step 1.
STEP3:
Compare the counts, and get the number that gave the max count.
*/
import java.io.*;
import java.util.*;
public class Solution14 {
public static void main(String[] args) {
long counter = 0;
long big_count = Long.MIN_VALUE;
long final_num = 0;
long test_num = 1;
long store = 0;
long intial_start = 1;
while(intial_start<1000000){
//do not touch the starting number
//test with a test number
test_num = intial_start;
//loop with rules
while(final_num!=1){
if(test_num%2 == 0){
test_num/=2;
final_num = test_num;
counter++;
}else{
test_num = 3*(test_num) + 1;
final_num = test_num;
counter++;
}
}
if(counter>big_count){
big_count = counter;
//get the number with max count
store = intial_start;
}
//reset
final_num = 0;
counter = 0;
//go to next number
intial_start ++;
}
System.out.println("ANSWER: " + store);
}
}
// ANSWER: 837799