Pythagorean quadruples: Difference between revisions
Content added Content deleted
m (→version 2: reduced runtime ( ~1/5 ) 22,000 from 0m4.184s downto 0m0,746s) |
|||
Line 1,338:
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.
<lang pascal>program pythQuad_2;
Line 1,343 ⟶ 1,344:
//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
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));
Begin▼
Begin
For
end;▼
end;
procedure CheckDifD2C2;
var
d,d2,c : tIdx;
begin
For d := 1 to MaxFactor do
Begin
//c < d => (d*d-c*c) > 0
d2 := d*d;
For c := d-1 downto 1 do
Begin
if sumA2B2[
▲ Begin
Check[d] := 1;
//first for d found is enough
BREAK;
end;
end;
Line 1,391 ⟶ 1,404:
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
MaxFactor = 22000
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
.. 2048 2560 4096 5120 8192 10240 16384 20480 32768 40960
real 0m3,222s
</pre>
|