Skip to content

Commit

Permalink
P75: Added Java solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
nayuki committed Feb 14, 2012
1 parent 01e5248 commit bde6317
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions Answers.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Problem 071: 428570
Problem 072: 303963552391 Problem 072: 303963552391
Problem 073: 7295372 Problem 073: 7295372
Problem 074: 402 Problem 074: 402
Problem 075: 161667
Problem 076: 190569291 Problem 076: 190569291
Problem 077: 71 Problem 077: 71
Problem 079: 73162890 Problem 079: 73162890
Expand Down
80 changes: 80 additions & 0 deletions p075.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Solution to Project Euler problem 75
* By Nayuki Minase
*/

import java.util.HashSet;
import java.util.Set;


public class p075 {

public static void main(String[] args) {
int n = 1500000;

/*
* Pythagorean triples theorem:
* Every primitive Pythagorean triple with a odd and b even can be expressed as
* a = st, b = (s^2-t^2)/2, c = (s^2+t^2)/2, where s > t >= 1 are coprime odd integers.
*/
Set<IntTriple> triples = new HashSet<IntTriple>();
for (int t = 1; t * t <= n; t += 2) {
for (int s = t + 2; s * s <= n; s += 2) {
if (Library.gcd(s, t) == 1) {
int a = s * t;
int b = (s * s - t * t) / 2;
int c = (s * s + t * t) / 2;
int sum = a + b + c;
if (sum <= n)
triples.add(new IntTriple(a, b, c));
}
}
}

byte[] ways = new byte[n + 1];
for (IntTriple triple : triples) {
int sum = triple.a + triple.b + triple.c;
for (int i = 1; i * sum <= n; i++)
ways[i * sum] = (byte)Math.min(ways[i * sum] + 1, 2); // Increment but saturate at 2
}

int count = 0;
for (int x : ways) {
if (x == 1)
count++;
}
System.out.println(count);
}



private static class IntTriple {

public final int a;
public final int b;
public final int c;


public IntTriple(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}


public boolean equals(Object obj) {
if (!(obj instanceof IntTriple))
return false;
else {
IntTriple other = (IntTriple)obj;
return a == other.a && b == other.b && c == other.c;
}
}

public int hashCode() {
return a + b + c;
}

}

}

0 comments on commit bde6317

Please sign in to comment.