Palindromic primes: Difference between revisions
Added Oberon-07
m (→{{header|ALGOL 68}}: tweak) |
(Added Oberon-07) |
||
(One intermediate revision by one other user not shown) | |||
Line 715:
<pre> 2 3 5 7 11 101 131 151 181 191
313 353 373 383 727 757 787 797 919 929</pre>
=={{header|Oberon-07}}==
Based on the Algol 68 sample with the Sieve routine from the Additive Primes task.
<syntaxhighlight lang="modula2">
MODULE PalindromicPrimes; (* find primes that are palendromic in base 10 *)
IMPORT
Out;
CONST
Max = 999;
VAR
fl, m, n :INTEGER;
Prime :ARRAY Max + 1 OF BOOLEAN;
PROCEDURE Sieve;
VAR i, j :INTEGER;
BEGIN
Prime[ 0 ] := FALSE; Prime[ 1 ] := FALSE;
FOR i := 2 TO Max DO Prime[ i ] := TRUE END;
FOR i := 2 TO Max DIV 2 DO
IF Prime[ i ] THEN
j := i * 2;
WHILE j <= Max DO
Prime[ j ] := FALSE;
j := j + i
END
END Sieve;
PROCEDURE OutN;
BEGIN
Out.String( " " );Out.Int( n, 0 )
END OutN;
BEGIN
Sieve;
(* print the palendromic primes in the base 10 *)
(* all 1 digit primes are palindromic *)
(* the only palindromic 2 digit numbers are multiples of 11 *)
(* so 11 is the only possible 2 digit palindromic prime *)
FOR n := 1 TO 11 DO IF Prime[ n ] THEN OutN END END;
(* three digit numbers, the first and last digits must be odd *)
(* and cannot be 5 (as the number would be divisible by 5) *)
FOR fl := 1 TO 9 BY 2 DO
IF fl # 5 THEN
FOR m := 0 TO 9 DO
n := ( ( ( fl * 10 ) + m ) * 10 ) + fl;
IF Prime[ n ] THEN
(* have a palindromic prime *)
OutN
END
END
END
END;
Out.Ln
END PalindromicPrimes.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
</pre>
=={{header|PARI/GP}}==
Line 1,217 ⟶ 1,281:
rem - return true if n is palindromic, otherwise false
function
var i, j
var s = string
s = str$(n)
Line 1,228 ⟶ 1,292:
j = j - 1
end
end
▲ result = TRUE
▲ else
rem - return n mod m
Line 1,262 ⟶ 1,322:
for i = 2 to 1000
if isprime(i) then
if
begin
print using "##### ";i;
|