SHOW:
|
|
- or go back to the newest paste.
| 1 | - | public class CollatzProblem {
|
| 1 | + | public class CollatzProblem {
|
| 2 | - | |
| 2 | + | |
| 3 | - | public static void main(String[] args) {
|
| 3 | + | public static void main(String[] args) {
|
| 4 | - | // This is the number the program will go up to, so to find maximum from 1 to 100, we make k = 100 |
| 4 | + | // This is the number the program will go up to, so to find maximum from 1 to 100, we make k = 100 |
| 5 | - | int k = 1000000; |
| 5 | + | double k = 1000000; |
| 6 | - | // An array to keep track of the values we get |
| 6 | + | // An array to keep track of the values we get |
| 7 | - | int[] length = new int[k]; |
| 7 | + | double[] length = new double[(int)k]; |
| 8 | - | |
| 8 | + | |
| 9 | - | // Loop through all possible starting values, from 1 to 'k' |
| 9 | + | // Loop through all possible starting values, from 1 to 'k' |
| 10 | - | for(int start = 1; start < k; ++start){
|
| 10 | + | for(double start = 1; start < k; ++start){
|
| 11 | - | int current = start; |
| 11 | + | double current = start; |
| 12 | - | int count = 0; |
| 12 | + | double count = 0; |
| 13 | - | |
| 13 | + | |
| 14 | - | // This is where iteration happens, we keep iterating until we get a '1' |
| 14 | + | // This is where iteration happens, we keep iterating until we get a '1' |
| 15 | - | // While doing so it counts each iteration |
| 15 | + | // While doing so it counts each iteration |
| 16 | - | while(current != 1){
|
| 16 | + | while(current != 1){
|
| 17 | - | current = iterate(current); // See next method to see how it iterates |
| 17 | + | current = iterate(current); // See next method to see how it iterates |
| 18 | - | ++count; |
| 18 | + | ++count; |
| 19 | - | } |
| 19 | + | } |
| 20 | - | |
| 20 | + | |
| 21 | - | length[start] = count; |
| 21 | + | length[(int)start] = count; |
| 22 | - | |
| 22 | + | |
| 23 | - | } |
| 23 | + | } |
| 24 | - | // This will find the actual maximum value in the array |
| 24 | + | // This will find the actual maximum value in the array |
| 25 | - | int max = length[0]; |
| 25 | + | double max = length[0]; |
| 26 | - | |
| 26 | + | |
| 27 | - | for(int i=0; i< k; ++i){
|
| 27 | + | for(double i=0; i< k; ++i){
|
| 28 | - | if(max < length[i]){
|
| 28 | + | if(max < length[(int)i]){
|
| 29 | - | max = length[i]; |
| 29 | + | max = length[(int)i]; |
| 30 | - | } |
| 30 | + | } |
| 31 | - | } |
| 31 | + | } |
| 32 | - | // This will find the value that belongs to this 'max', this is the value we want |
| 32 | + | // This will find the value that belongs to this 'max', this is the value we want |
| 33 | - | int maxIndex = 0; |
| 33 | + | double maxIndex = 0; |
| 34 | - | |
| 34 | + | |
| 35 | - | for(int i=0; i<k; ++i){
|
| 35 | + | for(double i=0; i<k; ++i){
|
| 36 | - | if(max == length[i]){
|
| 36 | + | if(max == length[(int)i]){
|
| 37 | - | maxIndex = i; |
| 37 | + | maxIndex = i; |
| 38 | - | } |
| 38 | + | } |
| 39 | - | } |
| 39 | + | } |
| 40 | - | |
| 40 | + | |
| 41 | - | System.out.println("Index: " + maxIndex + " Amount: " + max);
|
| 41 | + | System.out.println("Index: " + maxIndex + " Amount: " + max);
|
| 42 | - | |
| 42 | + | |
| 43 | - | } |
| 43 | + | } |
| 44 | - | |
| 44 | + | |
| 45 | - | public static int iterate(int n){
|
| 45 | + | public static double iterate(double n){
|
| 46 | - | // Do as the function says, if n is even, halve it, if n is odd, 3*n+1 is output |
| 46 | + | // Do as the function says, if n is even, halve it, if n is odd, 3*n+1 is output |
| 47 | - | int out = 0; |
| 47 | + | double out = 0; |
| 48 | - | |
| 48 | + | |
| 49 | - | if(n%2 == 0){
|
| 49 | + | if(n%2 == 0){
|
| 50 | - | out = n/2; |
| 50 | + | out = n/2; |
| 51 | - | } |
| 51 | + | } |
| 52 | - | if(n%2 != 0){
|
| 52 | + | if(n%2 != 0){
|
| 53 | - | out = 3*n+1; |
| 53 | + | out = 3*n+1; |
| 54 | - | } |
| 54 | + | } |
| 55 | - | return out; |
| 55 | + | return out; |
| 56 | - | } |
| 56 | + | } |
| 57 | - | |
| 57 | + | |
| 58 | - | } |
| 58 | + | } |