Find prime n such that reversed n is also prime

From Rosetta Code
Revision as of 17:59, 19 March 2021 by PureFox (talk | contribs) (Added Wren)
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 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

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.