Palindromic prime

8

I'm solving task, where:

Input:

A number as an integer.

Output:

The closest greater palindromic prime as an integer.

I would appreciate hints how to make my solution shorter. Or directions if change of approach would be beneficial.

golf=lambda l,r=range:next(x for x in r(10**6)if(x==int(`x`[::-1]))&all(x%d for d in r(2,x))and x>l)

palindromic:

x==int(`x`[::-1])

prime:

all(x%d for d in r(2,x))

Martin Rajnoha

Posted 2014-07-09T08:31:55.070

Reputation: 91

3

Before people close vote this, because it's not a challenge: things like this are on-topic now. The only thing I'm wondering is if this wasn't better suited as two separate questions "How can I make checking for palindromic numbers shorter?" and "How can I make checking for primes shorter?" (@Martin, don't do that though, until you get more feedback confirming this.)

– Martin Ender – 2014-07-09T08:40:28.140

@m.buettner, ok thanks. – Martin Rajnoha – 2014-07-09T08:47:45.387

Answers

7

Now, for some really big improvements. Your original program was 97 characters:

n=lambda l,r=range:next(x for x in r(10**6)if(x==int(`x`[::-1]))&all(x%d for d in r(2,x))and x>l)

With the improvements below, you could get to 90 characters:

n=lambda l,r=range:min(x for x in r(l+1,10**6)if(`x`==`x`[::-1])&all(x%d for d in r(2,x)))

With a trick to eliminate the parentheses around the palindrome checking statement, you can get to 87:

n=lambda l,r=range:min(x for x in r(l+1,10**4)if`x`==`x`[::-1]*all(x%d for d in r(2,x)))

However, I have a solution in 80 characters. To get it, focus on changing the big picture, not the individual components. Rethink your most basic choices in getting to this point. Why a lambda? Why a min of a filtered range? There may be a better way.


Using int() in the palindrome comparison is a lot of characters - can you see a shorter way to turn

x

and

`x`[::-1]

into the same type?

Also, that and x>l bit at the end is a lot of characters. Is there a way we could shorten it? Eliminate the need for it by changing something else?

Is next the right function for the job? Remember, this is code golf, runtime is irrelevant.

The prime checking function looks spot on, though.

Also, and this is merely aesthetic, I don't like aliasing functions, the way you are doing with range, when it doesn't save any characters, as in this case.

isaacg

Posted 2014-07-09T08:31:55.070

Reputation: 39 268

int for x==x[::-1], next for index [0] ...still thinking about and x>l – Martin Rajnoha – 2014-07-09T12:27:41.853

@MartinRajnoha HINT: take a deeper look into the range function. – BeetDemGuise – 2014-07-09T15:03:10.860

Thx, guys I found that :) Current status -10chars. Great! – Martin Rajnoha – 2014-07-09T15:43:36.727

@MartinRajnoha As a quick reference point, I was able (in my short time golfing down your code) to remove 12 characters. – BeetDemGuise – 2014-07-09T17:05:42.013

@BeetDemGuise now you are teasing me :) OK, how did u manage that? – Martin Rajnoha – 2014-07-09T21:28:42.927

@MartinRajnoha I have a solution with 17 less characters. See edited answer for hints. – isaacg – 2014-07-10T11:50:15.383

@MartinRajnoha ``` is your friend. – ɐɔıʇǝɥʇuʎs – 2014-07-10T11:56:33.397