Reputation: 15
my question today is if I am going down on the right path for Euler 145 and if it is sorta kinda efficient. I have most of it down, just one of my Defs is giving me troubles with int(str(numb)[:i])%2==0 for a even check. my code is below. Line 10 is the issue spot
def reversed(reg): # to flip the number around
fliped = str(reg)[::-1];
return(int(fliped)); # Return it as a int.
def allEvenDigits(numb): # This is the issue one
hasEvenNumb = False;
for i in range(0, len(str(numb))):
if int(str(numb)[:i])%2 == 0: # if int of the string numb's char at i is even
hasEvenNumb = True; ## return that it is true
break; # why go on if we found a even.
return(hasEvenNumb);
for i in range(1, 1000): # its 1000 to save a few minutes
revNumb = reversed(i);
total = revNumb+i;
if(allEvenDigits(total)):
print(i, "+" , revNumb, "=",Total);
Upvotes: 0
Views: 1208
Reputation: 1
def sumrevers(x):
summation = x + int(str(x)[::-1])
if summation % 2 != 0: return summation
def checknum(x):
if not (str(x)[-1] == "0") or (str(x)[0] == "0"):
if type(sumrevers(x)) == int:
num = str(sumrevers(x))
checklis = [k for k in str(num)]
if all(int(i) % 2 != 0 for i in checklis): return True
cnt = 0
for i in xrange(1, 1000000001):
if checknum(i):
cnt += 1
print cnt
Upvotes: 0
Reputation: 250951
You can use the built-in function all()
and use a set to keep a track of numbers that has been solved already; for example if you've solved 36
then there's no reason to solve 63
:
seen = set()
def allEvenDigits(numb): # This is the issue one
return all( int(n)%2 == 0 for n in str(numb))
for i in range(1, 1000): # its 1000 to save a few minutes
revNumb = reversed(i);
total = revNumb+i;
if i not in seen and revNumb not in seen:
if (allEvenDigits(total)):
print(i, "+" , revNumb, "=",total);
seen.add(i)
seen.add(revNumb)
output:
(1, '+', 1, '=', 2)
(2, '+', 2, '=', 4)
(3, '+', 3, '=', 6)
(4, '+', 4, '=', 8)
(11, '+', 11, '=', 22)
(13, '+', 31, '=', 44)
(15, '+', 51, '=', 66)
(17, '+', 71, '=', 88)
(22, '+', 22, '=', 44)
(24, '+', 42, '=', 66)
(26, '+', 62, '=', 88)
(33, '+', 33, '=', 66)
(35, '+', 53, '=', 88)
(44, '+', 44, '=', 88)
...
help on all
:
>>> all?
Type: builtin_function_or_method
String Form:<built-in function all>
Namespace: Python builtin
Docstring:
all(iterable) -> bool
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
Upvotes: 1
Reputation: 3483
You're starting with an empty string when your range is range(0, len(str(numb)))
. You could solve it with:
def allEvenDigits(numb): # This is the issue one
hasEvenNumb = False;
for i in range(1, len(str(numb))):
if int(str(numb)[:i])%2 == 0: # if int of the string numb's char at i is even
hasEvenNumb = True; ## return that it is true
break; # why go on if we found a even.
return(hasEvenNumb);
>>> allEvenDigits(52)
False
It seems, however, that the easier thing to do would be to check if each number is even:
def allEvenDigits(numb):
hasEvenNumb = True
for char in str(numb):
if int(char) % 2 == 0:
hasEvenNumb = False
break
return hasEvenNumb
allEvenDigits(52)
Makes it a little more straightforward, and checks only the individual digit rather than a substring.
Upvotes: 0