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 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) { |
||
⚫ | |||
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 |
|||
} |
|||
// See: https://oeis.org/A094958 |
|||
⚫ | |||
private static List<Long> getPythagoreanQuadruples(long max) { |
|||
⚫ | |||
List<Long> list = new ArrayList<>(); |
|||
for (int b = a; b <= MAX; b++) a2b2[a2 + b * b] = true; |
|||
long n = -1; |
|||
long m = -1; |
|||
while ( true ) { |
|||
long nTest = (long) Math.pow(2, n+1); |
|||
long mTest = (long) (5L * Math.pow(2, m+1)); |
|||
long test = 0; |
|||
if ( nTest > mTest ) { |
|||
test = mTest; |
|||
m++; |
|||
} |
|||
else { |
|||
test = nTest; |
|||
⚫ | |||
⚫ | |||
if ( test < max ) { |
|||
list.add(test); |
|||
} |
|||
else { |
|||
⚫ | |||
} |
} |
||
} |
} |
||
return list; |
|||
System.out.printf("The values of d <= %d which can't be represented:\n", MAX); |
|||
⚫ | |||
if (!found[d]) System.out.printf("%d ", d); |
|||
⚫ | |||
System.out.println(); |
|||
} |
} |
||
⚫ | |||
} |
|||
</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 |
[1, 2, 4, 5, 8, 10, 16, 20, 32, 40, 64, 80, 128, 160, 256, 320, 512, 640, 1024, 1280, 2048] |
||
</pre> |
</pre> |
||