Skip to content

Commit 2aa2542

Browse files
authored
Create problems.py
1 parent 4d8a3d4 commit 2aa2542

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Diff for: problems.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/user/bin/env python2
2+
3+
def problem_01():
4+
# 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.
5+
# Find the sum of all the multiples of 3 or 5 below 1000.
6+
sum = 0
7+
for i in range(1, 1000):
8+
if i % 3 == 0 or i % 5 == 0:
9+
sum = sum + i
10+
return sum
11+
12+
def problem_02():
13+
# 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:
14+
# 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
15+
# By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
16+
sum = 0
17+
prev = 1
18+
curr = 1
19+
while curr <= 4000000:
20+
if curr % 2 == 0:
21+
sum = sum + curr
22+
next = prev + curr
23+
prev = curr
24+
curr = next
25+
print sum
26+
27+
def problem_03():
28+
# The prime factors of 13195 are 5, 7, 13 and 29.
29+
# What is the largest prime factor of the number 600851475143 ?
30+
result = factorize(600851475143)
31+
print result[-1]
32+
33+
def factorize(num):
34+
factors = []
35+
divisor = 2
36+
while num != 1:
37+
if num % divisor == 0:
38+
num = num / divisor
39+
factors.append(divisor)
40+
else:
41+
divisor = divisor + 1
42+
return factors
43+
44+
def is_palindrome(num):
45+
num = str(num)
46+
if num[::-1] == num:
47+
return True
48+
else:
49+
return False
50+
51+
def problem_04():
52+
# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
53+
# Find the largest palindrome made from the product of two 3-digit numbers.
54+
current = 0
55+
for multi1 in range(100,1000):
56+
for multi2 in range(100,1000):
57+
product = multi1 * multi2
58+
if is_palindrome(product):
59+
if current < product:
60+
current = product
61+
print current
62+
63+
print problem_04()

0 commit comments

Comments
 (0)