-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblems.py
63 lines (56 loc) · 1.94 KB
/
problems.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/user/bin/env python2
def problem_01():
# If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
# Find the sum of all the multiples of 3 or 5 below 1000.
sum = 0
for i in range(1, 1000):
if i % 3 == 0 or i % 5 == 0:
sum = sum + i
return sum
def problem_02():
# Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
# 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
# By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
sum = 0
prev = 1
curr = 1
while curr <= 4000000:
if curr % 2 == 0:
sum = sum + curr
next = prev + curr
prev = curr
curr = next
print sum
def problem_03():
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?
result = factorize(600851475143)
print result[-1]
def factorize(num):
factors = []
divisor = 2
while num != 1:
if num % divisor == 0:
num = num / divisor
factors.append(divisor)
else:
divisor = divisor + 1
return factors
def is_palindrome(num):
num = str(num)
if num[::-1] == num:
return True
else:
return False
def problem_04():
# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
# Find the largest palindrome made from the product of two 3-digit numbers.
current = 0
for multi1 in range(100,1000):
for multi2 in range(100,1000):
product = multi1 * multi2
if is_palindrome(product):
if current < product:
current = product
print current
print problem_04()