def palindromesBelow(cap): for x in [i for i in range(10) if i <= cap]: yield x end = '1' shouldContinue = True while shouldContinue: if int(end[0]) % 2 == 0 or int(end[-1]) % 2 == 0: end = str(int(end) + 1) continue shouldContinue = False for middle in ['', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']: val = int(end + middle + end[::-1]) if val <= cap: shouldContinue = True yield val end = str(int(end) + 1) primesSoFar = [2,3] def isPrime(n): global primesSoFar if n < 2: return False if n in primesSoFar: return True # compute the primes up to sqrt(n) next = primesSoFar[-1] + 2 while next * next <= n: isPrime = True for p in primesSoFar: if next % p == 0: isPrime = False if isPrime: primesSoFar.append(next) next = next + 2 return not any([n % x == 0 for x in primesSoFar]) def palindromePrimesBelow(cap): return sorted([x for x in palindromesBelow(cap) if isPrime(x)])
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question