Skip to content

Commit 14dabd7

Browse files
author
Jessica Yung
committed
test(euler): add doctests to p1-4, 6 and refactored code.
ISSUE: p5 and p7 solutions incomplete.
1 parent af47b75 commit 14dabd7

File tree

6 files changed

+154
-139
lines changed

6 files changed

+154
-139
lines changed

mathematics/project-euler/1_multiples_of_three_and_five.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@
33
44
Find the sum of all multiples of 3 or 5 below a positive integer n
55
6+
Examples:
7+
>>> sum_multiples_3_5(10)
8+
23
9+
10+
>>> sum_multiples_3_5(-1)
11+
0
12+
13+
Author: Jessica Yung
14+
Refactored October 2016
615
"""
716

8-
# Function calculates sum of multiples of k less than n
9-
def sum_multiples(n,k):
17+
18+
def sum_multiples(n, k):
19+
"""Calculates sum of multiples of k less than n."""
1020
sum = 0
1121
if n >= k:
12-
for i in range(1,int(n/k)):
22+
for i in range(1, int(n/k)):
1323
sum += k*i
1424
# It should be range(1,int(n/k)+1) for k such that n % k != 0, so
1525
if n % k != 0:
1626
sum += int(n/k)*k
1727
return sum
1828

19-
# Function calculates sum of all multiples of 3 or 5 below n
20-
def sum_multiples_3_5(n):
21-
return sum_multiples(n,3) + sum_multiples(n,5) - sum_multiples(n,15)
2229

23-
# Read input and solve
24-
t = int(input().strip())
25-
for j in range(t):
26-
n = int(input().strip())
27-
print(sum_multiples_3_5(n))
30+
def sum_multiples_3_5(n):
31+
"""Calculates sum of all multiples of 3 or 5 below n."""
32+
return sum_multiples(n, 3) + sum_multiples(n, 5) - sum_multiples(n, 15)

mathematics/project-euler/2_even_fibonacci_numbers.py

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,25 @@
33
44
Calculate the sum of all even Fibonacci numbers below positive integer n
55
6+
Example:
7+
>>> sum_even_fibs_under_n(10)
8+
10
9+
10+
This file also includes a function that returns the nth Fibonacci number:
11+
>>> fib(0)
12+
1
13+
14+
>>> fib(1)
15+
1
16+
17+
>>> fib(10)
18+
89
19+
20+
21+
Author: Jessica Yung
22+
Refactored October 2016
623
"""
7-
import unittest
24+
825

926
def fib(n):
1027
"""Function to calculate nth Fibonacci number, n being a non-negative integer.
@@ -38,37 +55,3 @@ def sum_even_fibs_under_n(n):
3855
return sum
3956
else:
4057
return "Null"
41-
42-
# Tests
43-
44-
class Tests(unittest.TestCase):
45-
"""Tests fib(n)."""
46-
47-
def __init__(self):
48-
super(Tests, self).__init__()
49-
self.nth_fibonacci_test()
50-
self.sum_even_fibs_under_n_test()
51-
52-
53-
def nth_fibonacci_test(self):
54-
# type: () -> Error or not?
55-
self.assertEqual(fib(0), 1)
56-
self.assertEqual(fib(1), 1)
57-
self.assertEqual(fib(4), 5)
58-
59-
def sum_even_fibs_under_n_test(self):
60-
self.assertEqual(sum_even_fibs_under_n(10), 10)
61-
self.assertEqual(1, 0)
62-
63-
if __name__ == '__main__':
64-
unittest.main()
65-
# Tests.nth_fibonacci_test()
66-
# Tests.sum_even_fibs_under_n_test()
67-
68-
"""
69-
# Read input and solve
70-
t = int(input().strip())
71-
for i in range(t):
72-
n = int(input().strip())
73-
print(sum_even_fibs_under_n(n))
74-
"""
Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,60 @@
11
"""
2-
Problem 3:
2+
Problem 3:
33
4-
Find the largest prime factor of a positive integer n
4+
Find the largest prime factor of a positive integer n.
55
6+
Example:
7+
>>> largest_prime_factor(10)
8+
5
9+
10+
Author: Jessica Yung
11+
Refactored October 2016
612
"""
713
import math
14+
import unittest
15+
816

9-
# Function to find all factors
1017
def factors(n):
11-
factors = set()
12-
for i in range(1, int(math.sqrt(n))+1):
13-
if n % i == 0:
14-
factors.add(i)
15-
factors.add(int(n/i))
16-
return factors
17-
18-
# Function that tests if integers are prime
19-
def isprime(n):
20-
isprime = True
21-
if n < 2:
22-
isprime = False
23-
elif n > 3:
24-
for i in range(2, int(math.sqrt(n))+1):
25-
if n % i == 0:
26-
isprime = False
27-
break
28-
return isprime
29-
30-
# Function that finds largest prime factor
18+
"""Find all factors of a positive integer n."""
19+
factors_of_n = set()
20+
for i in range(1, int(math.sqrt(n))+1):
21+
if n % i == 0:
22+
factors_of_n.add(i)
23+
factors_of_n.add(int(n / i))
24+
return factors_of_n
25+
26+
27+
def is_prime(n):
28+
"""Tests if a positive integer n is prime."""
29+
is_n_prime = True
30+
if n < 2:
31+
is_n_prime = False
32+
elif n > 3:
33+
for i in range(2, int(math.sqrt(n))+1):
34+
if n % i == 0:
35+
is_n_prime = False
36+
break
37+
return is_n_prime
38+
39+
3140
def largest_prime_factor(n):
32-
primefactors = set()
33-
for i in factors(n):
34-
if isprime(i):
35-
primefactors.add(i)
36-
if len(primefactors) == 0:
37-
return "Null"
38-
else:
39-
return max(primefactors)
40-
41-
# Read input and solve
42-
t = int(input().strip())
43-
for i in range(t):
44-
n = int(input().strip())
45-
print(largest_prime_factor(n))
41+
"""Finds largest prime factor of a positive integer n."""
42+
prime_factors = set()
43+
for i in factors(n):
44+
if is_prime(i):
45+
prime_factors.add(i)
46+
if len(prime_factors) == 0:
47+
return "Null"
48+
else:
49+
return max(prime_factors)
50+
51+
52+
class Tests(unittest.TestCase):
53+
54+
def test_largest_prime_factor(self):
55+
self.assertEqual(largest_prime_factor(10), 5)
56+
self.assertEqual(largest_prime_factor(1), "Null")
57+
58+
59+
if __name__ == '__main__':
60+
unittest.main()

mathematics/project-euler/4_largest_palindrome_product.py

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,50 @@
33
44
Find the largest palindrome made from the product of two 3-digit integers which is less than positive integer n.
55
6-
"""
6+
Examples:
7+
>>> greatest_palindromic_product_below_n(10000)
8+
0
79
8-
# Function to test if a number is a palindrome
9-
def is_palindrome(n):
10-
is_palindrome = True
11-
n = str(n)
12-
for i in range(int(len(n)/2)):
13-
if n[i] != n[len(n)-1-i]:
14-
is_palindrome = False
15-
break
16-
return is_palindrome
17-
18-
# Narrow range
19-
def largest_three_digit_to_consider(n):
20-
k = 999
21-
while k*100 > n:
22-
k -= 1
23-
return k
10+
>>> greatest_palindromic_product_below_n(111112)
11+
111111
2412
13+
Author: Jessica Yung
14+
Refactored October 2016
2515
"""
26-
Given the palindromic product must be a six-digit number, it must also be a multiple of 11. Thus at least one of the factors must be a multiple of 11.
2716

28-
"""
17+
def is_palindrome(number):
18+
"""Test if a number is a palindrome."""
19+
is_a_palindrome = True
20+
number = str(number)
21+
for i in range(int(len(number) / 2)):
22+
if number[i] != number[len(number) - 1 - i]:
23+
is_a_palindrome = False
24+
break
25+
return is_a_palindrome
26+
27+
28+
# Narrow down range
29+
def largest_three_digit_to_consider(n):
30+
k = 999
31+
while k * 100 > n:
32+
k -= 1
33+
return k
34+
2935

30-
# Systematically go through all relevant products and find the greatest palindrome product
3136
def greatest_palindromic_product_below_n(n):
32-
k = largest_three_digit_to_consider(n)
33-
l = int(k/11)
34-
greatest = 0
35-
# Let a be the factor that must be a multiple of 11
36-
for i in range(10, l+1):
37-
a = 11*i
38-
for j in range(100,k):
39-
product = a*j
40-
if product < n:
41-
if product > greatest:
42-
if is_palindrome(product):
43-
greatest = product
44-
return greatest
45-
46-
# Read input and solve
47-
t = int(input().strip())
48-
for i in range(t):
49-
n = int(input().strip())
50-
print(greatest_palindromic_product_below_n(n))
37+
"""# Systematically go through all relevant products and find the greatest palindrome product."""
38+
k = largest_three_digit_to_consider(n)
39+
# Given the palindromic product must be a six-digit number, it must also be a multiple of 11.
40+
# Thus at least one of the factors must be a multiple of 11.
41+
l = int(k / 11)
42+
greatest = 0
43+
# Let a be the factor that must be a multiple of 11
44+
for i in range(10, l + 1):
45+
a = 11 * i
46+
for j in range(100, k):
47+
product = a * j
48+
if product < n:
49+
if product > greatest:
50+
if is_palindrome(product):
51+
greatest = product
52+
return greatest
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
"""
22
Problem 6:
33
4-
Find the absolute difference between the sum of the squares of the first n natural numbers and the square of the sum.
4+
Find the absolute difference between the sum of the squares of
5+
the first n natural numbers and the square of the sum.
56
7+
>>> abs_diff(10)
8+
2640
9+
10+
Author: Jessica Yung
11+
Refactored October 2016
612
"""
713

14+
815
# Need abs((1^2+2^2+...+N^2) - (1+2+...+N)^2)
916

10-
# Sum of the squares of the first n natural numbers
17+
1118
def sum_of_squares(n):
12-
sum = 0
13-
for i in range(1,n+1):
14-
sum += i**2
15-
return sum
19+
"""Sum of the squares of the first n natural numbers."""
20+
sum_of_sq = 0
21+
for i in range(1, n + 1):
22+
sum_of_sq += i ** 2
23+
return sum_of_sq
24+
1625

1726
def square_of_sum(n):
18-
sum = 0
19-
for i in range(1, n+1):
20-
sum += i
21-
return sum**2
22-
23-
# Read input and solve
24-
t = int(input().strip())
25-
for i in range(t):
26-
n = int(input().strip())
27-
print(abs(sum_of_squares(n) - square_of_sum(n)))
27+
sum = 0
28+
for i in range(1, n + 1):
29+
sum += i
30+
return sum ** 2
31+
32+
33+
def abs_diff(n):
34+
return abs(sum_of_squares(n) - square_of_sum(n))

mathematics/project-euler/example_code_with_unittest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""
22
A simple function and a unit test to go with it.
3+
34
Have tested that this returns no errors with the uncommented tests and that
45
it returns an AssertionError when the commented (deliberately false) test is included.
56
7+
Author: Jessica Yung
8+
October 2016
69
"""
710

811
import unittest
@@ -20,4 +23,4 @@ def test_square_x(self):
2023
# self.assertEqual(square_x(1), -1)
2124

2225
if __name__ == '__main__':
23-
unittest.main()
26+
unittest.main()

0 commit comments

Comments
 (0)