RosettaCodeData/Task/Magnanimous-numbers/XPL0/magnanimous-numbers.xpl0
2023-07-01 13:44:08 -04:00

45 lines
1.3 KiB
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;
];
func Magnan(N); \Return 'true' if N is a magnanimous number
int N, M, P;
[M:= 0; P:= 1;
loop [N:= N/10;
if N = 0 then return true;
M:= P*rem(0) + M;
P:= P*10;
if not IsPrime(N+M) then return false;
];
];
int Cnt, N;
[Cnt:= 0; N:= 0;
Text(0, "First 45 magnanimous numbers:^m^j");
loop [if N < 10 or Magnan(N) then
[Cnt:= Cnt+1;
if Cnt <= 45 then
[Format(4, 0);
RlOut(0, float(N));
if rem(Cnt/15) = 0 then CrLf(0);
];
if Cnt = 241 then
Text(0, "^m^j241st through 250th magnanimous numbers:^m^j");
if Cnt >= 241 and Cnt <= 250 then
[IntOut(0, N); ChOut(0, ^ )];
if Cnt = 391 then
Text(0, "^m^j^j391st through 400th magnanimous numbers:^m^j");
if Cnt >= 391 and Cnt <= 400 then
[IntOut(0, N); ChOut(0, ^ )];
if Cnt >= 400 then quit;
];
N:= N+1;
];
]