JavaScript (E6) 95
Q=n=>(f=>{for(;a=!f;)for(f=++n==[...n+''].reverse().join('');b=n/++a|0,f&&a<=b;)f=a*b-n;})(0)|n
Ungolfed
Q=n=>
{
for (f=0; !f;) // loop until both palindrome and prime
{
f = ++n == [...n+''].reverse().join(''); // palindrome check
if (f) // if palindrome, check if prime
for(a = 1; b=n/++a|0, f && a<=b; )
f = a*b-n;
}
return n
}
Test In FireBFox/FireBug console
for (o=[],i=9;i<100000;)o.push(i=Q(i));console.log(o)
Output
[11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929, 10301, 10501, 10601, 11311, 11411, 12421, 12721, 12821, 13331, 13831, 13931, 14341, 14741, 15451, 15551, 16061, 16361, 16561, 16661, 17471, 17971, 18181, 18481, 19391, 19891, 19991, 30103, 30203, 30403, 30703, 30803, 31013, 31513, 32323, 32423, 33533, 34543, 34843, 35053, 35153, 35353, 35753, 36263, 36563, 37273, 37573, 38083, 38183, 38783, 39293, 70207, 70507, 70607, 71317, 71917, 72227, 72727, 73037, 73237, 73637, 74047, 74747, 75557, 76367, 76667, 77377, 77477, 77977, 78487, 78787, 78887, 79397, 79697, 79997, 90709, 91019, 93139, 93239, 93739, 94049, 94349, 94649, 94849, 94949, 95959, 96269, 96469, 96769, 97379, 97579, 97879, 98389, 98689, 1003001]
7This seems more like a general request which are meant for Stack Overflow. This site is more of a programming contest. But I can easily see this as a contest, give you remove the limitation of Python language and add the [tag:code-golf] tag – Optimizer – 2014-09-27T18:34:04.667
Do you copy ???? – Optimizer – 2014-09-27T18:43:37.797
1I edited your question to make it into a valid contest here. If you really just wanted to know how to shorten your code, that is not for this site. – Justin – 2014-09-27T19:06:07.543
ok thanks for the clarification. I will remove the limitation of Python – Kevin Eaverquepedo – 2014-09-27T20:28:32.743
The "working program" doesn't work. The value
s
will never change. – Ray – 2014-09-27T23:47:12.293True. I'll edit that. – Kevin Eaverquepedo – 2014-09-28T04:19:14.573
Can we assume that
n
is a positive integer? – Dennis – 2014-09-28T04:53:24.5671yes you can assume that – Kevin Eaverquepedo – 2014-09-28T08:49:40.977
You forgot a
def
beforegolf(n)
. – Beta Decay – 2014-09-29T17:28:45.777tue. needless to say my example code was horribly wrong! i'll edit it – Kevin Eaverquepedo – 2014-09-30T03:18:17.203