Skip to content

Instantly share code, notes, and snippets.

@wchargin
Created October 23, 2016 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wchargin/46db9db0190c805acb6582b0f9c534ae to your computer and use it in GitHub Desktop.
Save wchargin/46db9db0190c805acb6582b0f9c534ae to your computer and use it in GitHub Desktop.
pythagorean triples timer
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
interface PythagoreanTriplesSolver {
List<List<Integer>> pythagoreanTriples(int n);
}
class OriginalSolver implements PythagoreanTriplesSolver {
public List<List<Integer>> pythagoreanTriples(int n) {
List<List<Integer>> result = new ArrayList<>();
int i, j, k;
for (i = 3; i < n; i++) {
for (j = n; j > i; j--) {
for (k = n; k > j; k--) {
if (i * i + j * j == k * k){
ArrayList<Integer> tempArray = new ArrayList<>();
tempArray.add(i);
tempArray.add(j);
tempArray.add(k);
result.add(tempArray);
}
}
}
}
return result;
}
}
class CachedSolver implements PythagoreanTriplesSolver {
public List<List<Integer>> pythagoreanTriples(int n) {
List<List<Integer>> result = new ArrayList<>();
int i, j, k;
for (i = 3; i < n; i++) {
int iSquared = i * i;
for (j = n; j > i; j--) {
int iSquaredPlusJSquared = iSquared + j * j;
for (k = n; k > j; k--) {
if (iSquaredPlusJSquared == k * k){
ArrayList<Integer> tempArray = new ArrayList<>();
tempArray.add(i);
tempArray.add(j);
tempArray.add(k);
result.add(tempArray);
}
}
}
}
return result;
}
}
public class Driver {
private static class Datum {
private long msElapsed;
private List<List<Integer>> result;
}
private static Datum measure(PythagoreanTriplesSolver algorithm, int n) {
final Datum datum = new Datum();
final long msStart = System.currentTimeMillis();
datum.result = algorithm.pythagoreanTriples(n);
final long msEnd = System.currentTimeMillis();
datum.msElapsed = msEnd - msStart;
return datum;
}
public static void main(String[] args) {
final int n = 3000;
final int trials = 16;
PythagoreanTriplesSolver normal = new OriginalSolver();
PythagoreanTriplesSolver cached = new CachedSolver();
long msNormal = 0;
long msCached = 0;
System.out.println("\tRuntimes (ms)");
System.out.println("Trial\tNormal\tCached\tEqual?");
for (int i = 0; i < trials; i++) {
System.out.print(i + "\t");
System.out.flush();
final Datum datumNormal;
final Datum datumCached;
datumNormal = measure(normal, n);
msNormal += datumNormal.msElapsed;
System.out.print(datumNormal.msElapsed + "\t");
System.out.flush();
datumCached = measure(cached, n);
msCached += datumCached.msElapsed;
System.out.print(datumCached.msElapsed + "\t");
System.out.flush();
System.out.println(datumNormal.result.equals(datumCached.result));
}
System.out.println("Normal: " + msNormal + "ms");
System.out.println("Cached: " + msCached + "ms");
}
}
$ java Driver
Runtimes (ms)
Trial Normal Cached Equal?
0 10450 9418 true
1 10459 9391 true
2 2242 2194 true
3 2231 2179 true
4 2230 2185 true
5 2233 2209 true
6 2236 2186 true
7 2236 2174 true
8 2237 2189 true
9 2258 2174 true
10 2239 2196 true
11 2240 2194 true
12 2219 2218 true
13 2245 2194 true
14 2246 2195 true
15 2246 2203 true
Normal: 52247ms
Cached: 49499ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment