Sy123
This too shall pass
- Joined
- Nov 6, 2011
- Messages
- 3,730
- Gender
- Male
- HSC
- 2013
Hello all,
I am posting this here because it has something to do with mathematics, but mostly programming, however the programming forums are mostly dead.
This is regarding Problem 14 of Project Euler
I decided to try and compute everything to see if I can brute force out the answer
I made this program:
http://pastebin.com/AMVbhebP
^Looks like the Code block has some sort of maximum character limit, please see the pastebin link.
To try and essentially solve the problem, the problem is when I run it in Eclipse, the program does not finish
Yet if I make k = 100 000, (10 times smaller), the program is able to be completed, and outputs the result of:
Index 77031 Max 350
However the problem relies on k = 1 000 000
Is there some way I can make sure my program goes until completion, I am sure it is merely due to the use of resources?
Also, is there perhaps a way to make my program more efficient? Technical or Mathematical?
Thank you.
I am posting this here because it has something to do with mathematics, but mostly programming, however the programming forums are mostly dead.
This is regarding Problem 14 of Project Euler
I decided to try and compute everything to see if I can brute force out the answer
I made this program:
http://pastebin.com/AMVbhebP
Code:
public class CollatzProblem {
public static void main(String[] args) {
// This is the number the program will go up to, so to find maximum from 1 to 100, we make k = 100
int k = 1000000;
// An array to keep track of the values we get
int[] length = new int[k];
// Loop through all possible starting values, from 1 to 'k'
for(int start = 1; start < k; ++start){
int current = start;
int count = 0;
// This is where iteration happens, we keep iterating until we get a '1'
// While doing so it counts each iteration
while(current != 1){
current = iterate(current); // See next method to see how it iterates
++count;
}
length[start] = count;
}
// This will find the actual maximum value in the array
int max = length[0];
for(int i=0; i< k; ++i){
if(max < length[i]){
max = length[i];
}
}
// This will find the value that belongs to this 'max', this is the value we want
int maxIndex = 0;
for(int i=0; i<k; ++i){
if(max == length[i]){
maxIndex = i;
}
}
System.out.println("Index: " + maxIndex + " Amount: " + max);
}
public static int iterate(int n){
// Do as the function says, if n is even, halve it, if n is odd, 3*n+1 is output
int out = 0;
if(n%2 == 0){
out = n/2;
}
if(n%2 != 0){
out = 3*n+1;
}
return out;
}
}
To try and essentially solve the problem, the problem is when I run it in Eclipse, the program does not finish
Yet if I make k = 100 000, (10 times smaller), the program is able to be completed, and outputs the result of:
Index 77031 Max 350
However the problem relies on k = 1 000 000
Is there some way I can make sure my program goes until completion, I am sure it is merely due to the use of resources?
Also, is there perhaps a way to make my program more efficient? Technical or Mathematical?
Thank you.
Last edited: