My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.google.code.projecteuler;

/**
* A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
*
* a^2 + b^2 = c^2
*
* For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
*
* There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find
* the product abc.
*
* @link <a
* href="http://projecteuler.net/index.php?section=problems&id=9">Problem
* 9</a>
* @author Cesar Arevalo
*/
public class Problem009 {

public static void main(String[] args) {

StopWatch stopWatch = new StopWatch();
stopWatch.start();

int limit = 1000;
int c = limit;
int number = 1;

while (c > 0) {
for (int b = c - 1; b > 0; b--) {
for (int a = b - 1; a > 0; a--) {
if (c + b + a == limit &&
c > b &&
b > a &&
(a * a) + (b * b) == c * c) {
number = c * b * a;
System.out.println("a = " + a + ", b = " + b + ", c = " + c);
c = 0;
}
}

if (c == 0) {
break;
}
}

c--;
}

stopWatch.stop();
System.out.println("Time taken: " + stopWatch.getTime());

System.out.println("number: " + number);
}
}

Change log

r20 by cesar.arevalo1 on Feb 26, 2009   Diff
- Renaming the folder euler to java, so it
is descriptive of what type of code it
contains.
- Renaming the project of each java and
flex type of solutions to euler_java and
euler_flex so it is more descriptive of
the different solutions projects.
Go to: 
Project members, sign in to write a code review

Older revisions

r14 by cesar.arevalo1 on Dec 21, 2008   Diff
Adding a StopWatch for showing how
long the solution takes.
r10 by cesar.arevalo1 on Nov 29, 2008   Diff
Renaming the root package so it
adheres to the place where the project
is hosted.
r2 by cesar.arevalo1 on Oct 5, 2008   Diff
Initial Commit
All revisions of this file

File info

Size: 1182 bytes, 55 lines
Powered by Google Project Hosting