Project Euler question 36
I'm up to question 36 and I thought this would be simple. As usual, I am apparently wrong. I'm trying to do this in Python (because I don't know Python). My code is below. I'm getting 19 as the output, which is apparently incorrect. I don't see what I'm missing. Any suggestions (without correcting the code) would be appreciated. I don't want the correct answer or the code (or even an exact location of my error) - just a hint to get me in the right direction.
def isPolynomial(number):
if(str(number) == str(number)[::-1]):
return True
else:
return Fa开发者_C百科lse
def isBinaryPolynomial(number):
binNum = bin(number)
binStr = str(binNum)[2:]
revbinStr = binStr[::-1]
if(binStr == revbinStr):
return True
else:
return False
count = 0
for i in range(1, 1000001):
if isPolynomial(i):
if isBinaryPolynomial(i):
count += 1
print count
It looks like your code is correct, but you need to read carefully what it asks you to submit as the answer. I can't be any more specific without giving it away!
From http://projecteuler.net/index.php?section=problems&id=36
Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
The question asks for the sum of the numbers, not the count. Also this does not make a difference in the answer you get, but the word is "Palindrome", not "Polynomial"
# -*- coding: utf-8 -*- """ @author: neo """ def bin(x): result = '' x = int(x) while x > 0: mod = x % 2 x /= 2 result = str(mod) + result return result print sum(i for i in xrange(1,1000001)\ if str(i)==str(i)[::-1] and str(bin(i))==str(bin(i))[::-1])
精彩评论