MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_036/sol1.py:39–65  ·  view source on GitHub ↗

Return the sum of all numbers, less than n , which are palindromic in base 10 and base 2. >>> solution(1000000) 872187 >>> solution(500000) 286602 >>> solution(100000) 286602 >>> solution(1000) 1772 >>> solution(100) 157 >>> solution(10) 25 >>

(n: int = 1000000)

Source from the content-addressed store, hash-verified

37
38
39def solution(n: int = 1000000):
40 """Return the sum of all numbers, less than n , which are palindromic in
41 base 10 and base 2.
42
43 >>> solution(1000000)
44 872187
45 >>> solution(500000)
46 286602
47 >>> solution(100000)
48 286602
49 >>> solution(1000)
50 1772
51 >>> solution(100)
52 157
53 >>> solution(10)
54 25
55 >>> solution(2)
56 1
57 >>> solution(1)
58 0
59 """
60 total = 0
61
62 for i in range(1, n):
63 if is_palindrome(i) and is_palindrome(bin(i).split("b")[1]):
64 total += i
65 return total
66
67
68if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 2

splitMethod · 0.80
is_palindromeFunction · 0.70

Tested by

no test coverage detected