Circular primes

3

Given a number determine if it is a circular prime.

A circular prime is a prime where if you rotate the digits you still get prime numbers. 1193 is circular prime because 1931 and 9311 and 3119 are all primes.

Input:          Output:
1193            circular
11939           circular
1456            nothing
193939          circular
1111111         nothing
7               circular
23              nothing

Shortest code wins.

Quillion

Posted 2013-11-12T18:00:32.443

Reputation: 809

1I don't think this should have the code-challenge tag. – Cruncher – 2013-11-12T19:18:38.223

@Cruncher Wouldn't solving such problem in shortest possible code require creative thinking? If it is not code-challenge then I will gladly remove it, just assumed it to be challenge. – Quillion – 2013-11-12T19:34:47.560

1

Not a big deal, but they're sort of disjoint. http://codegolf.stackexchange.com/tags/code-challenge/info "A code challenge is a competition for creative ways to solve a programming puzzle for an objective criterion other than code size."

– Cruncher – 2013-11-12T19:36:59.323

@Cruncher sorry about that. Thanks for clarifying it for me :) – Quillion – 2013-11-12T19:42:20.000

4-1 You stated: "Shortest code wins" but awarded the check to a submission that was 4 times as long as the shortest submitted! – DavidC – 2013-11-14T19:50:02.533

@DavidCarraher I asked for specific output. I do not see the shortest answer provide such output. Try running some programs and see if input and output matches for what is requested. – Quillion – 2013-11-14T20:37:31.617

You must be kidding. – DavidC – 2013-11-14T20:56:30.090

@DavidCarraher please do enlighten me which is the answer whose's code if I copy paste and run will yield the output on the given input specified above? – Quillion – 2013-11-14T21:44:34.203

1My own code returns True if a number is circular, False if a number is not circular. I find that a reasonable alternative to "circular" and "nothing". The shorter solution in J appears to follow the same Boolean convention (True, False). – DavidC – 2013-11-14T22:05:55.750

4@DavidCarraher Some problems on this site require specific output. This is one. Deal with it and stop whining. "Oh, poor me, my output 99,98,97... is equivalent to the lyrics of '99 bottles of beer on the wall' but it wasn't an accepted answer!" – boothby – 2013-11-14T22:13:41.640

1@Boothby. Bad example. "99 bottles of beer on the wall" is all about crafting a specific output, the lyrics of a song. The present example is advertised a test of circularity of a number: "Given a number determine if it is a circular prime." Example output was given. But it was not stated (nor should it have been, in my view) that that specific format is required). – DavidC – 2013-11-14T22:21:10.260

1@DavidCarraher, It most certainly is stated. despite your wishes to the contrary. Look at the question, and see "Input" and "Output". This is a 'compound question': solve a challenge and format the output. – boothby – 2013-11-15T02:42:56.227

Answers

2

GolfScript, 47 characters

:s,,{)s/(+~:P{(.P\%}do(},!"nothing
circular"n/=

Input must be given on STDIN without trailing newline (online example).

Howard

Posted 2013-11-12T18:00:32.443

Reputation: 23 109

It's beautiful :) – Quillion – 2013-11-15T15:04:31.217

6

J, 47 chars (w/ formatted output)

('nothing';'circular'){~*/1&p:".(|."0 1~i.&#)":

J, 23 chars (boolean only)

*/1&p:".(|."0 1~i.&#)":

": is a string conversion; i.&# produces a range of integers [0,len), and |."0 1~ is a rotation of the string by each successive integer of the range.

". converts the list of rotations back into numbers, 1&p: converts the list of numbers into booleans (i.e. "prime?" predicate), and */ is a multiply reduce over the booleans (i.e. and).

rationalis

Posted 2013-11-12T18:00:32.443

Reputation: 456

Wow I really like this :) nicely done – Quillion – 2013-11-13T13:56:12.457

Add an {'nothing';'circular' and beat the Python submission :) – marinus – 2013-11-14T23:46:55.313

1@marinus Done, though it really hurts my sense of aesthetics... The interface between the human and the program is always the least fun to code. – rationalis – 2013-11-15T04:03:51.127

Months after the fact, I finally just realized I could replace [:i.# with i.&#, which was bugging me at the time... L'esprit_de_l'escalier. Take that, GolfScript!

– rationalis – 2014-08-14T08:41:28.127

5

Python, 106 chars

p=input();n=`p`
for i in n:
 n=n[1:]+n[0];e=2
 while`e`!=n:p*=int(n)%e;e+=1
print'cniortchuilnagr'[p<1::2]

Daniel Lubarov

Posted 2013-11-12T18:00:32.443

Reputation: 301

I see what you did there with the answer :) very clever – Quillion – 2013-11-13T15:29:10.537

2Oh man! I just did mine in Python and I came up with "cniortchuilnagr" independently. – danmcardle – 2013-11-14T03:32:30.890

1

Whoa! I got the idea from http://codegolf.stackexchange.com/a/406/9395

– Daniel Lubarov – 2013-11-14T03:38:25.753

3You can make this both shorter and correct if you use for i in n... otherwise, this will fail to rule out certain primes with more than 10 digits. – boothby – 2013-11-14T19:02:54.443

@boothby good idea, thanks! – Daniel Lubarov – 2013-11-14T19:47:11.397

5

Mathematica 62 61

f@s_ := And @@ PrimeQ[FromDigits@StringRotateLeft[s, #] & /@ Range@9]

Usage

f["1193"]

True

f["11939"]

True

f["1456"]

False

f["193939"]

True

f["1111111"]

False

f["7"]

True

f["23"]

False

DavidC

Posted 2013-11-12T18:00:32.443

Reputation: 24 524

1You can use FromDigits instead of ToExpression. – alephalpha – 2013-11-13T02:27:05.060

I had no idea that FromDigits could handle strings! – DavidC – 2013-11-13T02:34:32.727

3

Ruby, 110 109 chars

require'mathn'
x=gets.chop.split''
puts (0..x.size).all?{|n|x.rotate(n).join.to_i.prime?}?:circular:'nothing'

Doorknob

Posted 2013-11-12T18:00:32.443

Reputation: 68 138

1You can shorten 1 character by using Symbol instead of String for :circular. – manatwork – 2013-11-15T09:31:12.657

3

Mathematica, 75 64

And@@PrimeQ[FromDigits/@NestList[RotateLeft,IntegerDigits@#,9]]&

Marcks Thomas

Posted 2013-11-12T18:00:32.443

Reputation: 141

You can save a few characters by using RotateRight or RotateLeft to rotate the digits. – DavidC – 2013-11-13T00:45:04.863

@DavidCarraher: Thanks, it saves quite a few, actually. How at one point I must have thought my elaborate Part trick would be shorter, I find most puzzling, but even now, you're still a good three characters ahead. – Marcks Thomas – 2013-11-13T13:13:23.827

1

R, 139

a=scan()
p=0
A=nchar(a)
S=substr
for(i in 1:A){b=as.integer(paste0(S(a,i+1,A),S(a,1,i)));p=p+!sum(!b%%1:b)<3}
c('nothing','circular')[!p+1]

plannapus

Posted 2013-11-12T18:00:32.443

Reputation: 8 610

1

Python3.3, 160C

def m():
 s=input()
 r=range
 for i in r(len(s)):
  a=int(s)
  if all(a%i for i in r(2,a)):return 0
  s=s[1:]+s[:1]
 return 1 
print("cniortchuilnagr"[m()::2])

danmcardle

Posted 2013-11-12T18:00:32.443

Reputation: 695

1

Smalltalk, 195 chars

[:x|r:=[:n|(n\\10)*(10 raisedTo:n log floor)+(n//10)].s:=OrderedCollection with:x.[(m:=r value:s last)=x]whileFalse:[s add:m].^(s allSatisfy:[:each|each isPrime])ifTrue:['circular']ifFalse:['nothing']]

formatted

[:x|
  r:=[:n|(n\\10)*(10 raisedTo:n log floor)+(n//10)].
  s:=OrderedCollection with:x.
  [(m:=r value:s last)=x]whileFalse:[s add:m].
  ^(s allSatisfy:[:each|each isPrime])ifTrue:['circular']ifFalse:['nothing']]

Paul Richter

Posted 2013-11-12T18:00:32.443

Reputation: 770

1

APL, 53

'nothing' 'circular'[1+^/{⌊/⍵|⍨1↓⍳⍵-1}¨⍎¨{⍵⌽∆}¨⍳⍴∆←⍞]

protist

Posted 2013-11-12T18:00:32.443

Reputation: 570