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

116 lines
2.7 KiB
Text

func IsCyclops(N); \Return 'true' if N is a cyclops number
int N, I, J, K;
char A(9);
[I:= 0; \parse digits into array A
repeat N:= N/10;
A(I):= rem(0);
I:= I+1;
until N=0;
if (I&1) = 0 then return false; \must have odd number of digits
K:= I>>1;
if A(K) # 0 then return false; \middle digit must be 0
for J:= 0 to I-1 do \other digits must not be 0
if A(J)=0 & J#K then return false;
return true;
];
func IsPrime(N); \Return 'true' if N > 2 is a prime number
int N, I;
[if (N&1) = 0 \even number\ then return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
func Blind(N); \Return blinded cyclops number
int N, I, J, K; \i.e. center zero removed
char A(9);
[I:= 0; \parse digits into array A
repeat N:= N/10;
A(I):= rem(0);
I:= I+1;
until N=0;
N:= A(I-1); \most significant digit
K:= I>>1;
for J:= I-2 downto 0 do
if J#K then \skip middle zero
N:= N*10 + A(J);
return N;
];
func Reverse(N); \Return N with its digits reversed
int N, M;
[M:= 0;
repeat N:= N/10;
M:= M*10 + rem(0);
until N=0;
return M;
];
func IntLen(N); \Return number of decimal digits in N
int N;
int P, I;
[P:= 10;
for I:= 1 to 9 do \assumes N is 32-bits
[if P>N then return I;
P:= P*10;
];
return 10;
];
int Count, N;
func Show; \Show results and return 'true' when done
[Count:= Count+1;
if Count <= 50 then
[IntOut(0, N);
if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
];
if N > 10_000_000 then
[Text(0, "First such number > 10,000,000: ");
IntOut(0, N);
Text(0, " at zero based index: ");
IntOut(0, Count-1);
CrLf(0);
return true;
];
return false;
];
proc Common(Filter); \Common code gathered here
int Filter;
[Count:= 0;
N:= 0;
loop [if IsCyclops(N) then
case Filter of
0: if Show then quit;
1: if IsPrime(N) then
if Show then quit;
2: if IsPrime(N) then if IsPrime(Blind(N)) then
if Show then quit;
3: if N=Reverse(N) then if IsPrime(N) then
if Show then quit
other [];
N:= N+1;
if (IntLen(N)&1) = 0 then N:= N*10; \must have odd number of digits
];
];
[Text(0, "First 50 cyclops numbers:
");
Common(0);
Text(0, "
First 50 prime cyclops numbers:
");
Common(1);
Text(0, "
First 50 blind prime cyclops numbers:
");
Common(2);
Text(0, "
First 50 palindromic prime cyclops numbers:
");
Common(3);
]