Programming Limitations and Project Euler (1 Viewer)

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

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;
	}

}
^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.
 
Last edited:

Squar3root

realest nigga
Joined
Jun 10, 2012
Messages
4,994
Location
phenchod
Gender
Male
HSC
1998
Uni Grad
2005
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?
use an infinite loop. I know in C, it is done by:

for (;;) {
//some code here executes infinity times
}
OR you could encase your entire program inside a while (return != 0 || return > 0 /*assuming positive returns only*/) loop iff it keeps returning different numbers

I don't know the java equivalent :/

NOTE your program will never terminate though.
 
Last edited:

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,284
Gender
Male
HSC
2011
I suspect int isnt big enough to handle it, I quickly changed it to use double - had to cast back to int for use in the indexers but it still seemed to work that way.

http://pastebin.com/6VggsDvS
 

Sy123

This too shall pass
Joined
Nov 6, 2011
Messages
3,730
Gender
Male
HSC
2013
I suspect int isnt big enough to handle it, I quickly changed it to use double - had to cast back to int for use in the indexers but it still seemed to work that way.

http://pastebin.com/6VggsDvS
The 'count' for the number of iterations needed from 1 to 100 000 is only 350, for 1 000 000 I don't think it would go over 2^31
Likewise for the number being iterated

Do you have suggestions on how to put more resources into running the program?
 

Squar3root

realest nigga
Joined
Jun 10, 2012
Messages
4,994
Location
phenchod
Gender
Male
HSC
1998
Uni Grad
2005
i could try to write a solution in C but i don't understand the problem :/
 
Last edited:

Squar3root

realest nigga
Joined
Jun 10, 2012
Messages
4,994
Location
phenchod
Gender
Male
HSC
1998
Uni Grad
2005
what i have so far:


//not sure what i am doing lel
 
Last edited:

SpiralFlex

Well-Known Member
Joined
Dec 18, 2010
Messages
6,960
Gender
Female
HSC
N/A
as I said before:
Think of a number being assigned to a sequence . So the number will be assigned to the sequence, according to the rule above . The sequence has length . We wish to find what number under 1 million will produce the longest sequence.

Basically what Sy's program does is whenever our current value is not at 1, (keep on generating more numbers depending on the rule). Count the numbers in the sequence produce and store that in an array. Then search through back in the array once we are done searching the 999999 numbers and see what index the number belongs to.
 
Last edited:

Squar3root

realest nigga
Joined
Jun 10, 2012
Messages
4,994
Location
phenchod
Gender
Male
HSC
1998
Uni Grad
2005
Think of a number being assigned to a sequence . So the number will be assigned to the sequence, according to the rule above . The sequence has length . We wish to find what number under 1 million will produce the longest sequence.
you just rephrased the question lel
 

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,284
Gender
Male
HSC
2011
Also with these kind of questions there are always mathematical tricks that you can use to reduce the amount of checks you need to do and so you can do them recursively - GCD comes to mind. My maths is pretty weak and i've never studied number theory so cant help you there.

The problem is the fix I gave you relies on there never being more elements than int max value, a value in the chain can be greater but the integer data structure wont support more elements than int max value. I just did a find and replace all, so some of those doubles should probably just be ints to avoid the cast haha.
 
Last edited:

Squar3root

realest nigga
Joined
Jun 10, 2012
Messages
4,994
Location
phenchod
Gender
Male
HSC
1998
Uni Grad
2005
i know this is off topic (i will delete it later) but is there a way to have post on thread appear instantly rather than keep refreshing the page?
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top