## Emirps

### November 2, 2010

An emirp is a prime number that is also prime when its digits are reversed, and that is not also a palindrome. For instance, 13 is an emirp because its reversal, 31, is also prime; 23 is not an emirp, even though it is prime, because its reversal, 32, is not prime; and 101 is not an emirp, even though it is prime, because it is a palindrome.

Your task is to enumerate the emirps below a million; you should strive for maximum speed. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisements

Pages: 1 2

[…] Praxis – Emirps By Remco Niemeijer In today’s Programming Praxis, our task is to enumerate all the non-palindrome prime numbers that are still […]

My Haskell solution (see http://bonsaicode.wordpress.com/2010/11/02/programming-praxis-emirps/ for a version with comments):

Here is my Haskell solution: http://www.gleocadie.net/?p=178&lang=en

Comments, advices are welcomed.

I’m loving this website! Thanks a lot for the problems.

I’m loving this website, thanks a lot!

Haskell:

Love the site.

Sorry, line 14 should read

if prime(int(str(n)[::-1])) and str(n) != str(n)[::-1]:

@Joe: your prime() function isn’t quite correct. For instance, prime(10) returns True.

Wrote it in python (though for speed, a different language may be better). In the interest of speed, I wrote a function called reverse(n) that writes n backwards using only numerical operations (avoiding strings). Interestingly, trying to memoize my is_prime() function made everything slower (perhaps due to memory usage?). Running ./emirps.py 1000000 took just under 15 seconds on my aging laptop. For more speed I used pypy (Python with a just in time compiler); it finished in just under 3 seconds.

Changing line 36 in main(n) from

to

yields a decent speed up; normal execution finishes in just under 11 seconds, while pypy finishes it in under 2.

Probably if you avoid using math.log10(n), you’ll end up with a faster execution.

My C Implementation

http://codepad.org/xVdzeVs6

This one was easy one. ;)

My C Implementation

http://codepad.org/xVdzeVs6

This one was easy one. ;)

We can build on the sieve of Erastosthenes exercise to create a list of primes < 1,000,000. Since the output array of the sieve is sorted, we can use binary search on the table for O(log n) search of the sieve, when checking for whether each reversal is prime. The Factor code for sieve is already posted on this blog and won't be reproduced here.

USING: kernel sequences vectors math math.parser locals

binary-search sieve ;

IN: emirp

: emirp? ( n vec — ? )

swap

number>string dup reverse 2dup =

[ 3drop f ]

[ nip string>number swap sorted-member? ] if ;

:: emirp-filter ( primes — semirp )

V{ } clone

primes

[ dup primes emirp?

[ suffix ]

[ drop ] if

] each ;

: Semirp ( n — vec )

primes emirp-filter ;

Factor session:

( scratchpad ) 100000 Semirp

— Data stack:

V{ 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199…

( scratchpad ) length .

1646

Sorry, somehow the code block didn’t work on last post, probably got a tag wrong somewhere…

We can build on the sieve of Erastosthenes exercise to create a list of primes < 1,000,000. Since the output array of the sieve is sorted, we can use binary search on the table for O(log n) search of the sieve, when checking for whether each reversal is prime. The Factor code for sieve is already posted on this blog and won't be reproduced here.

USING: kernel sequences vectors math math.parser locals

binary-search sieve ;

IN: emirp

: emirp? ( n vec — ? )

swap

number>string dup reverse 2dup =

[ 3drop f ]

[ nip string>number swap sorted-member? ] if ;

:: emirp-filter ( primes — semirp )

V{ } clone

primes

[ dup primes emirp?

[ suffix ]

[ drop ] if

] each ;

: Semirp ( n — vec )

primes emirp-filter ;

Factor session:

( scratchpad ) 100000 Semirp

— Data stack:

V{ 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199…

( scratchpad ) length .

1646

ruby solution : http://codepad.org/0ZlE1j4F

November 2nd, 2010.c:The solution interpreted using Leonardo IDE 3.4.1 on an Apple Power Mac G4 (AGP Graphics) (450MHz processor, 1GB memory) running Mac OS 9.2.2 (International English).

“November 2nd, 2010 (A).out” & “November 2nd, 2010 (B).out”:I interpreted the instruction that I “should strive for maximum speed” as an invitation to compare two functions for reversing the digits of an integer. The results were surprising: the function

`f_ReverseDigits_B`

, which reverses the digits of an integer by converting that integer into a string, reversing the characters of that string, and converting that string back into an integer, was as fast – and on occasion faster and on no occasion slower – than the function`f_ReverseDigits_A`

, which reverses the digits of an integer arithmetically.(I’m just trying to solve the problems posed by this ‘site whilst I try to get a job; I’m well aware that my solutions are far from the best – but, in my defence, I don’t have any traditional qualifications in computer science :/ )