Skip to content

Commit

Permalink
Problems upto 35
Browse files Browse the repository at this point in the history
  • Loading branch information
skate committed Feb 17, 2012
1 parent 1d484de commit 3d59bd0
Show file tree
Hide file tree
Showing 20 changed files with 525 additions and 27 deletions.
26 changes: 0 additions & 26 deletions 14_Memoize.py

This file was deleted.

29 changes: 29 additions & 0 deletions 18.py
@@ -0,0 +1,29 @@
input_str = '''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'''
str_list = input_str.split("\n")
lst = [[int(j) for j in str_list[i].split()] for i in range(len(str_list))]
ans_lst = lst
str = len(lst)-2
for r in range(str, -1, -1):
for c in range(0, len(lst[r])):
ans_lst[r][c] += max(lst[r+1][c], lst[r+1][c+1])
print ans_lst[0][0]






7 changes: 7 additions & 0 deletions 21.py
@@ -0,0 +1,7 @@
# sum of proper divisors
def pds(n):
return sum(x for x in range(1, n//2+1) if not n%x)

#print d(284)
print sum(i for i in range(1, 10000) if pds(pds(i)) == i and i != pds(i))

16 changes: 16 additions & 0 deletions 23.py
@@ -0,0 +1,16 @@
maxi = 28123+1
# sum of proper divisors
def pds(n):
return sum(x for x in range(1, n//2+1) if not n%x)

O = set(range(1, maxi))
#A set of abundant numbers, numbers are abundant if pds(n) > n
A = set([n for n in range(1, maxi) if pds(n) > n])

#B set of numbers which can be expressed as sum of two abundant numbers
B = set([a1+a2 for a1 in A for a2 in A if a1+a2<=maxi])

#C = O -B set of numbers which can't be expressed as sum of two abun nos.
C = O - B

print sum(C)
4 changes: 4 additions & 0 deletions 24.py
@@ -0,0 +1,4 @@
# print 1000000th permutation
import itertools
a = list[itertools.permutations('0123456789')]
print a[999999]
10 changes: 10 additions & 0 deletions 25.py
@@ -0,0 +1,10 @@
maxi = int('1%s' % ('0' * 999))
st1 = 0
st2 = 1
cnt = 1
while(len(str(st2))<=999):
curr, st1 = st1+st2, st2
st2 = curr
cnt += 1

print cnt, len(str(st2))
11 changes: 11 additions & 0 deletions 26.py
@@ -0,0 +1,11 @@
def get_decimals(num, den, stack =[]):
if num == 0 or den == 0:
return len(stack)
elif num in stack:
return len(stack) -1
else:
a,b = divmod(num, den)
return get_decimals(b*10, den, stack+[num])

print max((get_decimals(1, num), num) for num in range(2, 1001))

50 changes: 50 additions & 0 deletions 27.py
@@ -0,0 +1,50 @@
import math

def isPrime(n):
if(n%2 == 0):
return 0
for i in range(3,int(math.sqrt(n))+1):
if(n%i == 0):
return 0
return 1
'''
for n = 0, we should get a prime, we can ensure the largest prime,
by generating picking the max prime < 1000 hence b = 997
primes = [i for i in range(2, 1000) if isPrime(i)]
print primes[-1]
hence the max we can go is 997
now we simply put the equation as is and let my computer figure out the value.
'''

primes = [i for i in range(2, 1000) if isPrime(i)]

maxi = 997+1
a_max = 0
b_max = 0
i_max = 0

def get_prods(a, b):
global a_max
global b_max
global i_max
i = 0
while((i**2 + a*i + b) in primes):
i += 1
print a, b, i
if(i>i_max):
a_max = a
b_max = b
i_max = i
return 0

for a in range(0, maxi):
for b in range(0, maxi):
get_prods(a, b)
get_prods(a, -b)
get_prods(-a, b)

print a_max*b_max



43 changes: 43 additions & 0 deletions 28.py
@@ -0,0 +1,43 @@
'''Creating one more cycle of numbers, will show us an order,
which will eventually reduce the computation which
we need to perform.
the series from bottom left to top right:
43 21 7 1 3 13 31
re-arranged it looks like this: 1 3 7 13 21 31 43
the difference between the difference of numbers increases by 2
1+2
3+4
7+6
13+8
etc.
the series from top left to bottom right
37 17 5 1 9 25 49
re-arranged it looks like: 1 5 9 17 25 37 49
the difference between the difference of numbers increases by 4 at a cycle length of 2
1+4
5+4
9+8
17+8
25+12
37+12
etc.
'''
s1 = s2 = 0;maxi = 1001;diff1 = 2;diff2 = 4;l1 = [1];l2 = [1]

for cnt in range(1, maxi):
l2.append(l2[-1]+diff2)
if not (cnt&1): diff2+=4
#print l2

for cnt in range(1, maxi):
l1.append(l1[-1]+diff1)
diff1 += 2
#print l1
# removing an extra 1 since in both list1 and list2 one has occured.
print sum(l1) + sum(l2) - 1

7 changes: 7 additions & 0 deletions 29.py
@@ -0,0 +1,7 @@
lst = [a**b for a in range(2, 101) for b in range(2, 101)]
present = []
for i in lst:
if not i in present:
present.append(i)
print len(present)

14 changes: 14 additions & 0 deletions 30.py
@@ -0,0 +1,14 @@
''' We need to figure out an upper bound, since it is not given in the question. Let the number of digits in such a number be 'd'
(9^5)*d [A] will be the maximum sum of such a number.
This maximum sum will always be less than 10^(d-1) [B]
since A > B.
we can find the above relation holds true for d<= 6
hence we should go upto (9^5)*6
'''

high = 354294
chksum = lambda num: sum(int(i)**5 for i in str(num))
ans = sum(i for i in range(10, high+1) if i == chksum(i))
print ans
20 changes: 20 additions & 0 deletions 31.py
@@ -0,0 +1,20 @@
'''
This is the most famous coin change DP problem
'''
coins = [1, 2, 5, 10, 20, 50, 100, 200]
maxi = 200
lst = [1] + [0] * (maxi + 1)
[lst.__setitem__(i, lst[i] + lst[i - c]) for c in coins for i in range(c, maxi+1)]
print lst[200]


'''
BULLSHIT APPROACH
for c in coins:
for i in range(c, maxi+1):
lst[i] += lst[i-c]
'''




35 changes: 35 additions & 0 deletions 32.py
@@ -0,0 +1,35 @@
'''
The case is as follows:
Master String = ABCDEFGHI
x*y = z
all the integers of x, y and z should contribute to
the master string 'uniquely'
len(z) >= len(x) and len(z) >= len(y)
Hence we come to the following conclusion that the solution can always be of the
form
1) ABC*DE = FGHI
2) ABCD*E = FGHI
NOT AB*CD = EFGHI
since 99 * 99 = 9801
'''

import itertools
strn = "123456789"
soln = []
perms = list(itertools.permutations(strn))

for perm in perms:
num = ''.join(list(perm))
# processing form 1
n1 = int(num[0:3]); n2 = int(num[3:5]); n3 = int(num[5:]);
if n1*n2 == n3 and n3 not in soln:
soln.append(n3)
print n1, n2, n3
# processing second form (not the html form)
n1 = int(num[0:4]); n2 = int(num[4:5]); n3 = int(num[5:]);
if n1*n2 == n3 and n3 not in soln:
soln.append(n3)
print n1, n2, n3

print sum(soln)

19 changes: 19 additions & 0 deletions 33.py
@@ -0,0 +1,19 @@
num = 1;den = 1;
for n in range(10, 100):
for d in range(n+1, 100):
if n%10 != 0 and d%10 != 0:
n = float(n); d = float(d)
num1, num2 = divmod(n, 10)
den1, den2 = divmod(d, 10)
if num2 == den1:
chk1 = num1/den2
chk2 = n/d
if chk1 == chk2:
print n, d
num *= n; den *= d;


print num, den



9 changes: 9 additions & 0 deletions 34.py
@@ -0,0 +1,9 @@
maxi = 10000000
facti = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
ans = 0
for i in range(10, maxi):
b = map(int, str(i))
if i == sum([facti[j] for j in b]):
ans += i
print i
print ans
30 changes: 30 additions & 0 deletions 35.py
@@ -0,0 +1,30 @@
import math
maxi = 1000000+1
lst = range(2, maxi)
ans = []


def isPrime(n):
if(n%2 == 0):
return 0
for i in range(3,int(math.sqrt(n))+1):
if(n%i == 0):
return 0
return 1

primes = [i for i in lst if isPrime(i)]
print "primes generated"

def chk_circ(num):
s = str(num)
perms = [int(s[i:] + s[:i]) for i in range(len(s))]
for numstr in perms:
if numstr not in primes:
return False
for numstr in perms:
ans.append(numstr)
return True
[chk_circ(i) for i in primes if i not in ans]
print ans
print len(ans)

0 comments on commit 3d59bd0

Please sign in to comment.