Pythagorean quadruples: Difference between revisions

Added solution for EDSAC
(rust)
(Added solution for EDSAC)
 
(38 intermediate revisions by 22 users not shown)
Line 21:
For positive integers up   '''2,200'''   (inclusive),   for all values of   '''a''',  
'''b''',   '''c''',   and   '''d''',
<br>find &nbsp; (and show here) &nbsp; those values of &nbsp; '''d''' &nbsp; that &nbsp; ''<u>can't</u>'' &nbsp; be represented.
 
Show the values of &nbsp; '''d''' &nbsp; on one line of output &nbsp; (optionally with a title).
Line 29:
* &nbsp; [[Euler's sum of powers conjecture]].
* &nbsp; [[Pythagorean triples]].
 
 
;Reference:
:* &nbsp; the Wikipedia article: &nbsp; [https://en.wikipedia.org/wiki/Pythagorean_quadruple Pythagorean quadruple].
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F quad(top = 2200)
V r = [0B] * top
V ab = [0B] * (top * 2)^2
L(a) 1 .< top
L(b) a .< top
ab[a * a + b * b] = 1B
V s = 3
L(c) 1 .< top
(V s1, s, V s2) = (s, s + 2, s + 2)
L(d) c + 1 .< top
I ab[s1]
r[d] = 1B
s1 += s2
s2 += 2
R enumerate(r).filter((i, val) -> !val & i).map((i, val) -> i)
 
print(quad())</syntaxhighlight>
 
{{out}}
<pre>
[1, 2, 4, 5, 8, 10, 16, 20, 32, 40, 64, 80, 128, 160, 256, 320, 512, 640, 1024, 1280, 2048]
</pre>
 
=={{header|ALGOL 68}}==
As with the optimised REXX solution, we find the values of d for which there are no a^2 + b^2 = d^2 - c^2.
<langsyntaxhighlight lang="algol68">BEGIN
# find values of d where d^2 =/= a^2 + b^2 + c^2 for any integers a, b, c #
# where d in [1..2200], a, b, c =/= 0 #
Line 66 ⟶ 96:
OD;
print( ( newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
</pre>
 
=={{header|Amazing Hopper}}==
{{trans|C}}
The process runs in 2.9 secs. on an Intel Core 2 Duo at 2.53 or 2.66 GHz. It's SLOW, but believe me, when it comes to Hopper, it's quite a feat! :D
<syntaxhighlight lang="text">
#include <flow.h>
 
DEF-MAIN(argv, argc)
SET(N, 2200)
DIM( MUL(MUL(N,N),2) ) AS-ZEROS( temp )
DIM( N ) AS-ZEROS( found )
MSET( a,T1,T2 )
TIC(T1)
SEQ-SPC(1,N,N,a), LET( a := MUL(a,a) )
 
SET(i,1), SET(r,0)
PERF-UP(i,N,1)
LET( r := ADD( [i] GET( a ), [i:end] CGET(a) ) )
SET-RANGE( r ), SET(temp, 1), CLR-RANGE
NEXT
 
SET(c,1), SET(s,3), MSET(s1,s2,d)
PERF-UP(c, N, 1)
LET( s1 := s )
s += 2
LET( s2 := s )
LET( d := ADD(c,1) )
PERF-UP(d, N, 1)
COND ( [s1] GET(temp) )
[d] {1} PUT(found)
CEND
s1 += s2
s2 += 2
NEXT
NEXT
TOC(T1, T2), PRNL("Time = ", T2 )
PRN( "Imprimiendo resultados:\n" )
CART( IS-ZERO?( found ) ) MOVE-TO( r )
PRNL( r )
 
MCLEAR(temp, found, a, r)
END
</syntaxhighlight>
{{out}}
<pre>
Time = 2.88824
Imprimiendo resultados:
1,2,4,5,8,10,16,20,32,40,64,80,128,160,256,320,512,640,1024,1280,2048
</pre>
 
=={{header|AppleScript}}==
<langsyntaxhighlight AppleScriptlang="applescript">-- double :: Num -> Num
on double(x)
x + x
Line 272 ⟶ 352:
end if
end if
end uncons</langsyntaxhighlight>
{{Out}}
<pre>{1, 2, 4, 5, 8, 10, 16, 20, 32, 40, 64, 80, 128, 160, 256, 320, 512, 640, 1024, 1280, 2048}</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PYTHAGOREAN_QUADRUPLES.AWK
# converted from Go
BEGIN {
n = 2200
s = 3
for (a=1; a<=n; a++) {
a2 = a * a
for (b=a; b<=n; b++) {
ab[a2 + b * b] = 1
}
}
for (c=1; c<=n; c++) {
s1 = s
s += 2
s2 = s
for (d=c+1; d<=n; d++) {
if (ab[s1]) {
r[d] = 1
}
s1 += s2
s2 += 2
}
}
for (d=1; d<=n; d++) {
if (!r[d]) {
printf("%d ",d)
}
}
printf("\n")
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
</pre>
 
=={{header|C}}==
===Version 1===
Five seconds on my Intel Linux box.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <math.h>
#include <string.h>
Line 305 ⟶ 423:
if(!r[a]) printf("%d ",a); // print non solution
printf("\n");
}</langsyntaxhighlight>
{{out}}
<pre>
Line 314 ⟶ 432:
===Version 2 (much faster)===
Translation of second version of FreeBASIC entry. Runs in about 0.15 seconds.
<langsyntaxhighlight Clang="c">#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Line 349 ⟶ 467:
free(ab);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Same as first version.
</pre>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<syntaxhighlight lang="csharp">using System;
 
namespace PythagoreanQuadruples {
class Program {
const int MAX = 2200;
const int MAX2 = MAX * MAX * 2;
 
static void Main(string[] args) {
bool[] found = new bool[MAX + 1]; // all false by default
bool[] a2b2 = new bool[MAX2 + 1]; // ditto
int s = 3;
 
for(int a = 1; a <= MAX; a++) {
int a2 = a * a;
for (int b=a; b<=MAX; b++) {
a2b2[a2 + b * b] = true;
}
}
 
for (int c = 1; c <= MAX; c++) {
int s1 = s;
s += 2;
int s2 = s;
for (int d = c + 1; d <= MAX; d++) {
if (a2b2[s1]) found[d] = true;
s1 += s2;
s2 += 2;
}
}
 
Console.WriteLine("The values of d <= {0} which can't be represented:", MAX);
for (int d = 1; d < MAX; d++) {
if (!found[d]) Console.Write("{0} ", d);
}
Console.WriteLine();
}
}
}</syntaxhighlight>
{{out}}
<pre>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</pre>
 
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 400 ⟶ 562:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>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</pre>
 
=={{header|C#|C sharp}}==
{{trans|Java}}
<lang csharp>using System;
 
namespace PythagoreanQuadruples {
class Program {
const int MAX = 2200;
const int MAX2 = MAX * MAX * 2;
 
static void Main(string[] args) {
bool[] found = new bool[MAX + 1]; // all false by default
bool[] a2b2 = new bool[MAX2 + 1]; // ditto
int s = 3;
 
for(int a = 1; a <= MAX; a++) {
int a2 = a * a;
for (int b=a; b<=MAX; b++) {
a2b2[a2 + b * b] = true;
}
}
 
for (int c = 1; c <= MAX; c++) {
int s1 = s;
s += 2;
int s2 = s;
for (int d = c + 1; d <= MAX; d++) {
if (a2b2[s1]) found[d] = true;
s1 += s2;
s2 += 2;
}
}
 
Console.WriteLine("The values of d <= {0} which can't be represented:", MAX);
for (int d = 1; d < MAX; d++) {
if (!found[d]) Console.Write("{0} ", d);
}
Console.WriteLine();
}
}
}</lang>
{{out}}
<pre>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</pre>
 
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight Rubylang="ruby">n = 2200
l_add, l = Hash(Int32, Bool).new(false), Hash(Int32, Bool).new(false)
(1..n).each do |x|
Line 470 ⟶ 588:
end
 
puts (1..n).reject{ |x| l[x] }.join(" ")</langsyntaxhighlight>
{{out}}
<pre>1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
</pre>
Translation of faster Ruby version using Enumerators.
<syntaxhighlight lang="ruby">squares = (0..).each.map { |n| 2_u64**n }
squares5 = (0..).each.map { |n| 2_u64**n * 5 }
 
n = squares.next.as(Int)
m = squares5.next.as(Int)
 
pyth_quad = Iterator.of do
if n < m
value = n
n = squares.next.as(Int)
else
value = m
m = squares5.next.as(Int)
end
value
end
 
puts pyth_quad.take_while { |n| n <= 1000000000 }.join(" ")</syntaxhighlight>
{{Out}}
<pre>
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048 2560 4096 5120 8192 10240 16384 20480 32768 40960 65536 81920 131072 163840 262144 327680 524288 655360 1048576 1310720 2097152 2621440 4194304 5242880 8388608 10485760 16777216 20971520 33554432 41943040 67108864 83886080 134217728 167772160 268435456 335544320 536870912 671088640
</pre>
 
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight Dlang="d">import std.bitmanip : BitArray;
import std.stdio;
 
Line 519 ⟶ 660:
}
writeln;
}</langsyntaxhighlight>
 
{{out}}
<pre>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</pre>
 
=={{header|EDSAC order code}}==
A solution from first principles would probably take a long time on EDSAC, so we use the theoretical results [https://mathoverflow.net/questions/90914/sums-of-three-non-zero-squares quoted in MathOverflow]. From these it follows easily that if d is a power of 2, or 5 times a power of 2, then d^2 is not the sum of three non-zero squares. The converse does not follow, but if d is a counterexample then d^2 exceeds 5*(10^10), and therefore d exceeds the limit in the task description. The EDSAC output thus consists of two interleaved arrays, as in the AppleScript solution.
<syntaxhighlight lang="edsac">
[Pythagorean quadruples - Rosetta Code
EDSAC program, Initial Orders 2]
 
[Arrange the storage]
T46K P56F [N parameter: modified library s/r P7 to print integer]
T47K P106F [M parameter: main routine]
 
[Library subroutine M3, prints header at load time.
Here, header leaves teleprinter in figures mode.]
PFGKIFAFRDLFUFOFE@A6FG@E8FEZPF
*NUMBERS!WHOSE!SQUARES!ARE!NOT!THE!SUM!
OF!THREE!NONZERO!SQUARES@&MAXIMUM#!V!
..PK [after header, blank tape and PK (WWG, 1951, page 91)]
 
[------------------------------------------------------------------------------]
[Main routine]
E25K TM GK [load at address specified by M parameter]
[Constants]
[0] P1100F [limit, right-justified, e.g. P1100F for 2200]
[1] !F [teleprinter space]
[2] @F [carriage return]
[3] &F [line feed]
[4] K4096F [teleprinter null]
[5] PD [17-bit constant 1]
[6] P2D [17-bit constant 5]
[Variables]
[7] PF [2^m, where m = 0, 1, 2, ...]
[8] PF [5*2^n, where n = 0, 1, 2, ...]
[Enter here, with acc = 0]
[Complete header by printing limit]
[9] A4@ T1F [print leading zeros as nulls]
A@ TF [pass limit to print subroutine in 0F]
[13] A13@ GN [call print subroutine; leaves acc clear]
O2@ O3@ [print new line]
[Initialize variables]
A5@ T7@ [2^m := 1]
A6@ T8@ [5*2^n := 5]
[Loop back to here after printing number]
[Print 2^m or 5*2^n, whichever is smaller]
[21] A7@ S8@ [compare values]
E28@ [jump if 5*2^n is smaller]
A8@ [else restore 2^m in acc]
LD U7@ [double value in memory]
E32@ [jump to common code]
[28] T4F [clear acc]
A8@ [acc := 5*2^n]
LD U8@ [double value in memory]
[32] RD [common code: undo doubling in acc]
TF [pass number to print subroutine in 0F]
A@ SF [test for number > limit]
G42@ [jump to exit if so]
O1@ [print space before number]
T4F [clear acc]
[39] A39@ GN [call print subroutine; leaves acc clear]
E21@ [loop back for next number]
[Here when done]
[42] O2@ O3@ [print new line]
O4@ [print null to flush teleprinter buffer]
ZF [halt the machine]
 
[------------------------------------------------------------]
[Subroutine to print 17-bit non-negative integer
Parameters: 0F = integer to be printed (not preserved)
1F = character for leading zero
(preserved; typically null, space or zero)
Workspace: 4F, 5F
Even address; 39 locations]
E25K TN [load at address specified by N parameter]
GKA3FT34@A1FT35@S37@T36@T4DAFT4FH38@V4FRDA4D
R1024FH30@E23@O35@A2FT36@T5FV4DYFL8FT4DA5F
L1024FUFA36@G16@OFTFT35@A36@G17@ZFPFPFP4FZ219D
 
E25K TM GK [M parameter again]
E9Z [define entry point]
PF [acc = 0 on entry]
</syntaxhighlight>
{{out}}
<pre>
NUMBERS WHOSE SQUARES ARE NOT THE SUM OF THREE NONZERO SQUARES
MAXIMUM = 2200
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
 
</pre>
 
=={{header|FreeBASIC}}==
===From the Wikipedia page===
[https://en.wikipedia.org/wiki/Pythagorean_quadruple Alternate parametrization, second version both A and B even.]Time just less then 0.7 second on a AMD Athlon II X4 645 3.34GHz win7 64bit. Program uses one core. When the limit is set to 576 (abs. minimum for 2200), the time is about 0.85 sec.
<langsyntaxhighlight lang="freebasic">' version 12-08-2017
' compile with: fbc -s console
 
Line 579 ⟶ 807:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048</pre>
===Brute force===
Based on the second REXX version: A^2 + B^2 = D^2 - C^2. Faster then the first version, about 0.2 second
<langsyntaxhighlight lang="freebasic">' version 14-08-2017
' compile with: fbc -s console
 
Line 619 ⟶ 847:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_limit = 2200
 
long x, x2, y, s = 3, s1, s2, counter
long l( _limit )
long ladd( _limit * _limit * 2 )
 
for x = 1 to _limit
x2 = x * x
for y = x to _limit
ladd( x2 + y * y ) = 1
next
next
 
for x = 1 to _limit
s1 = s
s = s + 2
s2 = s
for y = x + 1 to _limit
if ladd(s1) == 1 then l(y) = 1
s1 = s1 + s2
s2 = s2 + 2
next
next
 
counter = 1
for x = 1 to _limit
if ( l(x) == 0 )
if ( counter mod 7 == 0 )
printf @"%6ld", x : counter == 1 : continue
else
printf @"%6ld\b", x
counter++
end if
end if
next
print
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
1 2 4 5 8 10 16
20 32 40 64 80 128 160
256 320 512 640 1024 1280 2048
</pre>
=={{header|Go}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 666 ⟶ 944:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 674 ⟶ 952:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">powersOfTwo :: [Int]
powersOfTwo = iterate (2 *) 1
 
Line 688 ⟶ 966:
main = do
putStrLn "The values of d <= 2200 which can't be represented."
print $ takeWhile (<= 2200) unrepresentable</langsyntaxhighlight>
{{out}}
<pre>The values of d <= 2200 which can't be represented.
Line 695 ⟶ 973:
=={{header|J}}==
Approach: generate the set of all triple sums of squares, then select the legs for which there aren't any squared "d"s. The solution is straightforward interactive play.
<syntaxhighlight lang="j">
<lang j>
 
Filter =: (#~`)(`:6)
Line 713 ⟶ 991:
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
 
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
Replace translation with Java coded implementation.
{{trans|Kotlin}}
<lang java>public class PythagoreanQuadruple {
 
Compute sequence directly.
static final int MAX = 2200;
<syntaxhighlight 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) {
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;
 
forwhile (int ctrue = 1; c <= MAX; c++) {
intlong s1nTest = s(long) Math.pow(2, n+1);
slong mTest += (long) (5L * Math.pow(2, m+1));
intlong s2test = s0;
forif (int dnTest > =mTest c + 1; d <= MAX; d++) {
if (a2b2[s1]) found[d]test = truemTest;
s1 m++= s2;
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>
}
</syntaxhighlight>
 
{{out}}
<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 ]
</pre>
 
=={{header|JavaScript}}==
{{Trans|Haskell}}
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 903 ⟶ 1,190:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>[1,2,4,5,8,10,16,20,32,40,64,80,128,160,256,320,512,640,1024,1280,2048]</pre>
Line 913 ⟶ 1,200:
be accomplished in jq without `foreach`. Notice also how
`first/1` is used in `is_pythagorean_quad/0` to avoid unnecessary computation.
<langsyntaxhighlight lang="jq"># Emit a proof that the input is a pythagorean quad, or else false
def is_pythagorean_quad:
. as $d
Line 928 ⟶ 1,215:
# The specific task:
 
[range(1; 2201) | select( is_pythagorean_quad | not )] | join(" ")</langsyntaxhighlight>
 
'''Invocation and Output'''
Line 936 ⟶ 1,223:
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|C}}
 
<langsyntaxhighlight lang="julia">function quadruples(N::Int=2200)
r = falses(N)
ab = falses(2N ^ 2)
Line 957 ⟶ 1,243:
end
 
return findfindall(.!, r)
end
 
println("Pythagorean quadruples up to 2200: ", join(quadruples(), ", "))</langsyntaxhighlight>
 
{{out}}
Line 968 ⟶ 1,254:
===Version 1 ===
This uses a similar approach to the REXX optimized version. It also takes advantage of a hint in the C entry that there is no solution if both a and b are odd (confirmed by Wikipedia article). Runs in about 7 seconds on my modest laptop which is more than 4 times faster than the brute force version would have been:
<langsyntaxhighlight lang="scala">// version 1.1.3
 
const val MAX = 2200
Line 1,002 ⟶ 1,288:
for (i in 1..MAX) if (!found[i]) print("$i ")
println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,013 ⟶ 1,299:
 
One thing I've noticed about the resulting sequence is that it appears to be an interleaving of the two series 2 ^ n and 5 * (2 ^ n) for n >= 0 though whether it's possible to prove this mathematically I don't know.
<langsyntaxhighlight lang="scala">// version 1.1.3
 
const val MAX = 2200
Line 1,042 ⟶ 1,328:
for (d in 1..MAX) if (!found[d]) print("$d ")
println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,050 ⟶ 1,336:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- initialize
local N = 2200
local ar = {}
Line 1,080 ⟶ 1,366:
end
end
print()</langsyntaxhighlight>
{{out}}
<pre>1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">max = 2200;
maxsq = max^2;
d = Range[max]^2;
Dynamic[{a, b, Length[d]}]
Do[
Do[
c = Range[1, Floor[(maxsq - a^2 - b^2)^(1/2)]];
dposs = a^2 + b^2 + c^2;
d = Complement[d, dposs]
,
{b, Floor[(maxsq - a^2)^(1/2)]}
]
,
{a, Floor[maxsq^(1/2)]}
]
Sqrt[d]</syntaxhighlight>
{{out}}
<pre>{1, 2, 4, 5, 8, 10, 16, 20, 32, 40, 64, 80, 128, 160, 256, 320, 512, 640, 1024, 1280, 2048}</pre>
 
=={{header|Modula-2}}==
{{trans|C}}
<langsyntaxhighlight lang="modula2">MODULE PythagoreanQuadruples;
FROM FormatString IMPORT FormatString;
FROM RealMath IMPORT sqrt;
Line 1,139 ⟶ 1,445:
 
ReadChar
END PythagoreanQuadruples.</langsyntaxhighlight>
{{out}}
<pre>1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048 </pre>
Line 1,147 ⟶ 1,453:
 
===Version 1===
<syntaxhighlight lang="text">import math
import sequtils
 
const N = 2_200
 
template isOdd(n: int): bool = (n and 1) != 0
var
 
d, b = 0
var r = newSeq[bool](N + 1)
 
for a in 1..N:
for b in a..N:
if a.isOdd and b.isOdd: continue
var aabb = 0
iflet aabb = (a and* 1).boola and+ (b and* 1).bool: continueb
aabb = a * a + b * b
for c in b..N:
varlet aabbcc = 0aabb + c * c
aabbcc = aabb + c * c
d = sqrt(aabbcc.float).int
if aabbcc == d * d and d <= N: r[d] = true
 
for i in 1..N:
if not r[I]: stdout.write i, " "</lang>
echo()</syntaxhighlight>
 
{{out}}
Line 1,173 ⟶ 1,478:
 
===Version 2===
<syntaxhighlight lang="text">const N = 2_200
<lang>import sequtils
 
const N = 2_200
const N2 = N * N * 2
 
var r = newSeq[bool](N + 1)
var
a2, d, s1, s2 = 0
s = 3
r = newSeq[bool](N + 1)
ab = newSeq[bool](N2 + 1)
var ab = newSeq[bool](N2 + 1)
for a in 1..N:
let a2 = a * a
for b in a..N:
ab[a2 + b * b] = true
 
var s = 3
for c in 1..N:
var s1 = s
s += 2
var s2 = s
for d in (c+1)..N:
if ab[s1]: r[d] = true
s1 += s2
Line 1,199 ⟶ 1,500:
 
for d in 1..N:
if not r[d]: stdout.write d, " "</langsyntaxhighlight>
 
{{out}}
<pre>1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048 </pre>
<pre>
 
Same as Version 1.
</pre>
=={{header|Pascal}}==
{{works with|Free Pascal}} compiled with fpc 3.2.0 ( 2019.01.10 ) -O4 -Xs
Line 1,210 ⟶ 1,510:
Brute froce, but not as brute as [http://rosettacode.org/mw/index.php?title=Pythagorean_quadruples#Ring Ring].Did it ever run?<BR>
Stopping search if limit is reached<BR>
<langsyntaxhighlight lang="pascal">program pythQuad;
//find phythagorean Quadrupel up to a,b,c,d <= 2200
//a^2 + b^2 +c^2 = d^2
Line 1,279 ⟶ 1,579:
writeln(CheckCnt,' checks were done');
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,289 ⟶ 1,589:
Using a variant of [http://rosettacode.org/wiki/Pythagorean_quadruples#optimized REXX optimized] optimized<BR>
As I now see the same as [http://rosettacode.org/wiki/Pythagorean_quadruples#ALGOL_68 Algol68]<BR>
a^2 + b^2 is like moving/jumping a rake with tines at a^2 from tine(1) to tine(MaxFactor) and mark their positions<BR>
Quite fast.
<langsyntaxhighlight lang="pascal">program pythQuad_2;
//find phythagorean Quadrupel up to a,b,c,d <= 2200
//a^2 + b^2 +c^2 = d^2
//a^2 + b^2 = d^2-c^2
{$IFDEF FPC}
 
{$R+,O+} //debug purposes, not slower
{$OPTIMIZATION ON,ALL}
{$CODEALIGN proc=16}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
sysutils;
const
MaxFactor = 2200;//22000;//40960;
limit = MaxFactor*MaxFactor;
type
tIdx = NativeUint;
tSum = NativeUint;
var
// global variables are initiated with 0 at startUp
sumA2B2 :array[0..limit] of byte;
check : array[0..MaxFactor] of byte;
 
procedure BuildSumA2B2;
var
a,b ,a2,Uplmt: tIdx;
s : tSum;
begin
//Uplimt = a*a+b*b < Maxfactor | max(a,b) = Uplmt
For a := 1 to MaxFactor do
Uplmt := Trunc(MaxFactor*sqrt(0.5));
For b := 1 to a do
For a := 1 to Uplmt do
Begin
Begin
s := a*a+b*b;
a2:= a*a;
if s < limit then
For sumA2B2[s]b := a downto 1 do
elsesumA2B2[b*b+a2] := 1
breakend;
end;
end;
 
procedure CheckDifD2C2;
var
d,d2,c : tIdx;
s : tSum;
begin
For d := 1 to MaxFactor do
Begin
//c < d => (d*d-c*c) > 0
//c < d => (d*d-c*c) > 0
d2 := d*d;
For c := d-1 downto 1 do
Begin
s :=// d*d-c*c; == (d+c)*(d-c) nonsense
if sumA2B2[sd2-c*c] <> 0 then
Begin
Check[d] := 1;
//first for d found is enough
BREAK;
end;
end;
end;
end;
 
Line 1,342 ⟶ 1,655:
BuildSumA2B2;
CheckDifD2C2;
//FindHoles;
For i := 1 to MaxFactor do
If Check[i] = 0 then
write(i,' ');
writeln;
end.</lang>
</syntaxhighlight>
{{Out}}
<pre>
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
real 0m0.018s,002s //4.8( MbRyzen -> Level2200G 3.7 cache 16 Mb ( Ryzen 5 1600Ghz )
MaxFactor = 22000
 
.. 2048 2560 4096 5120 8192 10240 16384 20480
//MaxFactor =22000;484 Mb -> no level X Cache
real 0m0,746s
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048 2560 4096 5120 8192 10240 16384 20480
MaxFactor = 40960
real 0m4.184s
.. 2048 2560 4096 5120 8192 10240 16384 20480 32768 40960
real 0m3,222s
</pre>
 
=={{header|Perl}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="perl">my $N = 2200;
push @sq, $_**2 for 0 .. $N;
my @not = (0) x $N;
Line 1,388 ⟶ 1,704:
$result .= "$_ " unless $not[$_]
}
print "$result\n"</langsyntaxhighlight>
{{out}}
<pre>1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2018.09}}
 
<lang perl6>my \N = 2200;
my @sq = (0 .. N)»²;
my @not = False xx N;
@not[0] = True;
 
(1 .. N).race.map: -> $d {
my $last = 0;
for $d ... ($d/3).ceiling -> $a {
for 1 .. ($a/2).ceiling -> $b {
last if (my $ab = @sq[$a] + @sq[$b]) > @sq[$d];
if (@sq[$d] - $ab).sqrt.narrow ~~ Int {
@not[$d] = True;
$last = 1;
last
}
}
last if $last;
}
}
 
say @not.grep( *.not, :k );</lang>
{{out}}
<pre>(1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048)</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant N = 2200,
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
N2 = N*N*2
<span style="color: #008080;">constant</span> <span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2200</span><span style="color: #0000FF;">,</span>
sequence found = repeat(false,N),
<span style="color: #000000;">N2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">*</span><span style="color: #000000;">N</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span>
squares = repeat(false,N2)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">),</span>
-- first mark all numbers that can be the sum of two squares
<span style="color: #000000;">squares</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N2</span><span style="color: #0000FF;">)</span>
for a=1 to N do
<span style="color: #000080;font-style:italic;">-- first mark all numbers that can be the sum of two squares</span>
integer a2 = a*a
<span style="color: #008080;">for</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
for b=a to N do
<span style="color: #004080;">integer</span> <span style="color: #000000;">a2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">a</span>
squares[a2+b*b] = true
<span style="color: #008080;">for</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">a</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">squares</span><span style="color: #0000FF;">[</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
-- now find all d such that d^2 - c^2 is in squares
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for d=1 to N do
<span style="color: #000080;font-style:italic;">-- now find all d such that d^2 - c^2 is in squares</span>
integer d2 = d*d
<span style="color: #008080;">for</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
for c=1 to d-1 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">d2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">*</span><span style="color: #000000;">d</span>
if squares[d2-c*c] then
<span style="color: #008080;">for</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
found[d] = true
<span style="color: #008080;">if</span> <span style="color: #000000;">squares</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">c</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
exit
<span style="color: #000000;">found</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
end if
<span style="color: #008080;">exit</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
sequence res = {}
for i=1 to N do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
if not found[i] then res &= i end if
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
end for
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">found</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">i</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
?res</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,454 ⟶ 1,746:
=={{header|PicoLisp}}==
{{trans|C}}
<langsyntaxhighlight PicoLisplang="picolisp">(de quadruples (N)
(let (AB NIL S 3 R)
(for A N
Line 1,474 ⟶ 1,766:
(or (idx 'R A) (link A)) ) ) ) )
 
(println (quadruples 2200))</langsyntaxhighlight>
 
{{out}}
<pre>(1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048)</pre>
 
=={{header|PureBasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">OpenConsole()
limite.i = 2200
s.i = 3
Dim l.i(limite)
Dim ladd.i(limite * limite * 2)
 
For x.i = 1 To limite
x2.i = x * x
For y = x To limite
ladd(x2 + y * y) = 1
Next y
Next x
 
For x.i = 1 To limite
s1.i = s
s.i + 2
s2.i = s
For y = x +1 To limite
If ladd(s1) = 1
l(y) = 1
EndIf
s1 + s2
s2 + 2
Next y
Next x
 
For x.i = 1 To limite
If l(x) = 0
Print(Str(x) + " ")
EndIf
Next x
Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048</pre>
 
=={{header|Python}}==
===Search===
{{trans|Julia}}
<langsyntaxhighlight Pythonlang="python">def quad(top=2200):
r = [False] * top
ab = [False] * (top * 2)**2
Line 1,500 ⟶ 1,830:
if __name__ == '__main__':
n = 2200
print(f"Those values of d in 1..{n} that can't be represented: {quad(n)}")</langsyntaxhighlight>
 
{{out}}
Line 1,510 ⟶ 1,840:
{{Trans|JavaScript}}
{{Trans|AppleScript}}
<syntaxhighlight lang="python">'''Pythagorean Quadruples'''
<lang Python>from itertools import (islice)
 
from itertools import islice, takewhile
 
 
# main :: IO ()
# unrepresentables :: () -> [Int]
def main():
def unrepresentables():
print (
'''A non-finite stream of powers of two which can
takeWhileGen(lambda x: 2200 > x)(
not be represented as a mergeInOrder(powersOfTwo())(Pythagorean quadruple.
'''
map(lambda x: 5 * x, powersOfTwo())
return )merge(
powersOfTwo()
)(
5 * x for x in powersOfTwo()
)
 
Line 1,526 ⟶ 1,859:
# powersOfTwo :: Gen [Int]
def powersOfTwo():
'''A non-finite stream of successive powers of two.
return iterate(lambda x: 2 * x)(1)
'''
def double(x):
return 2 * x
 
return iterate(double)(1)
 
# mergeInOrder :: Gen [Int] -> Gen [Int] -> Gen [Int]
def mergeInOrder(ga):
def go(ma, mb):
a = ma
b = mb
while not a['Nothing'] and not b['Nothing']:
ta = a['Just']
tb = b['Just']
if ta[0] < tb[0]:
yield(ta[0])
a = uncons(ta[1])
else:
yield(tb[0])
b = uncons(tb[1])
 
# ------------------------- TEST -------------------------
return lambda gb: go(uncons(ga), uncons(gb))
# main :: IO ()
def main():
'''For positive integers up to 2,200 (inclusive)
'''
def p(x):
return 2200 >= x
 
print(
list(
takewhile(p, unrepresentables())
)
)
 
# GENERIC ABSTRACTIONS ------------------------------------
 
# ----------------------- GENERIC ------------------------
 
# iterate :: (a -> a) -> a -> Gen [a]
def iterate(f):
'''An infinite list of repeated
applications of f to x.
'''
def go(x):
v = x
while True:
yield( v)
v = f(v)
return lambda x: go(x)
 
 
# Justmerge :: aGen [Int] -> Gen [Int] -> MaybeGen a[Int]
def Justmerge(xga):
'''An ordered stream of values drawn from two
return {'type': 'Maybe', 'Nothing': False, 'Just': x}
other ordered streams.
 
'''
 
def go(gb):
# Nothing :: Maybe a
def Nothingf(ma, mb):
a, b = ma, mb
return {'type': 'Maybe', 'Nothing': True}
while a and b:
ta, tb = a, b
if ta[0] < tb[0]:
yield ta[0]
a = uncons(ta[1])
else:
yield tb[0]
b = uncons(tb[1])
return f(uncons(ga), uncons(gb))
return go
 
 
Line 1,573 ⟶ 1,920:
# take :: Int -> String -> String
def take(n):
return'''The lambdaprefix of xs: (of length n,
or xs[0: itself if n] > length xs.
'''
if isinstance(xs, list)
else list(islice(xs, n))
)
 
 
# takeWhileGen :: (a -> Bool) -> Gen [a] -> [a]
def takeWhileGen(p):
def go(xs):
vsreturn = [](
v = next( xs)[0:n]
while (None is not vif andisinstance(xs, p(vlist, tuple)):
vs.appendelse list(vislice(xs, n))
v = next(xs)
return vsgo
return lambda xs: go(xs)
 
 
# uncons :: [a] -> Maybe (a, [a])
def uncons(xs):
'''The deconstruction of a non-empty list
(or generator stream) into two parts:
a head value, and the remaining values.
'''
if isinstance(xs, list):
return Just((xs[0], xs[1:])) if 0 < len(xs) else Nothing()None
else:
nxt = take(1)(xs)
return Just((nxt[0], xs)) if 0 < len(nxt) else Nothing()None
 
 
# MAIN ---
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
<pre>[1, 2, 4, 5, 8, 10, 16, 20, 32, 40, 64, 80, 128, 160, 256, 320, 512, 640, 1024, 1280, 2048]</pre>
 
=={{header|R}}==
The best solution to this problem - using lots of for loops - is practically language agnostic. The R way of doing this is far less efficient, taking about 10 minutes on my machine, but it's the obvious way to do this in R.
<syntaxhighlight lang="rsplus">squares <- d <- seq_len(2200)^2
aAndb <- outer(squares, squares, '+')
aAndb <- aAndb[upper.tri(aAndb, diag = TRUE)]
sapply(squares, function(c) d <<- setdiff(d, aAndb + c))
print(sqrt(d))</syntaxhighlight>
{{out}}
<pre>[1] 1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048</pre>
 
=={{header|Racket}}==
Line 1,610 ⟶ 1,965:
{{trans|Python}}
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require data/bit-vector)
Line 1,635 ⟶ 1,990:
(printf "Those values of d in 1..~a that can't be represented: ~a~%" n (quadruples n)))
 
(report 2200)</langsyntaxhighlight>
 
{{out}}
 
<pre>Those values of d in 1..2200 that 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)</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.09}}
 
<syntaxhighlight lang="raku" line>my \N = 2200;
my @sq = (0 .. N)»²;
my @not = False xx N;
@not[0] = True;
 
(1 .. N).race.map: -> $d {
my $last = 0;
for $d ... ($d/3).ceiling -> $a {
for 1 .. ($a/2).ceiling -> $b {
last if (my $ab = @sq[$a] + @sq[$b]) > @sq[$d];
if (@sq[$d] - $ab).sqrt.narrow ~~ Int {
@not[$d] = True;
$last = 1;
last
}
}
last if $last;
}
}
 
say @not.grep( *.not, :k );</syntaxhighlight>
{{out}}
<pre>(1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048)</pre>
 
=={{header|REXX}}==
Line 1,645 ⟶ 2,028:
This version is a brute force algorithm, with some optimization (to save compute time)
<br>which pre-computes some of the squares of the positive integers used in the search.
<syntaxhighlight lang="rexx">/*REXX pgm computes/shows (integers), D that aren't possible for: a² + b² + c² = d² */
<br>Integers too large for the precomputed values use an optimized integer square root.
<lang rexx>/*REXX pgm computes/shows (integers), D that aren't possible for: a² + b² + c² = d² */
parse arg hi . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi=2200; high= 3 * hi /*Not specified? Then use the default.*/
Line 1,671 ⟶ 2,053:
do p=1 for hi; if d.p==. then $=$ p /*Not possible? Then add it to the list*/
end /*p*/ /* [↓] display list of not-possibles. */
say substr($, 2) /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,689 ⟶ 2,071:
Programming note: &nbsp; testing for &nbsp; '''a''' &nbsp; and &nbsp; '''b''' &nbsp; both being &nbsp; <big>odd</big> &nbsp; (lines '''15''' and '''16''' &nbsp; that each contain a &nbsp; '''do''' &nbsp; loop) &nbsp; as
<br>being a case that won't produce any solutions actually slows up the calculations and makes the program execute slower.
<langsyntaxhighlight lang="rexx">/*REXX pgm computes/shows (integers), D that aren't possible for: a² + b² + c² = d² */
parse arg hi . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi=2200 /*Not specified? Then use the default.*/
Line 1,719 ⟶ 2,101:
do p=1 for hi; if d.p==. then $= $ p /*Not possible? Then add it to the list*/
end /*p*/ /* [↓] display list of not-possibles. */
say substr($, 2) /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is the same as the 1<sup>st</sup> REXX version.}} <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring"># Project : Pythagorean quadruples
 
limit = 2200
Line 1,745 ⟶ 2,127:
next
see pqstr + nl
</syntaxhighlight>
</lang>
{{Out}}
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
Line 1,751 ⟶ 2,133:
=={{header|Ruby}}==
{{trans|VBA}}
<langsyntaxhighlight Rubylang="ruby">n = 2200
l_add, l = {}, {}
1.step(n) do |x|
Line 1,771 ⟶ 2,153:
 
puts (1..n).reject{|x| l[x]}.join(" ")
</syntaxhighlight>
</lang>
{{outOut}}
<pre>1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
</pre>
Considering the observations in the Rust and Sidef sections and toying with Enumerators :
<syntaxhighlight lang="ruby">squares = Enumerator.new{|y| (0..).each{|n| y << 2**n} }
squares5 = Enumerator.new{|y| (0..).each{|n| y << 2**n*5} }
 
pyth_quad = Enumerator.new do |y|
n = squares.next
m = squares5.next
loop do
if n < m
y << n
n = squares.next
else
y << m
m = squares5.next
end
end
end
# this takes less than a millisecond
puts pyth_quad.take_while{|n| n <= 1000000000}.join(" ")</syntaxhighlight>
{{Out}}
<pre>
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048 2560 4096 5120 8192 10240 16384 20480 32768 40960 65536 81920 131072 163840 262144 327680 524288 655360 1048576 1310720 2097152 2621440 4194304 5242880 8388608 10485760 16777216 20971520 33554432 41943040 67108864 83886080 134217728 167772160 268435456 335544320 536870912 671088640
</pre>
 
=={{header|Rust}}==
 
{{output?}}
 
This is equivalent to https://oeis.org/A094958
which simply contains positive integers of the form 2^n or 5*2^n. Multiple implementations are provided.
We provide iterator-style and loop-style code:
 
<langsyntaxhighlight lang="rust">
use std::collections::BinaryHeap;
 
Line 1,792 ⟶ 2,198:
.collect::<BinaryHeap<u16>>()
.into_sorted_vec()
}
 
fn a094958_filter() -> Vec<u16> {
(1..2200) // ported from Sidef
.filter(|n| ((n & (n - 1) == 0) || (n % 5 == 0 && ((n / 5) & (n / 5 - 1) == 0))))
.collect()
}
 
Line 1,809 ⟶ 2,221:
println!("{:?}", a094958_iter());
println!("{:?}", a094958_loop());
println!("{:?}", a094958_filter());
}
 
Line 1,819 ⟶ 2,232:
assert!(format!("{:?}", a094958_iter()) == HAPPY);
}
#[test]
fn test_a094958_loop() {
assert!(format!("{:?}", a094958_loop()) == HAPPY);
}
#[test]
fn test_a094958_filter() {
assert!(format!("{:?}", a094958_filter()) == HAPPY);
}
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/drfij1d/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/6AHn7YXSRbKHzmOY5rWAwg Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">object PythagoreanQuadruple extends App {
val MAX = 2200
val MAX2: Int = MAX * MAX * 2
Line 1,854 ⟶ 2,272:
println(notRepresented.mkString(" "))
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby"># Finds all solutions (a,b) such that: a^2 + b^2 = n^2
<lang ruby>require('ntheory')
 
# Finds all solutions (a,b) such that: a^2 + b^2 = n^2
func sum_of_two_squares(n) is cached {
 
Line 1,898 ⟶ 2,314:
for p,e in (prime_powers) {
var pp = p**e
var r = %S<ntheory>.sqrtmod("#{pp - 1}", "#{pp}")
take([[r, pp], [pp - r, pp]])
}
Line 1,953 ⟶ 2,369:
sum_of_three_squares(n) || take(n)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,960 ⟶ 2,376:
 
Numbers d that cannot be expressed as a^2 + b^2 + c^2 = d^2, are numbers of the form 2^n or 5*2^n:
<langsyntaxhighlight lang="ruby">say gather {
for n in (1..2200) {
if ((n & (n-1) == 0) || (n%%5 && ((n/5) & (n/5 - 1) == 0))) {
Line 1,966 ⟶ 2,382:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,974 ⟶ 2,390:
=={{header|Swift}}==
{{trans|C}}
<langsyntaxhighlight lang="swift">func missingD(upTo n: Int) -> [Int] {
var a2 = 0, s = 3, s1 = 0, s2 = 0
var res = [Int](repeating: 0, count: n + 1)
Line 2,005 ⟶ 2,421:
}
 
print(missingD(upTo: 2200))</langsyntaxhighlight>
 
{{out}}
Line 2,013 ⟶ 2,429:
=={{header|VBA}}==
{{trans|FreeBasic}}
<langsyntaxhighlight lang="vb">Const n = 2200
Public Sub pq()
Dim s As Long, s1 As Long, s2 As Long, x As Long, x2 As Long, y As Long: s = 3
Line 2,037 ⟶ 2,453:
Next
Debug.Print
End Sub</langsyntaxhighlight>{{out}}
<pre> 1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048 </pre>
 
=={{header|Wren}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="wren">var N = 2200
var N2 = N * N * 2
var s = 3
var s1 = 0
var s2 = 0
var r = List.filled(N + 1, false)
var ab = List.filled(N2 + 1, false)
 
for (a in 1..N) {
var a2 = a * a
for (b in a..N) ab[a2 + b*b] = true
}
 
for (c in 1..N) {
s1 = s
s = s + 2
s2 = s
var d = c + 1
while (d <= N) {
if (ab[s1]) r[d] = true
s1 = s1 + s2
s2 = s2 + 2
d = d + 1
}
}
 
for (d in 1..N) {
if (!r[d]) System.write("%(d) ")
}
System.print()</syntaxhighlight>
 
{{out}}
<pre>
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
Twenty-seven seconds on my (cheap) Raspberry Pi 4.
<syntaxhighlight lang "XPL0">def N = 2200;
int A, B, C, D, AABB, AABBCC;
char R(N+1);
[FillMem(R, 0, N+1); \zero solution array
for A:= 1 to N do
[for B:= A to N do
[if (A&1 and B&1) = 0 then \for positive odd A and B, no solution
[AABB:= A*A + B*B;
for C:= B to N do
[AABBCC:= AABB + C*C;
D:= sqrt(AABBCC);
if AABBCC = D*D and D <= N then R(D):= 1; \solution
];
];
];
];
for A:= 1 to N do
if R(A) = 0 then
[IntOut(0, A); ChOut(0, ^ )]; \print non-solutions
CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
</pre>
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="freebasic">limite = 2200
s = 3
dim l(limite)
dim ladd(limite * limite * 2)
 
for x = 1 to limite
x2 = x * x
for y = x to limite
ladd(x2 + y * y) = 1
next y
next x
 
for x = 1 to limite
s1 = s
s = s + 2
s2 = s
for y = x +1 to limite
if ladd(s1) = 1 l(y) = 1
s1 = s1 + s2
s2 = s2 + 2
next y
next x
 
for x = 1 to limite
if l(x) = 0 print str$(x), " ";
next x
print
end</syntaxhighlight>
{{out}}
<pre>1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048</pre>
 
=={{header|zkl}}==
{{trans|ALGOL 68}}
<langsyntaxhighlight lang="zkl"># find values of d where d^2 =/= a^2 + b^2 + c^2 for any integers a, b, c #
# where d in [1..2200], a, b, c =/= 0 #
# max number to check #
Line 2,068 ⟶ 2,585:
if(not solution[ d ]) print(d, " ");
}
println();</langsyntaxhighlight>
{{out}}
<pre>
113

edits