RosettaCodeData/Task/Truncatable-primes/XPL0/truncatable-primes.xpl0
Ingy döt Net 68f8f3e56b all tasks
2013-04-11 01:07:29 -07:00

42 lines
1.1 KiB
Text

code CrLf=9, IntOut=11;
func Prime(P); \Return true if P is a prime number
int P; \(1 is not prime, but 2 is, etc.)
int I;
[if P<=1 then return false; \negative numbers are not prime
for I:= 2 to sqrt(P) do
if rem(P/I) = 0 then return false;
return true;
];
func RightTrunc(N); \Return largest right-truncatable prime < one million
int N;
int M;
[for N:= 1_000_000-1 downto 2 do
[M:= N;
loop [if not Prime(M) then quit;
M:= M/10;
if rem(0) = 0 then quit; \no zeros allowed
if M=0 then return N;
];
];
];
func LeftTrunc(N); \Return largest left-truncatable prime < one million
int N;
int M, P;
[for N:= 1_000_000-1 downto 2 do
[M:= N;
P:=100_000;
loop [if not Prime(M) then quit;
M:= rem(M/P);
P:= P/10;
if M<P then quit; \no zeros allowed
if M=0 then return N;
];
];
];
[IntOut(0, LeftTrunc); CrLf(0);
IntOut(0, RightTrunc); CrLf(0);
]