Find prime n such that reversed n is also prime: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Phix}}: extended output)
m (clarified task (decimal digits))
Line 2: Line 2:


;Task
;Task
Find prime '''n''' for that '''reversed n''' is also prime.
Find prime '''n''' for '''0 < n < 500''' which are also primes when the (decimal) digits are reversed.
<br> Let '''0 < n < 500'''


=={{header|Phix}}==
=={{header|Phix}}==

Revision as of 05:29, 20 March 2021

Find prime n such that reversed n is also prime is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Find prime n for 0 < n < 500 which are also primes when the (decimal) digits are reversed.

Phix

<lang Phix>function rp(integer p) return is_prime(to_integer(reverse(sprint(p)))) end function procedure test(sequence args)

   {integer n, string fmt} = args
   sequence res = apply(true,sprintf,{{"%3d"},filter(get_primes_le(n),rp)})
   string r = sprintf(fmt,{join_by(res,1,length(res)/2," ")})
   printf(1,"%d reverse primes < %,d found%s\n",{length(res),n,r})

end procedure papply({{500,":\n%s"},{1000,":\n%s"},{10000,"."}},test)</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

260 reverse primes < 10,000 found.

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

Library: Wren-math
Library: Wren-fmt
Library: Wren-seq

<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.