Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-duffy committed Aug 28, 2013
0 parents commit f022a46
Show file tree
Hide file tree
Showing 26 changed files with 471 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Project Euler Solutions
=======================

These are my solutions to the Project Euler problems, written in Python.

The incomplete solutions for the problems which I have started but not yet been able to solve are kept in the 'unsolved' folder.
1 change: 1 addition & 0 deletions problem_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print sum([i for i in range(1, 1000) if i % 3 == 0 or i % 5 == 0])
54 changes: 54 additions & 0 deletions problem_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
with open('problem_11_resource.txt', 'rb') as raw_data:

data = []
for row in raw_data:
data.append(row.split())

greatest_product = 0

def product_find():
global greatest_product
if product > greatest_product:
greatest_product = product

# Horizontal
for row in data:
for i in range(17):

product = 1
for n in range(4):
product *= int(row[n + i])

product_find()

# Vertical
for x in range(20):
for y in range(16):

product = 1
for n in range(4):
product *= int(data[x][y + n])

product_find()

#Diagonal Right
for x in range(17):
for y in range(17):

product = 1
for n in range(4):
product *= int(data[x + n][y + n])

product_find()

#Diagonal Left
for x in range(3, 20):
for y in range(17):

product = 1
for n in range(4):
product *= int(data[x - n][y + n])

product_find()

print greatest_product
20 changes: 20 additions & 0 deletions problem_11_resource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
7 changes: 7 additions & 0 deletions problem_13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
number = "37107287533902102798797998220837590246510135740250463769376774900097126481248969700780504170182605387432498619952474105947423330951305812372661730962991942213363574161572522430563301811072406154908250230675882075393461711719803104210475137780632466768926167069662363382013637841838368417873436172675728112879812849979408065481931592621691275889832738442742289174325203219235894228767964876702721893184745144573600130643909116721685684458871160315327670386486105843025439939619828917593665686757934951621764571418565606295021572231965867550793241933316490635246274190492910143244581382266334794475817892575867718337217661963751590579239728245598838407582035653253593990084026335689488301894586282278288018119938482628201427819413994056758715117009439035398664372827112653829987240784473053190104293586865155060062958648615320752733719591914205172558297169388870771546649911559348760353292171497005693854370070576826684624621495650076471787294438377604532826541087568284431911906346940378552177792951453612327252500029607107508256381565671088525835072145876576172410976447339110607218265236877223636045174237069058518606604482076212098132878607339694128114266041808683061932846081119106155694051268969251934325451728388641918047049293215058642563049483624672216484350762017279180399446930047329563406911573244438690812579451408905770622942919710792820955037687525678773091862540744969844508330393682126183363848253301546861961243487676812975343759465158038628759287849020152168555482871720121925776695478182833757993103614740356856449095527097864797581167263201004368978425535399209318374414978068609844840309812907779179908821879532736447567559084803087086987551392711854517078544161852424320693150332599594068957565367821070749269665376763262354472106979395067965269474259770973916669376304263398708541052684708299085211399427365734116182760315001271653786073615010808570091499395125570281987460043753582903531743471732693212357815498262974255273730794953759765105305946966067683156574377167401875275889028025717332296191766687138199318110487701902712526768027607800301367868099252546340106163286652636270218540497705585629946580636237993140746255962240744869082311749777923654662572469233228109171419143028819710328859780666976089293863828502533340334413065578016127815921815005561868836468420090470230530811728164304876237919698424872550366387845831148769693215490281042402013833512446218144177347063783299490636259666498587618221225225512486764533677201869716985443124195724099139590089523100588229554825530026352078153229679624948164195386821877476085327132285723110424803456124867697064507995236377742425354112916842768655389262050249103265729672370191327572567528565324825826546309220705859652229798860272258331913126375147341994889534765745501184957014548792889848568277260777137214037988797153829820378303147352772158034814451349137322665138134829543829199918180278916522431027392251122869539409579530664052326325380441000596549391598795936352974615218550237130764225512118369380358038858490341698116222072977186158236678424689157993532961922624679571944012690438771072750481023908955235974572318970677254791506150550495392297953090112996751986188088225875314529584099251203829009407770775672113067397083047244838165338735023408456470580773088295917476714036319800818712901187549131054712658197623331044818386269515456334926366572897563400500428462801835170705278318394258821455212272512503275512160354698120058176216521282765275169129689778932238195734329339946437501907836945765883352399886755061649651847751807381688378610915273579297013376217784275219262340194239963916804498399317331273132924185707147349566916674687634660915035914677504995186714302352196288948901024233251169136196266227326746080059154747183079839286853520694694454072476841822524674417161514036427982273348055556214818971426179103425986472045168939894221798260880768528778364618279934631376775430780936333301898264209010848802521674670883215120185883543223812876952786713296124747824645386369930090493103636197638780396218407357239979422340623539380833965132740801111666627891981488087797941876876144230030984490851411606618262936828367647447792391803351109890697907148578694408955299065364044742557608365997664579509666024396409905389607120198219976047599490197230297649139826800329731560371200413779037855660850892521673093931987275027546890690370753941304265231501194809377245048795150954100921645863754710598436791786391670211874924319957006419179697775990283006991536871371193661495281130587638027841075444973307840789923115535562561142322423255033685442488917353448899115014406480203690680639606723221932041495354150312888033953605329934036800697771065056663195481234880673210146739058568557934581403627822703280826165707739483275922328459417065250945123252306082291880205877731971983945018088807242966198081119777158542502016545090413245809786882778948721859617721078384350691861554356628840622574736922845095162084960398013400172393067166682355524525280460972253503534226472524250874054075591789781264330331690"
total = 0

for i in range(5000)[::50]:
total += int(number[i:i+50:])

print str(total)[:10:]
23 changes: 23 additions & 0 deletions problem_14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
max_count = 0

def collatz(n):
if n == 0:
return 1

if n % 2 == 0:
return n/2
else:
return (3*n) + 1

for i in range(1, 1000000):
n = i
count = 0
while n > 1:
n = collatz(n)
count += 1

if count > max_count:
max_count = count
solution = i

print solution
7 changes: 7 additions & 0 deletions problem_16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
num = str(2 ** 1000)
total = 0

for digit in num:
total += int(digit)

print total
66 changes: 66 additions & 0 deletions problem_17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
total = 0
ones = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
teys = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']

# 1 - 9
for one in ones:
print one
total += len(one)

#10 - 19
for teen in teens:
print teen
total += len(teen)

#20 - 99
for tey in teys:
print tey
total += len(tey)

for one in ones:
print tey, one
total += len(tey)
total += len(one)

for one in ones:
print one, 'hundred'
total += len(one)
total += len('hundred')

for sub_one in ones:
print one, 'hundred', 'and', sub_one
total += len(one)
total += len('hundred')
total += len('and')
total += len(sub_one)

for teen in teens:
print one, 'hundred', 'and', teen
total += len(one)
total += len('hundred')
total += len('and')
total += len(teen)

for tey in teys:
print one, 'hundred', 'and', tey
total += len(one)
total += len('hundred')
total += len('and')
total += len(tey)

for sub_one in ones:
print one, 'hundred', 'and', tey, sub_one
total += len(one)
total += len('hundred')
total += len('and')
total += len(tey)
total += len(sub_one)

print 'one', 'thousand'

total += len('one')
total += len('thousand')

print
print total
22 changes: 22 additions & 0 deletions problem_18.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
with open('problem_18_resource.txt', 'rb') as raw_data:
data = []
for row in raw_data:
data.append(row.split())

greatest_total = 0
start = int(data.pop(0)[0])

for c in range(8193, 16384):

this_total = start
position = 0

for i, row in enumerate(data):
this_c = str(bin(c))[i+2]
position += int(this_c)
this_total += int(row[position])

if this_total > greatest_total:
greatest_total = this_total

print greatest_total
15 changes: 15 additions & 0 deletions problem_18_resource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
10 changes: 10 additions & 0 deletions problem_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fib = [1, 2]
i = 2
n = 0

while n < 4000000:
n = fib[i - 1] + fib[i - 2]
fib.append(n)
i += 1

print sum(filter(lambda x: x % 2 == 0, fib))
10 changes: 10 additions & 0 deletions problem_20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
total = 0
factorial = 1

for i in range(2, 101):
factorial *= i

for c in str(factorial):
total += int(c)

print total
21 changes: 21 additions & 0 deletions problem_21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def divisor_sum(n):
d_sum = 0
for d in range(1, n):
if n % d == 0:
d_sum += d
return d_sum

def is_amicable(n1):
n2 = divisor_sum(n1)
if divisor_sum(n2) == n1 and n1 != n2:
return True
else:
return False

total = 0

for i in range(10000):
if is_amicable(i):
total += i

print total
14 changes: 14 additions & 0 deletions problem_25.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fib_1 = 1;
fib_2 = 1;
n = 0;
i = 2;

while len(str(n)) < 1000:
n = fib_1 + fib_2
print n
fib_1 = fib_2
fib_2 = n
i += 1

print
print i
23 changes: 23 additions & 0 deletions problem_26.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from decimal import *

getcontext().prec = 10000

def recursion_length(n):
f = str(1/Decimal(n))[2::]
for cycle_length in range(1, 10000):
part_one = f[0:0+cycle_length:]
part_two = f[cycle_length:cycle_length*2:]
part_three = f[cycle_length*2:cycle_length*3:]
if part_one == part_two and part_two == part_three:
return cycle_length
return 0

longest_cycle = 0

for i in range(1, 1000):
if recursion_length(i) > longest_cycle:
i_longest = i
longest_cycle = recursion_length(i)

print
print i_longest
21 changes: 21 additions & 0 deletions problem_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
min_limit = 100
max_limit = 999
largest = 0

def is_palindrome(n):
n = str(n)
l = len(n)
if l % 2 == 0:
if n[:l/2:] == n[:(l/2)-1:-1]:
return True
else:
if n[:((l+1)/2)-1:] == n[:((l+1)/2)-1:-1]:
return True
return False

for a in range(min_limit, max_limit+1)[::-1]:
for b in range(min_limit, max_limit+1)[::-1]:
if is_palindrome(a*b) and a*b > largest:
largest = a*b

print largest
12 changes: 12 additions & 0 deletions problem_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i = 100000
found = False

while not found:
found = True
for n in range(1, 21):
if i % n != 0:
found = False
break
i += 20

print i - 20
8 changes: 8 additions & 0 deletions problem_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sum_natural = 0
sum_squares = 0

for i in range(1, 101):
sum_natural += i
sum_squares += i**2

print sum_natural**2 - sum_squares
12 changes: 12 additions & 0 deletions problem_8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
search_number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"

greatest_product = 0

for i in range(0, 994):
product = 1
for n in search_number[i:i+5:]:
product *= int(n)
if product > greatest_product:
greatest_product = product

print greatest_product
Loading

0 comments on commit f022a46

Please sign in to comment.