Skip to content

Commit 8fb79ac

Browse files
author
Jessica Yung
committed
Better documentation for project euler problems
1 parent dc6f6a0 commit 8fb79ac

7 files changed

+117
-0
lines changed

mathematics/project-euler/1-multiples-of-three-and-five.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Problem 1:
3+
34
Find the sum of all multiples of 3 or 5 below a positive integer n
5+
46
"""
57

68
# Function calculates sum of multiples of k less than n

mathematics/project-euler/2-even-fibonacci-numbers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Problem 2:
3+
34
Calculate the sum of all even Fibonacci numbers below positive integer n
5+
46
"""
57

68
# Function to calculate nth Fibonacci number, n being a non-negative integer.

mathematics/project-euler/3-largest-prime-factor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Problem 3:
3+
34
Find the largest prime factor of a positive integer n
5+
46
"""
57
import math
68

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Problem 4:
3+
4+
Find the largest palindrome made from the product of two 3-digit integers which is less than positive integer n.
5+
6+
"""
7+
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
24+
25+
"""
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.
27+
28+
"""
29+
30+
# Systematically go through all relevant products and find the greatest palindrome product
31+
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))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Problem 5:
3+
4+
2520 is the smallest number that can be divided by each of the numbers from to without any remainder.
5+
What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N?
6+
7+
"""
8+
9+
# Find all the prime factors and their powers in that number
10+
# Idea: Recursive
11+
12+
def blah():
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Problem 6:
3+
4+
Find the absolute difference between the sum of the squares of the first n natural numbers and the square of the sum.
5+
6+
"""
7+
8+
# Need abs((1^2+2^2+...+N^2) - (1+2+...+N)^2)
9+
10+
# Sum of the squares of the first n natural numbers
11+
def sum_of_squares(n):
12+
sum = 0
13+
for i in range(1,n+1):
14+
sum += i**2
15+
return sum
16+
17+
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)))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Problem 6:
3+
4+
Find the nth prime number.
5+
n=< 10**4
6+
7+
"""
8+
9+
primes=[]
10+
def all_primes_under_n(n):
11+
primes_under_n = []
12+
for i in range(n):
13+
if isprime(i) == True:
14+
primes_under_n.append(i)
15+
return primes_under_n
16+
17+
def nth_prime(n):
18+
all_primes_under_n[n]
19+
20+
i = int(primes[n-2])
21+
while
22+

0 commit comments

Comments
 (0)