algorithm - Palindrome/Prime Checker in Python not working -


my task this:

what (double-digit, or above) prime number palindromic in both base-2 (binary), , base-10 (decimal) instances?

whenever run code nothing happens, , acts infinite loop. doing wrong, or how can improve? many in advance.

def isprime(n):     if type(n) != int or n <= 1:         return false     elif n == 2:         return true     elif n%2 == 0:         return false     else:         x in range(2, int(n**0.5)+1):             if n%x == 0:                 return false                 break         return true def ispalindrome(x):     num = str(x)[::-1]     if str(x) == num:         return true     else:         return false while true:     = 11     if isprime(a) , ispalindrome(a) == true:         if ispalindrome(bin(a)) == true:             print             break     else:         a+=2         print 

--------- edit: **solved** ---------

revised code:

def isprime(n):     if n < 2 or n%2 == 0:         return false     if n == 2:         return true     else:         x in range(3, int(n**0.5)+1, 2):             if n%x == 0:                 return false         return true def ispalindrome(x):     num = str(x)[::-1]     return str(x) == num = 11 while true:     if isprime(a) , ispalindrome(a) , ispalindrome(format(a, "b")):             print             break     a+=2 

thank contributed answers.

the output of bin() never palindrome, because characters 0b prepended it:

>>> bin(3) '0b11' >>> ispalindrome(bin(3)) false 

either slice 2 characters of, or use format() produce binary representation:

>>> format(3, 'b') '11' >>> ispalindrome(format(3, 'b')) true 

your loop has problem: won't increment if found palindromic prime not palindromic prime in base 2:

if isprime(a) , ispalindrome(a) == true:     if ispalindrome(format(a, 'b')) == true:         print         break     # no else here else:     a+=2     print 

just test conditions in 1 test:

if isprime(a) , ispalindrome(a) , ispalindrome(format(a, 'b')):     print     break += 2 

there rarely need test == true in boolean test.

next reset a in loop:

while true:     = 11 

it doesn't matter a in rest of loop, @ top goes 11. move out of loop:

a = 11 while true:     if isprime(a) , ispalindrome(a) , ispalindrome(format(a, 'b')):         print         break     += 2 

whenever write if comparison: return true followed else: return false, just return test instead:

def ispalindrome(x):     num = str(x)[::-1]     return str(x) == num 

because == comparison operator returns boolean value.

your primality test doesn't need test type of n, (you ever pass in integers), correct way test type use:

isinstance(n, int) 

as allows subclasses of int too. if do need limit specific type , cannot allow subclasses, you'd use:

type(n) int 

as types singletons.

taking advice isprime function python language isprime() function little faster if used:

def isprime2(n):     if n < 2: return false     if n == 2: return true     in range(3, int(n ** 0.5) + 1, 2):   # odd numbers         if n % == 0:             return false     return true 

instead.

your loop made more efficient limiting number of values at. if @ palindronic prime series on oeis you'll see apart 11, all base-10 palindromic primes have odd number of digits, can skip large swathes of numbers. following produce better candidates:

def palindromic_candidates():     yield 11     outer = 1     while true:         in range(10):             yield int('{}{}{}'.format(outer, i, str(outer)[::-1]))         outer += 1 

demo:

>>> itertools import islice >>> pc = palindromic_candidates() >>> list(islice(pc, 30)) [11, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353, 363, 373, 383] >>> list(islice(pc, 100, 130)) [13931, 14041, 14141, 14241, 14341, 14441, 14541, 14641, 14741, 14841, 14941, 15051, 15151, 15251, 15351, 15451, 15551, 15651, 15751, 15851, 15951, 16061, 16161, 16261, 16361, 16461, 16561, 16661, 16761, 16861] 

use instead of while true loop, remove a += 2 line:

for in palindromic_candidates():     if isprime(a) , ispalindrome(format(a, 'b')):         print         break 

where can drop test base-10 palindromes generator produces only palindromes.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -