Last active
April 7, 2019 07:41
-
-
Save jinahya/40dee9762a15f9059ef512af446796e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.lang.Math.ceil; | |
import static java.lang.Math.floor; | |
import static java.lang.Math.pow; | |
import static java.lang.Math.round; | |
import java.util.*; | |
strictfp class A3b3EqualsC3d3 { | |
static class Pair { | |
static Pair of(final int a, final int b) { | |
final Pair i = new Pair(); | |
i.a = a; | |
i.b = b; | |
return i; | |
} | |
@Override | |
public String toString() { | |
return a + "," + b; | |
} | |
@Override | |
public boolean equals(final Object o) { | |
final Pair p = (Pair) o; | |
return a == p.a && b == p.b; | |
} | |
@Override | |
public int hashCode() { | |
int h = Integer.hashCode(a); | |
h = 31 * h + Integer.hashCode(b); | |
return h; | |
} | |
int pow3() { | |
return (int) pow(a, 3) + (int) pow(b, 3); | |
} | |
int a; | |
int b; | |
} | |
static class Combination { | |
static Combination of(final int a, final int b, final int c, final int d) { | |
final Combination i = new Combination(); | |
i.p1 = Pair.of(a, b); | |
i.p2 = Pair.of(c, d); | |
return i; | |
} | |
@Override | |
public String toString() { | |
return p1.toString() + "," + p2.toString(); | |
} | |
@Override | |
public boolean equals(final Object o) { | |
final Combination c = (Combination) o; | |
return Objects.equals(p1, c.p1) && Objects.equals(p2, c.p2); | |
} | |
@Override | |
public int hashCode() { | |
int h = Objects.hashCode(p1); | |
h = 31 * h + Objects.hashCode(p2); | |
return h; | |
} | |
boolean valid() { | |
return p1.pow3() == p2.pow3(); | |
} | |
Pair p1; | |
Pair p2; | |
} | |
static final List<Combination> l1 = new ArrayList<>(); | |
static final List<Combination> l2 = new ArrayList<>(); | |
static final List<Combination> l3 = new ArrayList<>(); | |
static void f1(final int N) { | |
long steps = 0L; | |
long count = 0L; | |
for (int a = 1; a <= N; a++) { | |
final int a3 = (int) pow(a, 3); | |
for (int b = 1; b <= N; b++) { | |
final int b3 = (int) pow(b, 3); | |
for (int c = 1; c <= N; c++) { | |
final int c3 = (int) pow(c, 3); | |
for (int d = 1; d <= N; d++) { | |
steps++; | |
final int d3 = (int) pow(d, 3); | |
if (a3 + b3 == c3 + d3) { | |
count++; | |
l1.add(Combination.of(a, b, c, d)); | |
} | |
} | |
} | |
} | |
} | |
System.out.printf("f2: %1$d\t%2$d\n", count, steps); | |
} | |
static void f2(final int N) { | |
long steps = 0L; | |
long count = 0L; | |
for (int a = 1; a <= N; a++) { | |
final int a3 = (int) pow(a, 3); | |
for (int b = 1; b <= N; b++) { | |
final int b3 = (int) pow(b, 3); | |
for (int c = 1; c <= N; c++) { | |
steps++; | |
final int c3 = (int) pow(c, 3); | |
final int d = (int) ceil(pow(a3 + b3 - c3, 1 / 3.0d)); | |
if (d > N) { | |
continue; | |
} | |
if (a3 + b3 == c3 + (int) pow(d, 3)) { | |
count++; | |
l2.add(Combination.of(a, b, c, d)); | |
} | |
} | |
} | |
} | |
System.out.printf("f2: %1$d\t%2$d\n", count, steps); | |
} | |
static void f3(final int N) { | |
long steps = 0L; | |
long count = 0L; | |
final Map<Integer, List<Pair>> map = new HashMap<>(); | |
for (int c = 1; c <= N; c++) { | |
for (int d = 1; d <= N; d++) { | |
final int key = (int) pow(c, 3) + (int) pow(d, 3); | |
List<Pair> pairs = map.get(key); | |
if (pairs == null) { | |
pairs = new ArrayList<>(); | |
map.put(key, pairs); | |
} | |
pairs.add(Pair.of(c, d)); | |
} | |
} | |
for (int a = 1; a <= N; a++) { | |
for (int b = 1; b <= N; b++) { | |
steps++; | |
final int key = (int) pow(a, 3) + (int) pow(b, 3); | |
for (final Pair pair : map.get(key)) { | |
count++; | |
l3.add(Combination.of(a, b, pair.a, pair.b)); | |
} | |
} | |
} | |
System.out.printf("f2: %1$d\t%2$d\n", count, steps); | |
} | |
public static void main(final String... args) { | |
if (args.length == 4) { | |
Arrays.stream(args) | |
.map(Integer::parseInt) | |
.mapToDouble(i -> pow(i, 3)) | |
.forEach(System.out::println); | |
final int a = Integer.parseInt(args[0]); | |
final int b = Integer.parseInt(args[1]); | |
final int c = Integer.parseInt(args[2]); | |
final int d = Integer.parseInt(args[3]); | |
System.out.println((pow(a, 3) + pow(b, 3)) + "," + (pow(c, 3) + pow(d, 3))); | |
return; | |
} | |
final int N = Integer.parseInt(args[0]); | |
f1(N); | |
f2(N); | |
f3(N); | |
System.out.printf("l1.size: %d\t%d\n", l1.size(), new HashSet<>(l1).size()); | |
System.out.printf("l2.size: %d\t%d\n", l2.size(), new HashSet<>(l2).size()); | |
System.out.printf("l3.size: %d\t%d\n", l3.size(), new HashSet<>(l3).size()); | |
l1.stream().filter(c -> !c.valid()).forEach(c -> System.out.println("l1 / not valid: " + c)); | |
l2.stream().filter(c -> !c.valid()).forEach(c -> System.out.println("l2 / not valid: " + c)); | |
l3.stream().filter(c -> !c.valid()).forEach(c -> System.out.println("l3 / not valid: " + c)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment