Pythagorean quadruples: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: added a <lang> xxx </lang> HTML tags, also <pre> and </pre> HTML tags (the old code "swallowed up" Rust's code.)
Line 757: Line 757:


=={{header|Java}}==
=={{header|Java}}==
Replace translation with Java coded implementation.
{{trans|Kotlin}}
<lang java>public class PythagoreanQuadruple {


Compute sequence directly.
static final int MAX = 2200;
<lang java>
static final int MAX2 = MAX * MAX * 2;
import java.util.ArrayList;
import java.util.List;

public class PythagoreanQuadruples {


public static void main(String[] args) {
public static void main(String[] args) {
long d = 2200;
boolean[] found = new boolean[MAX + 1]; // all false by default
System.out.printf("Values of d < %d where a, b, and c are non-zero and a^2 + b^2 + c^2 = d^2 has no solutions:%n%s%n", d, getPythagoreanQuadruples(d));
boolean[] a2b2 = new boolean[MAX2 + 1]; // ditto
int s = 3;
}


// See: https://oeis.org/A094958
for (int a = 1; a <= MAX; a++) {
private static List<Long> getPythagoreanQuadruples(long max) {
int a2 = a * a;
List<Long> list = new ArrayList<>();
for (int b = a; b <= MAX; b++) a2b2[a2 + b * b] = true;
}
long n = -1;
long m = -1;

for (int c = 1; c <= MAX; c++) {
while ( true ) {
int s1 = s;
long nTest = (long) Math.pow(2, n+1);
s += 2;
long mTest = (long) (5L * Math.pow(2, m+1));
int s2 = s;
long test = 0;
for (int d = c + 1; d <= MAX; d++) {
if ( nTest > mTest ) {
if (a2b2[s1]) found[d] = true;
test = mTest;
s1 += s2;
m++;
s2 += 2;
}
else {
test = nTest;
n++;
}
if ( test < max ) {
list.add(test);
}
else {
break;
}
}
}
}
return list;

System.out.printf("The values of d <= %d which can't be represented:\n", MAX);
for (int d = 1; d <= MAX; d++) {
if (!found[d]) System.out.printf("%d ", d);
}
System.out.println();
}
}

}</lang>
}
</lang>


{{out}}
{{out}}
<pre>
<pre>
Values of d < 2200 where a, b, and c are non-zero and a^2 + b^2 + c^2 = d^2 has no solutions:
The values of d <= 2200 which can't be represented:
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
[1, 2, 4, 5, 8, 10, 16, 20, 32, 40, 64, 80, 128, 160, 256, 320, 512, 640, 1024, 1280, 2048]
</pre>
</pre>