-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path036.py
More file actions
50 lines (40 loc) · 1.16 KB
/
036.py
File metadata and controls
50 lines (40 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# COMPLETED
"""
Problem 36
==========
The decimal number, 585 = 1001001001[2] (binary), is palindromic in both
bases.
Find the sum of all numbers, less than one million, which are palindromic
in base 10 and base 2.
(Please note that the palindromic number, in either base, may not include
leading zeros.)
Answer: 0e175dc2f28833885f62e7345addff03
"""
from common import check
import math
PROBLEM_NUMBER = 36
ANSWER_HASH = "0e175dc2f28833885f62e7345addff03"
def is_palindrome(text):
length = len(text)
i = 0
while i < length / 2.0:
a = text[i]
b = text[length - 1 - i]
if a != b:
return False
i = i + 1
return True
def to_binary(number):
bits = []
previous = number
while previous != 0:
current = int(math.floor(previous / 2))
remainder = previous - (current * 2)
bits.append(remainder)
previous = current
return "".join((str(c) for c in reversed(bits)))
total = 0
for i in range(1000000):
if is_palindrome(str(i)) and is_palindrome(to_binary(i)):
total += i
check(total, PROBLEM_NUMBER, ANSWER_HASH)