Pythagorean quadruples: Difference between revisions
Added solution for EDSAC
(Added solution for EDSAC) |
|||
(6 intermediate revisions by 6 users not shown) | |||
Line 38:
{{trans|Python}}
<
V r = [0B] * top
V ab = [0B] * (top * 2)^2
Line 54:
R enumerate(r).filter((i, val) -> !val & i).map((i, val) -> i)
print(quad())</
{{out}}
Line 63:
=={{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.
<
# 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 96:
OD;
print( ( newline ) )
END</
{{out}}
<pre>
Line 103:
=={{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>
Line 143 ⟶ 144:
MCLEAR(temp, found, a, r)
END
</syntaxhighlight>
{{out}}
<pre>
Line 152 ⟶ 153:
=={{header|AppleScript}}==
<
on double(x)
x + x
Line 351 ⟶ 352:
end if
end if
end uncons</
{{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
Line 388 ⟶ 389:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 397 ⟶ 398:
===Version 1===
Five seconds on my Intel Linux box.
<
#include <math.h>
#include <string.h>
Line 422 ⟶ 423:
if(!r[a]) printf("%d ",a); // print non solution
printf("\n");
}</
{{out}}
<pre>
Line 431 ⟶ 432:
===Version 2 (much faster)===
Translation of second version of FreeBASIC entry. Runs in about 0.15 seconds.
<
#include <stdio.h>
#include <string.h>
Line 466 ⟶ 467:
free(ab);
return 0;
}</
{{out}}
<pre>
Line 474 ⟶ 475:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
namespace PythagoreanQuadruples {
Line 511 ⟶ 512:
}
}
}</
{{out}}
<pre>The values of d <= 2200 which can't be represented:
Line 518 ⟶ 519:
=={{header|C++}}==
{{trans|D}}
<
#include <vector>
Line 561 ⟶ 562:
return 0;
}</
{{out}}
<pre>The values of d <= 2200 which can't be represented:
Line 568 ⟶ 569:
=={{header|Crystal}}==
{{trans|Ruby}}
<
l_add, l = Hash(Int32, Bool).new(false), Hash(Int32, Bool).new(false)
(1..n).each do |x|
Line 587 ⟶ 588:
end
puts (1..n).reject{ |x| l[x] }.join(" ")</
{{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.
<
squares5 = (0..).each.map { |n| 2_u64**n * 5 }
Line 609 ⟶ 610:
end
puts pyth_quad.take_while { |n| n <= 1000000000 }.join(" ")</
{{Out}}
<pre>
Line 617 ⟶ 618:
=={{header|D}}==
{{trans|C}}
<
import std.stdio;
Line 659 ⟶ 660:
}
writeln;
}</
{{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.
<
' compile with: fbc -s console
Line 719 ⟶ 807:
Print : Print "hit any key to end program"
Sleep
End</
{{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
<
' compile with: fbc -s console
Line 759 ⟶ 847:
Print : Print "hit any key to end program"
Sleep
End</
{{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}}
<
import "fmt"
Line 806 ⟶ 944:
}
fmt.Println()
}</
{{out}}
Line 814 ⟶ 952:
=={{header|Haskell}}==
<
powersOfTwo = iterate (2 *) 1
Line 828 ⟶ 966:
main = do
putStrLn "The values of d <= 2200 which can't be represented."
print $ takeWhile (<= 2200) unrepresentable</
{{out}}
<pre>The values of d <= 2200 which can't be represented.
Line 835 ⟶ 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">
Filter =: (#~`)(`:6)
Line 853 ⟶ 991:
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
</syntaxhighlight>
=={{header|Java}}==
Line 859 ⟶ 997:
Compute sequence directly.
<
import java.util.ArrayList;
import java.util.List;
Line 898 ⟶ 1,036:
}
</syntaxhighlight>
{{out}}
Line 908 ⟶ 1,046:
=={{header|JavaScript}}==
{{Trans|Haskell}}
<
'use strict';
Line 1,052 ⟶ 1,190:
// MAIN ---
return main();
})();</
{{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,062 ⟶ 1,200:
be accomplished in jq without `foreach`. Notice also how
`first/1` is used in `is_pythagorean_quad/0` to avoid unnecessary computation.
<
def is_pythagorean_quad:
. as $d
Line 1,077 ⟶ 1,215:
# The specific task:
[range(1; 2201) | select( is_pythagorean_quad | not )] | join(" ")</
'''Invocation and Output'''
Line 1,087 ⟶ 1,225:
{{trans|C}}
<
r = falses(N)
ab = falses(2N ^ 2)
Line 1,108 ⟶ 1,246:
end
println("Pythagorean quadruples up to 2200: ", join(quadruples(), ", "))</
{{out}}
Line 1,116 ⟶ 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:
<
const val MAX = 2200
Line 1,150 ⟶ 1,288:
for (i in 1..MAX) if (!found[i]) print("$i ")
println()
}</
{{out}}
Line 1,161 ⟶ 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.
<
const val MAX = 2200
Line 1,190 ⟶ 1,328:
for (d in 1..MAX) if (!found[d]) print("$d ")
println()
}</
{{out}}
Line 1,198 ⟶ 1,336:
=={{header|Lua}}==
<
local N = 2200
local ar = {}
Line 1,228 ⟶ 1,366:
end
end
print()</
{{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}}==
<
maxsq = max^2;
d = Range[max]^2;
Line 1,248 ⟶ 1,386:
{a, Floor[maxsq^(1/2)]}
]
Sqrt[d]</
{{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,254 ⟶ 1,392:
=={{header|Modula-2}}==
{{trans|C}}
<
FROM FormatString IMPORT FormatString;
FROM RealMath IMPORT sqrt;
Line 1,307 ⟶ 1,445:
ReadChar
END PythagoreanQuadruples.</
{{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,315 ⟶ 1,453:
===Version 1===
<syntaxhighlight lang="text">import math
const N = 2_200
Line 1,334 ⟶ 1,472:
for i in 1..N:
if not r[I]: stdout.write i, " "
echo()</
{{out}}
Line 1,340 ⟶ 1,478:
===Version 2===
<syntaxhighlight lang="text">const N = 2_200
const N2 = N * N * 2
Line 1,362 ⟶ 1,500:
for d in 1..N:
if not r[d]: stdout.write d, " "</
{{out}}
Line 1,372 ⟶ 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>
<
//find phythagorean Quadrupel up to a,b,c,d <= 2200
//a^2 + b^2 +c^2 = d^2
Line 1,441 ⟶ 1,579:
writeln(CheckCnt,' checks were done');
end.
</syntaxhighlight>
{{out}}
<pre>
Line 1,453 ⟶ 1,591:
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.
<
//find phythagorean Quadrupel up to a,b,c,d <= 2200
//a^2 + b^2 +c^2 = d^2
Line 1,523 ⟶ 1,661:
writeln;
end.
</syntaxhighlight>
{{Out}}
<pre>
Line 1,538 ⟶ 1,676:
=={{header|Perl}}==
{{trans|Raku}}
<
push @sq, $_**2 for 0 .. $N;
my @not = (0) x $N;
Line 1,566 ⟶ 1,704:
$result .= "$_ " unless $not[$_]
}
print "$result\n"</
{{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)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
<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>
<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>
<span style="color: #000080;font-style:italic;">-- first mark all numbers that can be the sum of two squares</span>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- now find all d such that d^2 - c^2 is in squares</span>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<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>
<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>
<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,605 ⟶ 1,746:
=={{header|PicoLisp}}==
{{trans|C}}
<
(let (AB NIL S 3 R)
(for A N
Line 1,625 ⟶ 1,766:
(or (idx 'R A) (link A)) ) ) ) )
(println (quadruples 2200))</
{{out}}
Line 1,632 ⟶ 1,773:
=={{header|PureBasic}}==
{{trans|FreeBASIC}}
<
limite.i = 2200
s.i = 3
Line 1,664 ⟶ 1,805:
Next x
Input()
CloseConsole()</
{{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,671 ⟶ 1,812:
===Search===
{{trans|Julia}}
<
r = [False] * top
ab = [False] * (top * 2)**2
Line 1,689 ⟶ 1,830:
if __name__ == '__main__':
n = 2200
print(f"Those values of d in 1..{n} that can't be represented: {quad(n)}")</
{{out}}
Line 1,699 ⟶ 1,840:
{{Trans|JavaScript}}
{{Trans|AppleScript}}
<
from itertools import islice, takewhile
Line 1,806 ⟶ 1,947:
# MAIN ---
if __name__ == '__main__':
main()</
{{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,812 ⟶ 1,953:
=={{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.
<
aAndb <- outer(squares, squares, '+')
aAndb <- aAndb[upper.tri(aAndb, diag = TRUE)]
sapply(squares, function(c) d <<- setdiff(d, aAndb + c))
print(sqrt(d))</
{{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>
Line 1,824 ⟶ 1,965:
{{trans|Python}}
<
(require data/bit-vector)
Line 1,849 ⟶ 1,990:
(printf "Those values of d in 1..~a that can't be represented: ~a~%" n (quadruples n)))
(report 2200)</
{{out}}
Line 1,859 ⟶ 2,000:
{{works with|Rakudo|2018.09}}
<syntaxhighlight lang="raku"
my @sq = (0 .. N)»²;
my @not = False xx N;
Line 1,879 ⟶ 2,020:
}
say @not.grep( *.not, :k );</
{{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,887 ⟶ 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.
<
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,912 ⟶ 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. */</
{{out|output|text= when using the default input:}}
<pre>
Line 1,930 ⟶ 2,071:
Programming note: testing for '''a''' and '''b''' both being <big>odd</big> (lines '''15''' and '''16''' that each contain a '''do''' loop) as
<br>being a case that won't produce any solutions actually slows up the calculations and makes the program execute slower.
<
parse arg hi . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi=2200 /*Not specified? Then use the default.*/
Line 1,960 ⟶ 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. */</
{{out|output|text= is the same as the 1<sup>st</sup> REXX version.}} <br><br>
=={{header|Ring}}==
<
limit = 2200
Line 1,986 ⟶ 2,127:
next
see pqstr + nl
</syntaxhighlight>
{{Out}}
1 2 4 5 8 10 16 20 32 40 64 80 128 160 256 320 512 640 1024 1280 2048
Line 1,992 ⟶ 2,133:
=={{header|Ruby}}==
{{trans|VBA}}
<
l_add, l = {}, {}
1.step(n) do |x|
Line 2,012 ⟶ 2,153:
puts (1..n).reject{|x| l[x]}.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
</pre>
Considering the observations in the Rust and Sidef sections and toying with Enumerators :
<
squares5 = Enumerator.new{|y| (0..).each{|n| y << 2**n*5} }
Line 2,034 ⟶ 2,175:
end
# this takes less than a millisecond
puts pyth_quad.take_while{|n| n <= 1000000000}.join(" ")</
{{Out}}
<pre>
Line 2,047 ⟶ 2,188:
which simply contains positive integers of the form 2^n or 5*2^n. Multiple implementations are provided.
<
use std::collections::BinaryHeap;
Line 2,100 ⟶ 2,241:
}
}
</syntaxhighlight>
=={{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)].
<
val MAX = 2200
val MAX2: Int = MAX * MAX * 2
Line 2,131 ⟶ 2,272:
println(notRepresented.mkString(" "))
}</
=={{header|Sidef}}==
<
func sum_of_two_squares(n) is cached {
Line 2,228 ⟶ 2,369:
sum_of_three_squares(n) || take(n)
}
}</
{{out}}
<pre>
Line 2,235 ⟶ 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:
<
for n in (1..2200) {
if ((n & (n-1) == 0) || (n%%5 && ((n/5) & (n/5 - 1) == 0))) {
Line 2,241 ⟶ 2,382:
}
}
}</
{{out}}
<pre>
Line 2,249 ⟶ 2,390:
=={{header|Swift}}==
{{trans|C}}
<
var a2 = 0, s = 3, s1 = 0, s2 = 0
var res = [Int](repeating: 0, count: n + 1)
Line 2,280 ⟶ 2,421:
}
print(missingD(upTo: 2200))</
{{out}}
Line 2,288 ⟶ 2,429:
=={{header|VBA}}==
{{trans|FreeBasic}}
<
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,312 ⟶ 2,453:
Next
Debug.Print
End Sub</
<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}}
<
var N2 = N * N * 2
var s = 3
Line 2,346 ⟶ 2,487:
if (!r[d]) System.write("%(d) ")
}
System.print()</
{{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>
Line 2,355 ⟶ 2,525:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<
s = 3
dim l(limite)
Line 2,382 ⟶ 2,552:
next x
print
end</
{{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 2,388 ⟶ 2,558:
=={{header|zkl}}==
{{trans|ALGOL 68}}
<
# where d in [1..2200], a, b, c =/= 0 #
# max number to check #
Line 2,415 ⟶ 2,585:
if(not solution[ d ]) print(d, " ");
}
println();</
{{out}}
<pre>
|