Find prime n such that reversed n is also prime: Difference between revisions
m (→{{header|REXX}}: added a stub.) |
(→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 24: | Line 24: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program counts/displays the number of reversed primes under a specified number N.*/ |
|||
<lang rexx></lang> |
|||
parse arg n cols . /*get optional number of primes to find*/ |
|||
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/ |
|||
if cols=='' | cols=="," then cols= 10 /* " " " " " */ |
|||
call genP copies(9, length(n) ) /*generate all primes under N. */ |
|||
w= 10 /*width of a number in any column. */ |
|||
if cols>0 then say ' index │'center(" reversed primes that are < " n, 1 + cols*(w+1) ) |
|||
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') |
|||
Rprimes= 0; idx= 1 /*initialize # of additive primes & idx*/ |
|||
$= /*a list of additive primes (so far). */ |
|||
do j=2 until j>=n; if \!.j then iterate /*Is J not a prime? No, then skip it.*/ |
|||
_= reverse(j); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/ |
|||
Rprimes= Rprimes + 1 /*bump the count of additive primes. */ |
|||
if cols<1 then iterate /*Build the list (to be shown later)? */ |
|||
$= $ right( commas(j), w) /*add the additive prime to the $ list.*/ |
|||
if Rprimes//cols\==0 then iterate /*have we populated a line of output? */ |
|||
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ |
|||
idx= idx + cols /*bump the index count for the output*/ |
|||
end /*j*/ |
|||
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
|||
say |
|||
say 'found ' commas(Rprimes) " reversed primes < " commas(n) |
|||
exit 0 /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
genP: parse arg h; @.=.; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; #= 7 |
|||
w= length(h); !.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1 |
|||
do j=@.7+2 by 2 while j<h /*continue on with the next odd prime. */ |
|||
parse var j '' -1 _ /*obtain the last digit of the J var.*/ |
|||
if _ ==5 then iterate /*is this integer a multiple of five? */ |
|||
if j // 3 ==0 then iterate /* " " " " " " three? */ |
|||
/* [↓] divide by the primes. ___ */ |
|||
do k=4 to # while k*k<=j /*divide J by other primes ≤ √ J */ |
|||
if j//@.k == 0 then iterate j /*÷ by prev. prime? ¬prime ___ */ |
|||
end /*k*/ /* [↑] only divide up to √ J */ |
|||
#= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/ |
|||
end /*j*/ |
|||
return</lang> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
index │ reversed primes that are < 500 |
|||
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
1 │ 2 3 5 7 11 13 17 31 37 71 |
|||
11 │ 73 79 97 101 107 113 131 149 151 157 |
|||
21 │ 167 179 181 191 199 311 313 337 347 353 |
|||
31 │ 359 373 383 389 |
|||
found 34 reversed primes < 500 |
|||
</pre> |
|||
{{out|output|text= when using the inputs: <tt> 10000 0 </tt> }} |
|||
<pre> |
|||
found 260 reversed primes < 10,000 |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
Revision as of 04:13, 20 March 2021
- Task
Find prime n for that reversed n is also prime.
Let 0 < n < 500
Phix
<lang Phix>function rp(integer p) return is_prime(to_integer(reverse(sprint(p)))) end function for n=500 to 1000 by 500 do
sequence res = filter(get_primes_le(n),rp) string r = join_by(apply(true,sprintf,{{"%3d"},res}),1,length(res)/2," ") printf(1,"%d reverse primes < %,d found:\n%s\n",{length(res),n,r})
end for</lang>
- Output:
34 reverse primes < 500 found: 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 56 reverse primes < 1,000 found: 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 701 709 727 733 739 743 751 757 761 769 787 797 907 919 929 937 941 953 967 971 983 991
REXX
<lang rexx>/*REXX program counts/displays the number of reversed primes under a specified number N.*/ parse arg n cols . /*get optional number of primes to find*/ if n== | n=="," then n= 500 /*Not specified? Then assume default.*/ if cols== | cols=="," then cols= 10 /* " " " " " */ call genP copies(9, length(n) ) /*generate all primes under N. */ w= 10 /*width of a number in any column. */ if cols>0 then say ' index │'center(" reversed primes that are < " n, 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') Rprimes= 0; idx= 1 /*initialize # of additive primes & idx*/ $= /*a list of additive primes (so far). */
do j=2 until j>=n; if \!.j then iterate /*Is J not a prime? No, then skip it.*/ _= reverse(j); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/ Rprimes= Rprimes + 1 /*bump the count of additive primes. */ if cols<1 then iterate /*Build the list (to be shown later)? */ $= $ right( commas(j), w) /*add the additive prime to the $ list.*/ if Rprimes//cols\==0 then iterate /*have we populated a line of output? */ say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ idx= idx + cols /*bump the index count for the output*/ end /*j*/
if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say say 'found ' commas(Rprimes) " reversed primes < " commas(n) exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? /*──────────────────────────────────────────────────────────────────────────────────────*/ genP: parse arg h; @.=.; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; #= 7
w= length(h); !.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1 do j=@.7+2 by 2 while j<h /*continue on with the next odd prime. */ parse var j -1 _ /*obtain the last digit of the J var.*/ if _ ==5 then iterate /*is this integer a multiple of five? */ if j // 3 ==0 then iterate /* " " " " " " three? */ /* [↓] divide by the primes. ___ */ do k=4 to # while k*k<=j /*divide J by other primes ≤ √ J */ if j//@.k == 0 then iterate j /*÷ by prev. prime? ¬prime ___ */ end /*k*/ /* [↑] only divide up to √ J */ #= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/ end /*j*/ return</lang>
- output when using the default inputs:
index │ reversed primes that are < 500 ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 2 3 5 7 11 13 17 31 37 71 11 │ 73 79 97 101 107 113 131 149 151 157 21 │ 167 179 181 191 199 311 313 337 347 353 31 │ 359 373 383 389 found 34 reversed primes < 500
- output when using the inputs: 10000 0
found 260 reversed primes < 10,000
Ring
<lang ring> load "stdlib.ring"
see "working..." + nl
row = 0 num = 0 limit = 500
for n = 1 to limit
strm = "" strn = string(n) for m = len(strn) to 1 step -1 strm = strm + strn[m] next strnum = number(strm) if isprime(n) and isprime(strnum) num = num + 1 row = row + 1 see "" + n + " " if row%10 = 0 see nl ok ok
next
see nl + "found " + num + " primes" + nl see "done..." + nl </lang>
- Output:
working... 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 primes done...
Wren
<lang ecmascript>import "/math" for Int import "/fmt" for Fmt import "/seq" for Lst
var reversed = Fn.new { |n|
var rev = 0 while (n > 0) { rev = rev * 10 + n % 10 n = (n/10).floor } return rev
}
var primes = Int.primeSieve(499) var reversedPrimes = [] for (p in primes) {
if (Int.isPrime(reversed.call(p))) reversedPrimes.add(p)
} System.print("Primes under 500 which are also primes when the digits are reversed:") for (chunk in Lst.chunks(reversedPrimes, 17)) Fmt.print("$3d", chunk) System.print("\n%(reversedPrimes.count) such primes found.")</lang>
- Output:
Primes under 500 which are also primes when the digits are reversed: 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 34 such primes found.