Find prime n such that reversed n is also prime: Difference between revisions
Content deleted Content added
→{{header|Ruby}}: Add Ruby |
|||
(15 intermediate revisions by 8 users not shown) | |||
Line 133:
383 389
34 primes.</pre>
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # find primes whose reversed digits are also prime #
INT max number = 500; # largest prime we will reverse #
INT max prime = 1 000; # enough primes to handle reversing #
# max number #
[ 1 : max prime ]BOOL p; FOR n TO UPB p DO p[ n ] := ODD n OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( UPB p ) DO
IF p[ i ] THEN
FOR s FROM i * i BY i + i TO UPB p DO p[ s ] := FALSE OD
FI
OD;
p[ 1 ] := FALSE; p[ 2 ] := TRUE;
OP FMT = ( INT n )STRING: whole( n, 0 );
INT count := 0;
FOR n TO max number DO
IF p[ n ] THEN
INT r := n MOD 10;
INT v := n;
WHILE ( v OVERAB 10 ) > 0 DO
r *:= 10 +:= v MOD 10
OD;
IF p[ r ] THEN
print( ( " ", whole( n, -3 ) ) );
IF ( count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
FI
FI
OD;
print( ( newline, "Found ", whole( count, 0 )
, " reversable primes up to ", whole( max number, 0 )
)
)
END
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 31 37 71
73 79 97 101 107 113 131 149 151 157
167 179 181 191 199 311 313 337 347 353
359 373 383 389
Found 34 reversable primes up to 500</pre>
=={{header|ALGOL W}}==
Line 191 ⟶ 233:
Found 34 reversable primes below 500
</pre>
=={{header|Arturo}}==
Line 508 ⟶ 551:
found 34 primes
done...</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang=text>
fastfunc reverse n .
while n > 0
r = r * 10 + n mod 10
n = n div 10
.
return r
.
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
fastfunc nextprim prim .
repeat
prim += 1
until isprim prim = 1
.
return prim
.
prim = 2
while prim < 500
if isprim reverse prim = 1
write prim & " "
.
prim = nextprim prim
.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 149 151 157 167 179 181 191 199 311 313 337 347 353 359 373 383 389
</pre>
=={{header|F_Sharp|F#}}==
Line 520 ⟶ 602:
2 3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 149 151 157 167 179 181 191 199 311 313 337 347 353 359 373 383 389
</pre>
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
Line 620 ⟶ 703:
<pre>
[2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97, 101, 107, 113, 131, 149, 151, 157, 167, 179, 181, 191, 199, 311, 313, 337, 347, 353, 359, 373, 383, 389]
</pre>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
local fn PrimeWhenReversed( n as NSUInteger ) as BOOL
if fn IsPrime(n) == NO then return NO
NSUInteger m = 0
while n
m *= 10
m += n % 10
n /= 10
wend
return fn IsPrime(m)
end fn = YES
local fn TestForReversablePrimes
NSUInteger i, count = 1
for i = 2 to 499
if fn PrimeWhenReversed( i ) == YES then count++ : printf @"%5u\b", i
if count == 11 then print : count = 1
next
end fn
fn TestForReversablePrimes
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
2 3 5 7 11 13 17 31 37 71
73 79 97 101 107 113 131 149 151 157
167 179 181 191 199 311 313 337 347 353
359 373 383 389
</pre>
Line 739 ⟶ 869:
359 373 383 389</pre>
=={{header|J}}==
<syntaxhighlight lang="j"> (#~ 1 p: |.&.":"0) i.&.(p:inv) 500
2 3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 149 151 157 167 179 181 191 199 311 313 337 347 353 359 373 383 389</syntaxhighlight>
=={{header|jq}}==
Line 823 ⟶ 956:
{{out}}
<pre>{2,3,5,7,11,13,17,31,37,71,73,79,97,101,107,113,131,149,151,157,167,179,181,191,199,311,313,337,347,353,359,373,383,389}</pre>
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- find primes whose reversed digits are also prime
local function isPrime( p )
if p <= 1 or p % 2 == 0 then
return p == 2
else
local prime = true
local i = 3
local rootP = math.floor( math.sqrt( p ) )
while i <= rootP and prime do
prime = p % i ~= 0
i = i + 2
end
return prime
end
end
local function reverseDigits( n )
return tonumber( string.reverse( tostring( n ) ) )
end
local count = 0
for n = 1,500 do
if isPrime( n ) then
if isPrime( reverseDigits( n ) ) then
count = count + 1
io.write( string.format( "%3d", n ), count % 10 == 0 and "\n" or " " )
end
end
end
io.write( "\nFound ", count, " reversable primes up to 500" )
end
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 31 37 71
73 79 97 101 107 113 131 149 151 157
167 179 181 191 199 311 313 337 347 353
359 373 383 389
Found 34 reversable primes up to 500
</pre>
=={{header|MAD}}==
Line 1,003 ⟶ 1,177:
{{out}}
<pre> 2 3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 149 151 157 167 179 181 191 199 311 313 337 347 353 359 373 383 389</pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
Type Tboolarr = array Of boolean;
Const MAX = 1000;
Function SieveOfEratosthenes(limit: Integer): Tboolarr;
Var
sieve: Tboolarr;
i, j: Integer;
Begin
SetLength(sieve, limit + 1);
sieve[2] := True;
For i := 3 To limit Do
sieve[i] := (i Mod 2 <> 0);
For i := 3 To Trunc(Sqrt(limit)) Do
Begin
If sieve[i] Then
Begin
j := i * i;
While j <= limit Do
Begin
sieve[j] := False;
Inc(j, 2 * i);
End;
End;
End;
SieveOfEratosthenes := sieve;
End;
Function ReverseNumber(number: Integer): Integer;
Var
reversed: Integer;
Begin
reversed := 0;
While number <> 0 Do
Begin
reversed := reversed * 10 + (number Mod 10);
number := number Div 10;
End;
ReverseNumber := reversed;
End;
Var
primes: Tboolarr;
i: Integer;
Begin
primes := SieveOfEratosthenes(MAX);
For i := 2 To 500 Do
Begin
If primes[i] And Primes[ReverseNumber(i)] Then
Write(i,' ');
End;
End.
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 149 151 157 167 179 181 191 199 311 313 337 347 353 359 373 383 389
</pre>
=={{header|Perl}}==
Line 1,193 ⟶ 1,427:
167 179 181 191 199 311 313 337 347 353 359 373 383 389
Reverse primes found: 34</pre>
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* FIND PRIMES THAT ARE STILL PRIME WHEN THEIR DIGITS ARE REVERSED */
/* CP/M BDOS SYSTEM CALL */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
/* I/O ROUTINES */
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
/* RETURNS TRUE IF N IS PRIME, FALSE OTHERWISE, USES TRIAL DIVISION */
IS$PRIME: PROCEDURE( N )BYTE;
DECLARE N ADDRESS;
DECLARE PRIME BYTE;
IF N < 3 THEN PRIME = N = 2;
ELSE IF N MOD 3 = 0 THEN PRIME = N = 3;
ELSE IF N MOD 2 = 0 THEN PRIME = 0;
ELSE DO;
DECLARE ( F, F2, TO$NEXT ) ADDRESS;
PRIME = 1;
F = 5;
F2 = 25;
TO$NEXT = 24; /* NOTE: ( 2N + 1 )^2 - ( 2N - 1 )^2 = 8N */
DO WHILE F2 <= N AND PRIME;
PRIME = N MOD F <> 0;
F = F + 2;
F2 = F2 + TO$NEXT;
TO$NEXT = TO$NEXT + 8;
END;
END;
RETURN PRIME;
END IS$PRIME;
REVERSE: PROCEDURE( N )ADDRESS; /* RETURNS THE REVERSED DIGITS OF N */
DECLARE N ADDRESS;
DECLARE ( R, V ) ADDRESS;
V = N;
R = V MOD 10;
DO WHILE( ( V := V / 10 ) > 0 );
R = ( R * 10 ) + ( V MOD 10 );
END;
RETURN R;
END REVERSE ;
/* FIND THE NUMBERS UP TO 500 */
DECLARE ( I, COUNT ) ADDRESS;
COUNT = 0;
DO I = 1 TO 500;
IF IS$PRIME( I ) THEN DO;
IF IS$PRIME( REVERSE( I ) ) THEN DO;
IF I < 10 THEN CALL PR$CHAR( ' ' );
IF I < 100 THEN CALL PR$CHAR( ' ' );
CALL PR$NUMBER( I );
IF ( COUNT := COUNT + 1 ) MOD 20 = 0 THEN CALL PR$NL;
ELSE CALL PR$CHAR( ' ' );
END;
END;
END;
EOF
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 149 151 157
167 179 181 191 199 311 313 337 347 353 359 373 383 389
</pre>
=={{header|Quackery}}==
Line 1,216 ⟶ 1,533:
<pre>2 3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 149 151
157 167 179 181 191 199 311 313 337 347 353 359 373 383 389
</pre>
=={{header|R}}==
<syntaxhighlight lang="R">
library(schoolmath) #to get prime numbers and to know if number is prime or not
n<- 500
all_primes_n <- primes(end=n)
reversed_primes_n = as.numeric(sapply(strsplit(as.character(all_primes_n),""),\(x){paste(rev(x),collapse="")}))
cat(all_primes_n[is.prim(reversed_primes_n)])
</syntaxhighlight>
'''Output'''
<pre>
1 2 3 5 7 11 13 17 31 37 71 73 79 97 101 107 113 131 133 149 151 157 167 179 181 191 199 301 311 313 337 347 353 359 373 383 389
</pre>
Line 1,331 ⟶ 1,664:
This program uses the words <code>RVSTR</code> and <code>BPRIM?</code>, respectively made to [[Reverse_a_string#RPL|revert a string]] and to [[Primality_by_trial_division#RPL|test primality by trial division]]
≪ { } 1 499 '''FOR''' n
'''IF''' n R→B '
'''IF''' n →STR
'''END'''
'''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
====Straightforward approach====
Numbers are here reversed without any string conversion.
{{works with|HP|49}}
« { }
1 499 '''FOR''' n
'''IF''' n ISPRIME? '''THEN'''
n 0
'''WHILE''' OVER '''REPEAT'''
SWAP 10 IDIV2 ROT 10 * +
'''END''' NIP
'''IF''' ISPRIME? '''THEN''' n + '''END'''
'''END'''
'''NEXT'''
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
Line 1,442 ⟶ 1,790:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
var reversed = Fn.new { |n|
Line 1,462 ⟶ 1,808:
}
System.print("Primes under 500 which are also primes when the digits are reversed:")
Fmt.tprint("$3d", reversedPrimes, 17)
System.print("\n%(reversedPrimes.count) such primes found.")</syntaxhighlight>
|