RosettaCodeData/Task/Goldbachs-comet/XPL0/goldbachs-comet.xpl0
2023-07-01 13:44:08 -04:00

43 lines
965 B
Text

func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
int PT(1_000_000);
func G(E); \Ways E can be expressed as sum of two primes
int E, C, I, J, T;
[C:= 0; I:= 0;
loop [J:= I;
if PT(J) + PT(I) > E then return C;
loop [T:= PT(J) + PT(I);
if T = E then C:= C+1;
if T > E then quit;
J:= J+1;
];
I:= I+1;
];
];
int I, N;
[I:= 0; \make prime table
for N:= 2 to 1_000_000 do
if IsPrime(N) then
[PT(I):= N; I:= I+1];
I:= 4; \show first 100 G numbers
Format(4, 0);
for N:= 1 to 100 do
[RlOut(0, float(G(I)));
if rem(N/10) = 0 then CrLf(0);
I:= I+2;
];
CrLf(0);
Text(0, "G(1,000,000) = "); IntOut(0, G(1_000_000));
CrLf(0);
]