Projecteuler.net – Problem 45 – Java

http://projecteuler.net/problem=45

I just solved each equation for the variable I was trying to find. Then, if the ‘solution modulo 1′ equals 0, I know I have a whole number. Since we know the first Triangle number that works is 285, we will start out search from there until the next number is found.

Josh.

public class Euler45 {

	public static void main(String[] args) {

		double t = 285;		// start at the first number that is know to work
		double p = 0.5;		// set an initial value that will fail the first while test
		double h = 0.5;		// set an initial value that will fail the first while test
		
		while ((p%1 != 0) || (h%1 != 0)) {
			t++;
			p = (Math.sqrt((12 * Math.pow(t, 2)) + (12 * t) + 1) + 1) / 6;
			h = (t + 1) / 2;			
		}
		
		long x = (long) (t*(t+1))/2;
		System.out.println(x);
		
	}

}
This entry was posted in Java, Project Euler. Bookmark the permalink.

Leave a Reply