Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.80/5 (5 votes)
See more:
This is my code for project euler problem 12. it is meant to find the first triangle number that has over 500 divisors but just runs endlessly. Any help would be appreciated.

Java
public class Problem12 {

	public static void main(String[] args) {
		int [] array = new int [501];
		long trinumber = 0;
		int j;
		
		for(int i = 1;; i ++){
			j = 0;
			for(int k = 1; k<=trinumber;k++){
				
				if (trinumber%k == 0)
				{array[j] = k;
				j++;}
				
			}
			if (array[500] !=0)
				{System.out.println(trinumber);
				return;}
			
			trinumber+=i;
				}
		
		}
		
	
	}
Posted
Updated 27-Jul-15 6:24am
v3
Comments
[no name] 27-Jul-15 11:45am    
http://www.javamex.com/tutorials/java/#.VbZR2NFRGUk

This is homework, so you learn Java. The debugger is your Friend.

So run your code step by step with a debugger, and look at what append.
For testing purpose, choose some small numbers, calculate by hand the number of divisor they should have an run the debugger to see if your program work or not .
 
Share this answer
 
Instead of repeatedly posting these questions, your time would really be better spent studying Java. Go to http://download.oracle.com/javase/tutorial/index.html[^] and work through the tutorials.
 
Share this answer
 
The Project Euler problems are NOT good for teaching yourself a language.

They are challenge problems for testing your bounds of data representation, math and algorithms.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900