Pythagorean quadruples: Difference between revisions
Content added Content deleted
m (→{{header|Pascal}}: Give i386 a chance und improved runtime for both Cpu64/Cpu32 :-)) |
(→{{header|Pascal}}: added Version 2 like REXX optimized) |
||
Line 1,123: | Line 1,123: | ||
</pre> |
</pre> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} compiled with fpc 3.2.0 ( 2019.01.10 ) -O4 -Xs |
||
===version 1=== |
|||
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> |
Stopping search if limit is reached<BR> |
||
<lang pascal>program pythQuad; |
<lang pascal>program pythQuad; |
||
Line 1,200: | Line 1,202: | ||
929605937 checks were done |
929605937 checks were done |
||
real 0m2.323s -> 9 cpu-cycles per check on Ryzen 5 1600 3,7 Ghz ( Turbo ) |
real 0m2.323s -> 9 cpu-cycles per check on Ryzen 5 1600 3,7 Ghz ( Turbo ) |
||
</pre> |
|||
===version 2=== |
|||
Using a variant of [http://rosettacode.org/wiki/Pythagorean_quadruples#optimized REXX optimized] optimized<BR>Quite fast. |
|||
<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 |
|||
const |
|||
MaxFactor =2200; |
|||
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 : tIdx; |
|||
s : tSum; |
|||
begin |
|||
For a := 1 to MaxFactor do |
|||
For b := a to MaxFactor do |
|||
Begin |
|||
s := a*a+b*b; |
|||
if s < limit then |
|||
sumA2B2[s] := 1 |
|||
else |
|||
break; |
|||
end; |
|||
end; |
|||
procedure CheckDifD2C2; |
|||
var |
|||
d,c : tIdx; |
|||
s : tSum; |
|||
begin |
|||
For d := 1 to MaxFactor do |
|||
//c < d => (d*d-c*c) > 0 |
|||
For c := d-1 downto 1 do |
|||
Begin |
|||
s := d*d-c*c; |
|||
if sumA2B2[s] <> 0 then |
|||
Check[d] := 1; |
|||
end; |
|||
end; |
|||
var |
|||
i : NativeUint; |
|||
begin |
|||
BuildSumA2B2; |
|||
CheckDifD2C2; |
|||
//FindHoles; |
|||
For i := 1 to MaxFactor do |
|||
If Check[i] = 0 then |
|||
write(i,' '); |
|||
writeln; |
|||
end.</lang> |
|||
{{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 //4.8 Mb -> Level 3 cache 16 Mb ( Ryzen 5 1600 ) |
|||
//MaxFactor =22000;484 Mb -> no level X Cache |
|||
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 |
|||
real 0m4.184s |
|||
</pre> |
</pre> |
||