-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem001.py
More file actions
25 lines (17 loc) · 869 Bytes
/
Copy pathProblem001.py
File metadata and controls
25 lines (17 loc) · 869 Bytes
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
#Problem 1 for Project Euler
#The goal is to find the sum of all multiples of 3, and 5 below 1000.
#from project Euler 9/4/2015
def PE1(a, b, n):
'''Returns the sum of all multiples of a and b, below the positive integer n.'''
def f(x, n):
fl = int((n-1)/x)
return x * fl * (fl+1)
return (f(a,n) + f(b,n) - f(a*b,n))//2
print(PE1(3,5,1000))
#This is my favorite python solution for problem 1. There are no loops, because this solution requires no loops.
#I also enjoy the functions within functions.
#What we see in f, is our desired multiple being multiplied by the sum of the natural numbers
#that index the number of those multiples below our limit.
#3+6+9+...+999 = 3(1+2+3+...+333)
#5+10+15+...+995 = 5(1+2+3+...+199)
#Then we simply add the union of the sets, and subtract their intersection resulting in the sum of two sets.