This commit is contained in:
Marina Wahl 2014-04-16 21:23:49 -04:00
parent 1e9fdfbbfd
commit 5107f16df0
96 changed files with 17 additions and 1702 deletions

View file

@ -1,25 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2014
# steinkirch at gmail
# for hacker school application
'''
Write a program that prints out the numbers 1 to 100 (inclusive). If the number is divisible by 3, print Crackle instead of the number. If it's divisible by 5, print Pop. If it's divisible by both 3 and 5, print CracklePop.
'''
def CracklePop(n):
for i in range(1, n+1):
if i%3 == 0 and i%5 == 0: print('CracklePop!!!')
elif i%3 == 0: print("Crackle!!!")
elif i%5 == 0: print("Pop!!!")
else: print(i)
def main():
CracklePop(100)
if __name__ == '__main__':
main()

View file

@ -1,44 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
'''
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.
'''
def find_sum_proper_divisors(n):
sum_proper_div = 0
for i in range(1, n):
if n%i == 0:
sum_proper_div += i
return sum_proper_div
def amicable_numbers(N):
sum_div_list = [find_sum_proper_divisors(i) for i in range(1, N+1)]
sum_amicable_numbers = 0
set_div = set()
for a in range(1, N):
da = sum_div_list[a-1]
if da < N:
b = da
db = sum_div_list[b-1]
if a != b and db == a and a not in set_div and b not in set_div:
sum_amicable_numbers += a + b
set_div.add(a)
set_div.add(b)
return sum_amicable_numbers
def main():
print(amicable_numbers(10000))
print('Tests Passed!')
if __name__ == '__main__':
main()

View file

@ -1,91 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def isPrime(n):
n = abs(int(n))
if n < 2:
return False
if n == 2:
return True
for x in range(2, int(n**0.5)+1):
if n%x == 0:
return False
return True
def findPermutations(s):
res = []
if len(s) == 1:
res.append(s)
else:
for i, c in enumerate(s):
for perm in findPermutations(s[:i] + s[i+1:]):
res.append(c + perm)
return res
def isCircular(n):
n_str = str(n)
permutations = findPermutations(n_str)
for perm in permutations:
if not isPrime(perm):
return False
return True
def generatePrimes(n):
if n == 2: return [2]
elif n < 2: return []
s = [i for i in range(3, n+1, 2)]
mroot = n ** 0.5
half = (n+1)//2 - 1
i, m = 0, 3
while m <= mroot:
if s[i]:
j = (m*m-3)//2
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i+1
m = 2*i+3
return [2]+[x for x in s if x]
def generate_n_Primes(n):
primes = []
chkthis = 2
while len(primes) < n:
ptest = [chkthis for i in primes if chkthis%i == 0]
primes += [] if ptest else [chkthis]
chkthis += 1
return primes
def circular_primes(n):
primes = generatePrimes(n)
count = 0
for prime in primes:
if isCircular(prime):
count += 1
return count
def main():
import time
start = time.time()
print(circular_primes(1000000))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,65 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
'''
1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
'''
def find_if_leap_year(y):
if (y%4 == 0 and y%100 != 0) or (y%400 == 0):
return True
return False
def counting_sundays():
''' define variables '''
days_year = 7*31 + 4*30 + 28
count_sundays = 0
days_week = 7
dict_week = {0: 'mon', 1:'tue', 2:'wed', 3:'thu', 4:'fri', 5:'sat', 6:'sun'}
''' with info from 1900 find first day for 1901 '''
first_day = days_year%days_week # not a leap year
for y in range (1901, 2001):
leap_year = find_if_leap_year(y)
days_count = first_day
for m in range(1, 13):
if days_count%7 == 6:
count_sundays += 1
if m == 2:
if leap_year:
days_count += 29
else:
days_count += 28
elif m == 4 or m == 6 or m == 9 or m == 11:
days_count += 30
else:
days_count += 31
if leap_year: first_day = (first_day +2)%days_week
else: first_day = (first_day +1)%days_week
return count_sundays
def main():
print(counting_sundays())
print('Tests Passed!')
if __name__ == '__main__':
main()

View file

@ -1,30 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def digit_fifth_pow(n):
lnum = []
for num in range(10**(2), 10**(n+2)):
sum_here = 0
num_str = str(num)
for i in num_str:
num_int = int(i)
num_int_pow = num_int**n
sum_here += num_int_pow
if sum_here == num:
lnum.append(num)
return lnum, sum(lnum)
def main():
import time
start = time.time()
print(digit_fifth_pow(5))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,27 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def dist_pow(a1, a2, b1, b2):
set1 = set()
for a in range(a1, a2 + 1):
for b in range(b1, b2 + 1):
set1.add(a**b)
return len(set1)
def main():
import time
start = time.time()
print(dist_pow(2, 5, 2, 5))
print(dist_pow(2, 100, 2, 100))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,18 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def even_fib_num(limit):
a, b = 0, 1
while a < limit:
yield a
a, b = b, a + b
def main():
print(sum(n for n in even_fib_num(4e6) if not (n & 1)))
print('Tests Passed!')
if __name__ == '__main__':
main()

View file

@ -1,32 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def factorial(n):
prod = 1
for i in range(1,n):
prod *= i
return prod
def find_sum(n):
sum_ = 0
fact = factorial(n)
number = str(fact)
for i in number:
sum_ += int(i)
return sum_
def main():
import time
start = time.time()
assert(find_sum(10) == 27)
print(find_sum(100))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,32 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
import math
def is_prime(number, prime_set):
if number in prime_set: return True
for i in range(2, int(math.sqrt(number)) + 1):
if not number%i: return False
return True
def findstprime(n):
count = 0
candidate = 1
prime_set = set()
while count < n:
candidate +=1
if is_prime(candidate, prime_set):
prime_set.add(candidate)
count += 1
return candidate
def main():
assert(findstprime(6 == 13))
print(findstprime(10001))
print('Tests Passed!')
if __name__ == '__main__':
main()

View file

@ -1,64 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def isPrime(n):
n = abs(int(n))
if n < 2:
return False
if n == 2:
return True
if not n & 1:
return False
for x in range(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True
def generetePrimes(n):
if n == 2: return [2]
elif n < 2: return []
s = [i for i in range(3, n+1,2)]
mroot = n ** 0.5
half = (n+1)//2-1
i = 0
m = 3
while m <= mroot:
if s[i]:
j = (m*m - 3)//2
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i+1
m = 2*i+3
return [2]+[x for x in s if x]
def gold_other(n):
primes_for_n = generetePrimes(n)
numbers = {prime + 2*x**2 for prime in primes_for_n for x in range(1, int(n**0.5))}
conj = {x for x in range(3, n, 2) if not isPrime(x)}
while True:
candidates = conj - numbers
if not candidates:
gold_other(2*n)
else:
return min(candidates)
def main():
import time
start = time.time()
print(gold_other(10000))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,43 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
import math
def find_div(n):
''' find the divisor of a given n'''
set_div = {1, n}
for i in range(2, int(math.sqrt(n))+ 1):
if not n % i:
set_div.add(i)
set_div.add(n//i)
l1 = list(set_div)
return len(l1)
def find_trian(l):
''' find the lth trian number'''
return sum(range(1, l+1))
def highly_divisible_trian_num(d):
thtriangle, n_div, count = 1, 0, 1
while n_div < d:
count += 1
thtriangle += count
n_div = find_div(thtriangle)
return (thtriangle, count)
def main():
import time
start = time.time()
assert(highly_divisible_trian_num(6) == (28, 7))
print(highly_divisible_trian_num(500))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,47 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def is_palindrome(s):
return s == reverse(s)
def reverse(s):
rev = 0
while s > 0:
rev = 10*rev + s%10
s = s//10
return rev
def is_palindrome_2(s):
# to use it you need to cast str() first
while s:
if s[0] != s[-1]: return False
else:
s = s[1:-1]
is_palindrome(s)
return True
def larg_palind_product(n):
nmax, largpal = 9, 0
for i in range(1, n):
nmax += 9*10**i
for i in range(nmax, nmax//2, -1):
for j in range(i -1, (i -1)//2, -1):
candidate = i*j
if is_palindrome(candidate) and candidate > largpal:
largpal = candidate
return largpal
def test_larg_palind_product():
assert(larg_palind_product(2)== 9009)
print(larg_palind_product(3))
print('Tests Passed!')
if __name__ == '__main__':
test_larg_palind_product()

View file

@ -1,20 +0,0 @@
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

View file

@ -1,66 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
import string
def get_grid(filename):
grid = [ [ 0 for i in range(20) ] for j in range(20) ]
with open(filename) as file:
for row, line in enumerate(file):
line.strip('\n')
for collumn, number in enumerate(line.split(' ')):
grid[row][collumn] = int(number)
return grid
def larg_prod_grid(grid):
row, col, larg_prod = 0, 0, 0
up, down, left, right, diag1, diag2, diag3, diag4 = 0, 0, 0, 0, 0, 0, 0, 0
while row < len(grid):
while col < len(grid[0]):
if col > 2:
up = grid[row][col] * grid[row][col-1] * grid[row][col-2] * grid[row][col-3]
if col < len(grid[0]) - 3:
down = grid[row][col] * grid[row][col+1] * grid[row][col+2] * grid[row][col+3]
if row > 2:
left = grid[row][col] * grid[row-1][col] * grid[row-2][col] * grid[row-3][col]
if row < len(grid) - 3:
right = grid[row][col] * grid[row+1][col] * grid[row+2][col] * grid[row+3][col]
if col > 2 and row > 2:
diag1 = grid[row][col] * grid[row-1][col-1] * grid[row-2][col-2] * grid[row-3][col-3]
if col > 2 and row < len(grid) - 3:
diag2 = grid[row][col] * grid[row+1][col-1] * grid[row+2][col-2] * grid[row+3][col-3]
if col < len(grid[0]) - 3 and row > 2:
diag3 = grid[row][col] * grid[row-1][col+1] * grid[row-2][col+2] * grid[row-3][col+3]
if col < len(grid[0]) -3 and row < len(grid) - 3:
down = grid[row][col] * grid[row+1][col+1] * grid[row+1][col+2] * grid[row+1][col+3]
l1 = [up, down, left, right, diag1, diag2, diag3, diag4]
largest_prod_here = max(l1)
if largest_prod_here > larg_prod:
larg_prod = largest_prod_here
col += 1
col = 0
row += 1
return larg_prod
def main():
import time
start = time.time()
filename = 'larg_prod_grid.dat'
grid = get_grid(filename)
assert((grid[6][8], grid[7][9], grid[8][10], grid[9][11]) == (26, 63, 78, 14))
print(larg_prod_grid(grid))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,100 +0,0 @@
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951
62176457141856560629502157223196586755079324193331
64906352462741904929101432445813822663347944758178
92575867718337217661963751590579239728245598838407
58203565325359399008402633568948830189458628227828
80181199384826282014278194139940567587151170094390
35398664372827112653829987240784473053190104293586
86515506006295864861532075273371959191420517255829
71693888707715466499115593487603532921714970056938
54370070576826684624621495650076471787294438377604
53282654108756828443191190634694037855217779295145
36123272525000296071075082563815656710885258350721
45876576172410976447339110607218265236877223636045
17423706905851860660448207621209813287860733969412
81142660418086830619328460811191061556940512689692
51934325451728388641918047049293215058642563049483
62467221648435076201727918039944693004732956340691
15732444386908125794514089057706229429197107928209
55037687525678773091862540744969844508330393682126
18336384825330154686196124348767681297534375946515
80386287592878490201521685554828717201219257766954
78182833757993103614740356856449095527097864797581
16726320100436897842553539920931837441497806860984
48403098129077791799088218795327364475675590848030
87086987551392711854517078544161852424320693150332
59959406895756536782107074926966537676326235447210
69793950679652694742597709739166693763042633987085
41052684708299085211399427365734116182760315001271
65378607361501080857009149939512557028198746004375
35829035317434717326932123578154982629742552737307
94953759765105305946966067683156574377167401875275
88902802571733229619176668713819931811048770190271
25267680276078003013678680992525463401061632866526
36270218540497705585629946580636237993140746255962
24074486908231174977792365466257246923322810917141
91430288197103288597806669760892938638285025333403
34413065578016127815921815005561868836468420090470
23053081172816430487623791969842487255036638784583
11487696932154902810424020138335124462181441773470
63783299490636259666498587618221225225512486764533
67720186971698544312419572409913959008952310058822
95548255300263520781532296796249481641953868218774
76085327132285723110424803456124867697064507995236
37774242535411291684276865538926205024910326572967
23701913275725675285653248258265463092207058596522
29798860272258331913126375147341994889534765745501
18495701454879288984856827726077713721403798879715
38298203783031473527721580348144513491373226651381
34829543829199918180278916522431027392251122869539
40957953066405232632538044100059654939159879593635
29746152185502371307642255121183693803580388584903
41698116222072977186158236678424689157993532961922
62467957194401269043877107275048102390895523597457
23189706772547915061505504953922979530901129967519
86188088225875314529584099251203829009407770775672
11306739708304724483816533873502340845647058077308
82959174767140363198008187129011875491310547126581
97623331044818386269515456334926366572897563400500
42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
32238195734329339946437501907836945765883352399886
75506164965184775180738168837861091527357929701337
62177842752192623401942399639168044983993173312731
32924185707147349566916674687634660915035914677504
99518671430235219628894890102423325116913619626622
73267460800591547471830798392868535206946944540724
76841822524674417161514036427982273348055556214818
97142617910342598647204516893989422179826088076852
87783646182799346313767754307809363333018982642090
10848802521674670883215120185883543223812876952786
71329612474782464538636993009049310363619763878039
62184073572399794223406235393808339651327408011116
66627891981488087797941876876144230030984490851411
60661826293682836764744779239180335110989069790714
85786944089552990653640447425576083659976645795096
66024396409905389607120198219976047599490197230297
64913982680032973156037120041377903785566085089252
16730939319872750275468906903707539413042652315011
94809377245048795150954100921645863754710598436791
78639167021187492431995700641917969777599028300699
15368713711936614952811305876380278410754449733078
40789923115535562561142322423255033685442488917353
44889911501440648020369068063960672322193204149535
41503128880339536053299340368006977710650566631954
81234880673210146739058568557934581403627822703280
82616570773948327592232845941706525094512325230608
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690

View file

@ -1,25 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def large_sum(filename):
sum_total, lines, numbers = 0, 0, 0
with open(filename) as file:
for line in file:
sum_total += int(line.strip('\n'))
return str(sum_total)[0:10]
def main():
import time
start = time.time()
filename = 'large_sum.dat'
print(large_sum(filename))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,41 +0,0 @@
#!/usr/bin/python3
#!/usr/bin/python3
def is_prime(n):
if n < 4 : return True
for i in range(2, int(n**0.5 + 1)):
if not n%i: return False
return True
def largest_prime_factor(n):
i = int(n**0.5 +1)
while i > 1 :
if not n%i and i&1:
if is_prime(i): return i
i -= 1
return None
def largest_prime_factor_optimized(n):
factor = 2
lastfactor = 1
while n > 1:
if not n%factor:
lastfactor = factor
n = n//factor
while n%factor == 0:
n = n//factor
factor += 1
return lastfactor
def test_largest_prime_factor():
assert(largest_prime_factor(13195)== 29)
print(largest_prime_factor(600851475143))
assert(largest_prime_factor_optimized(13195) == 29)
print(largest_prime_factor_optimized(600851475143))
print('Tests Passed!')
if __name__ == '__main__':
test_largest_prime_factor()

View file

@ -1,28 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def largest_prod_seq(n):
result = 0
for i in range(0, len(n)-4):
first = int(n[i])
second = int(n[i+1])
third = int(n[i+2])
fourth = int(n[i+3])
fifth = int(n[i+4])
result_here = first*second*third*fourth*fifth
if result < result_here:
result = result_here
return result
def main():
n = '7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'
print(largest_prod_seq(n))
print('Tests Passed!')
if __name__ == '__main__':
main()

View file

@ -1,42 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def lattice_paths(squares):
gridsize = squares+1
grid = [[0 for i in range(gridsize)] for j in range(gridsize)]
row, col = 0, 0
while col < gridsize:
while row < gridsize:
if row == 0 and col == 0:
grid[row][col] = 1
else:
if row == 0 and col != 0:
grid[row][col] += grid[row][col-1]
elif row != 0 and col == 0:
grid[row][col] += grid[row-1][col]
else:
grid[row][col] += grid[row][col-1] + grid[row-1][col]
row += 1
row = 0
col += 1
return grid[gridsize-1][gridsize-1]
def main():
import time
start = time.time()
assert(lattice_paths(2) == 6)
print(lattice_paths(20))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,33 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def perm_item(elements):
if len(elements) <= 1:
yield elements
else:
for (index, elmt) in enumerate(elements):
other_elmts = elements[:index]+elements[index+1:]
for permutation in perm_item(other_elmts):
yield [elmt] + permutation
def lex_perm(l1, n):
perm_list = list(perm_item(l1))
return sorted(perm_list)[n-1]
def main():
import time
start = time.time()
l1 = [0,1,2,3,4,5,6,7,8,9]
n = 10**6
print(lex_perm(l1, n))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,43 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def find_coll_seq(n):
count = 1
while n > 1:
if n%2 == 0:
n = n//2
else:
n = 3*n +1
count += 1
return count
def find_longest_chain(limit):
longest, number = 0, 0
start = 0
while start <= limit:
size_chain = find_coll_seq(start)
if size_chain > longest:
longest = size_chain
number = start
start += 1
return (longest, number)
def main():
import time
start = time.time()
#print(find_longest_chain(13))
print(find_longest_chain(10**6))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,15 +0,0 @@
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

View file

@ -1,55 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def max_path_sum(t):
root = t[0][0]
height, width, index, large_num = 1, 0, 0, 0
max_sum = root
heights = len(t[:])
while height < heights:
values_here = t[height][index:index+2]
if values_here[0] > values_here[1]:
large_num = values_here[0]
else:
large_num = values_here[1]
index += 1
max_sum += large_num
pivot = large_num
width, large_num = 0, 0
height += 1
return max_sum
def edit_input(filename):
output = []
with open(filename) as file:
for line in file:
line = line.rstrip('\n')
output.append(line.split(' '))
for i, l1 in enumerate(output):
for j, c in enumerate(output[i]):
output[i][j] = int(c)
return(output)
def main():
import time
start = time.time()
filename = 'max_path_sum0.dat'
t1 = edit_input(filename)
print('Little pir: ',max_path_sum(t1))
filename = 'max_path_sum.dat'
t2 = edit_input(filename)
print('Big pir: ', max_path_sum(t2))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,4 +0,0 @@
3
7 4
2 4 6
8 5 9 3

View file

@ -1,22 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def mul3and5(n):
result = 0
for num in range(1, n):
if num%3 == 0 or num%5 == 0:
result += num
return result
def test_():
assert(mul3and5(10) == 23)
print(mul3and5(1000))
print('Tests Passed!')
if __name__ == '__main__':
test_()

File diff suppressed because one or more lines are too long

View file

@ -1,33 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def calculate_score(name, dict_letters):
sum_letters = 0
for letter in name:
sum_letters += dict_letters[letter]
return sum_letters
def names_score(filename):
dict_letters ={'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7,'H':8,'I':9,'J':10,'K':11,'L':12,'M':13,'N':14,'O':15,'P':16,'Q':17,'R':18,'S':19, 'T':20,'U':21,'V':22,'W':23,'X':24,'Y':25,'Z':26}
total_score = 0
with open(filename) as file:
for line in file:
names = [name.strip('"') for name in line.split(',')]
names.sort()
for i, name in enumerate(names):
total_score += (i+1)* calculate_score(name, dict_letters)
return total_score
def main():
filename = 'names.txt'
print(names_score(filename))
if __name__ == '__main__':
main()

View file

@ -1,44 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def find_sum_proper_div(n):
sum_proper_div = 0
for i in range(1, n):
if n%i == 0:
sum_proper_div += i
return sum_proper_div
def find_all_abund(n):
sum_div_list = [find_sum_proper_div(i) for i in range(n)]
abu = set()
for i in range(n):
if i < sum_div_list[i]:
abu.add(i)
return abu
def non_abund_sums(n):
abu = find_all_abund(n)
sum_nom_abu = 0
for i in range(n):
if not any( (i-a in abu) for a in abu):
sum_nom_abu += i
return sum_nom_abu
def test_():
r = set([i for i in range(25)])
r_abu = {24}
r = r - r_abu
assert(non_abund_sums(25) == sum(r))
print(non_abund_sums(28123))
print('Tests Passed!')
if __name__ == '__main__':
test_()

View file

@ -1,63 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def number_letter_counts(n):
dict_lett = build_dict(n)
sum_letter = 0
for item in dict_lett:
sum_letter += dict_lett[item]
return sum_letter
def build_dict(n):
lett_dict = {}
numbers = (x for x in range(1, n+1))
dec = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
ties = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
for number in numbers:
if 1 <= number < 20:
lett_dict[number] = len(dec[number-1])
elif 20 <= number < 100:
index_dec = number//10
index_num = number%10
if index_num == 0:
lett_dict[number] = len(ties[index_dec-2])
else:
lett_dict[number] = len(ties[index_dec-2]) + len(dec[index_num-1])
elif 100 <= number < 1000:
index_hun = number//100
index_dec = number%100
if index_dec == 0:
lett_dict[number] = len(dec[index_hun-1]) + len('hundred')
else:
if 1 <= index_dec < 20:
lett_dict[number] = len(dec[index_hun-1]) + len('hundred') + len('and') + len(dec[index_dec-1])
elif 20 <= index_dec < 100:
index_dec2 = index_dec//10
index_num = index_dec%10
if index_num == 0:
lett_dict[number] = len(dec[index_hun-1]) + len('hundred') + len('and') + len(ties[index_dec2-2])
else:
lett_dict[number] = len(dec[index_hun-1]) + len('hundred') + len('and') + len(ties[index_dec2-2]) + len(dec[index_num-1])
elif number == 1000:
lett_dict[number] = len('onethousand')
return lett_dict
def main():
import time
start = time.time()
assert(number_letter_counts(5) == 19)
print(number_letter_counts(1000))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,39 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def number_spiral(spiral):
return rows, mid
def make_spiral(n):
spiral = []
row = rows//2
col = col//2
count = 1
while row < n:
while col < n:
spiral[col][row] = count
count += 1
if count%2 == 0:
col += 1
else:
row += 1
return spiral
def main():
import time
start = time.time()
n = 5
spiral = make_spiral(n)
print(number_spiral(spiral))# 101
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,22 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def power_digit_sum(n):
number = str(2**n)
sum_res = 0
for i in number:
sum_res += int(i)
return sum_res
def test_():
assert(power_digit_sum(15) == 26)
print(power_digit_sum(1000))
print('Tests Passed!')
if __name__ == '__main__':
test_()

View file

@ -1,50 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def quad_form(n, a, b):
return n**2 + a*n + b
def isPrime(n):
n = abs(int(n))
if n < 2:
return False
if n == 2:
return True
if not n & 1:
return False
for x in range(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True
def quad_primes(a, b):
count_max = 0
coef = ()
for aa in range(-a, a):
for bb in range(-b, b):
n = 0
while True:
number = quad_form(n, aa, bb)
if isPrime(number):
n += 1
else:
if n > count_max:
count_max = n
coef = (aa, bb)
break
return coef(0)*coef(1), coef
def main():
import time
start = time.time()
print(quad_primes(1000, 1000))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,29 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def self_powers(power, digits):
sum_total = 0
for pow in range(1, power+1):
sum_total += pow**pow
sum_total_str = str(sum_total)
last_digits = ''
for i, c in enumerate(sum_total_str[-digits:]):
last_digits += c
return int(last_digits)
def main():
import time
start = time.time()
assert(self_powers(10, len('10405071317')) == 10405071317)
print(self_powers(1000, 10))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,32 +0,0 @@
#!/usr/bin/python3
def smallest_multiple(n):
set1 = set([x for x in range(1, n+1)])
set2 = set()
for i in range(len(set1), 0, -1):
for j in range(1, i):
if i%j == 0:
set2.add(j)
set1 = set1 - set2
res_num = n*n
while True:
for i in set1:
missing_div = False
if res_num%i:
missing_div = True
shift = res_num%i
break
if not missing_div: return res_num
res_num += 1 or shift
shift = 0
def test_():
assert(smallest_multiple(10) == 2520)
print(smallest_multiple(20))
print('Tests Passed!')
if __name__ == '__main__':
test_()

View file

@ -1,26 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def special_pyt(n):
for i in range(3, n):
for j in range(i+1, n):
c = calc_c(i,j)
if i + j + c == n:
return i*j*c
def calc_c(a, b):
return (a**2 + b**2)**0.5
def main():
assert(special_pyt(3+4+5) == (3*4*5))
print(special_pyt(1000))
print('Tests Passed!')
if __name__ == '__main__':
main()

View file

@ -1,38 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def calculate_chain(n):
n_str = str(n)
while n_str != 1 or n_str != 89:
n_str = str(n_str)
sum_here = 0
for d in n_str:
sum_here += int(d)**2
n_str = sum_here
if n_str == 89:
return 1
if n_str == 1:
return 0
def square_dig_chains(n):
count = 0
for i in range(1, n+1):
count += calculate_chain(i)
return count
def main():
import time
start = time.time()
print(square_dig_chains(10**7))
elapsed = (time.time() - start)
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
if __name__ == '__main__':
main()

View file

@ -1,21 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def sum_square_diff(n):
sq_sum, sum_sq = 0, 0
for i in range(1, n+1):
sum_sq += i**2
sq_sum += i
sq_sum = sq_sum **2
return sq_sum - sum_sq
def main():
assert(sum_square_diff(10) == 2640)
print(sum_square_diff(100))
print('Tests Passed!')
if __name__ == '__main__':
main()

View file

@ -1,24 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
from findstprime import is_prime
def summation_primes(n):
candidate = 2
prime_set = set()
while candidate < n:
if is_prime(candidate, prime_set):
prime_set.add(candidate)
candidate +=1
return sum(prime_set)
def main():
assert(summation_primes(10) == 17)
print(summation_primes(2000000))
print('Tests Passed!')
if __name__ == '__main__':
main()

View file

@ -1,70 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
def findToneDiff(tones):
tonesDiff = []
n = len(tones)
for i, tone in enumerate(tones):
for j in range(i+1, len(tones)):
sum_here = abs(tone - tones[j])
tonesDiff.append([sum_here, i, j])
return sorted(tonesDiff)
def findAllPossible(duration, tones, T):
tonesDiff = findToneDiff(tones)
sumsTone1 = [(song, i) for i, song in enumerate(duration) if song <= T]
sumsTone2 = []
for song in tonesDiff:
sum_here = song[0] + duration[song[1]] + duration[song[2]]
if sum_here <= T:
sumsTone2.append((sum_here, song[1], song[2], 2))
return sumsTone1, sumsTone2
def findAllPossibleNext(sumsTone, T, n_music):
sumsTone2 = []
for i, song1 in enumerate(sumsTone):
index1 = song1[1]
for j in range(i+1, len(sumsTone)):
song2 = sumsTone[j]
index2 = song2[1]
if index1 == index2:
sum_here = song1[0] + song2[0]
if sum_here < T:
sumsTone2.append((sum_here, song2[1], song2[2], n_music))
return sumsTone2
def maxSongs(duration, tones, T):
if min(duration) >= T:
return 0
sumsTone1, sumsTone = findAllPossible(duration, tones, T)
if not sumsTone:
return 1
while sumsTone:
n_music = sumsTone[0][3]+1
sumsTone = findAllPossibleNext(sumsTone, T, n_music)
if not sumsTone:
return n_music
def tests_250():
print(maxSongs([3, 5, 4, 11], [2, 1, 3, 1], 17)) #3
print(maxSongs([9, 11, 13, 17], [2, 1, 3, 4], 20)) #1
print(maxSongs([100, 200, 300], [1,2,3], 99)) #0
print(maxSongs([87,21,20,73,97,57,12,80,86,97,98,85,41,12,89,15,41,17,68,37,21,1,9,65,4,67,38,91,46,82,7,98,21,70,99,41,21,65,11,1,8,12,77,62,52,69,56,33,98,97], [88,27,89,2,96,32,4,93,89,50,58,70,15,48,31,2,27,20,31,3,23,86,69,12,59,61,85,67,77,34,29,3,75,42,50,37,56,45,51,68,89,17,4,47,9,14,29,59,43,3], 212))
if __name__ == '__main__':
tests_250()