Thursday, May 29, 2008

J and Project Euler p35

Problem definition:
------------------------------------------
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?
------------------------------------------

This time again solution in J language:



rot =: |: ". (i.@:#) ((({:,}:) ^: )) (@: ":)
ffac =: 0{|: @: q: @: rot
circ =: ((+/@: (ffac = rot)) = (#@:":))
tot =: +/ @: (circ"0) @: (2&+)
tot i.999998


No comments:

Post a Comment

Comment will be posted after comment moderation.
Thank you for your appreciation.