├── 33 ├── hrank.py └── euler.py ├── 39 └── hrank.py ├── #13.py ├── #29.py ├── .gitignore ├── #16.py ├── #28.py ├── #36.py ├── README.md ├── #20.py ├── #48.py ├── #6.py ├── #2.py ├── #44.py ├── #40.py ├── p52.py ├── #34.py ├── #25.py ├── #9.py ├── #39.py ├── #30.py ├── p53.py ├── #5.py ├── #8.py ├── #1.py ├── #3.py ├── #4.py ├── #14.py ├── #22.py ├── #18.py ├── #27.py ├── #31.py ├── #11.py ├── #26.py ├── #15.py ├── #35.py ├── #41.py ├── #45.py ├── #32.py ├── #37.py ├── #24.py ├── #42.py ├── #38.py ├── #10.py ├── #7.py ├── #12.py ├── #50.py ├── #43.py ├── #21.py ├── #46.py ├── #47.py ├── #49.py ├── #19.py └── #23.py /#13.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | t = int(next(inputs)) 4 | sum_all = 0 5 | for i in range(t): 6 | n = int(next(inputs)) 7 | sum_all += n 8 | print str(sum_all)[:10] -------------------------------------------------------------------------------- /#29.py: -------------------------------------------------------------------------------- 1 | n = 100 2 | seq = [] 3 | for a in xrange(2, n+1): 4 | for b in xrange(2, n+1): 5 | if pow(a, b) not in seq: 6 | seq.append(pow(a, b)) 7 | 8 | print len(seq) 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.sublime-project 2 | *.sublime-workspace 3 | 4 | # Byte-compiled / optimized / DLL files 5 | __pycache__/ 6 | *.py[cod] 7 | *$py.class 8 | 9 | names.txt 10 | 501.py 11 | p042_words.txt -------------------------------------------------------------------------------- /#16.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | t = int(next(inputs)) 4 | for i in range(t): 5 | n = int(next(inputs)) 6 | sum_dig = 0 7 | for x in str(pow(2,n)): 8 | sum_dig +=int(x) 9 | print sum_dig -------------------------------------------------------------------------------- /#28.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | t = int(next(inputs)) 4 | for x in xrange(t): 5 | L = int(next(inputs)) 6 | n = (L-1) // 2 7 | sum_d = (16*n**3 + 30*n**2 + 26*n + 3) // 3 8 | print sum_d % (10**9+7) -------------------------------------------------------------------------------- /#36.py: -------------------------------------------------------------------------------- 1 | sum_ = 0 2 | for num in xrange(10**6): 3 | # If Palindrome in base-10 and base-2 4 | binary = bin(num)[2:] 5 | if str(num) == str(num)[::-1] and binary == binary[::-1]: 6 | sum_ += num 7 | 8 | print sum_ 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Goodbye 2 | 3 | Say Goodbye to problems with my solutions to [Project Euler](https://projecteuler.net/) problems from [here](https://www.hackerrank.com/contests/projecteuler/challenges). 4 | 5 | I will be using Python 2.7 . -------------------------------------------------------------------------------- /#20.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | t = int(next(inputs)) 4 | for i in range(t): 5 | n = int(next(inputs)) 6 | fac = 1 7 | for x in xrange(2,n+1): 8 | fac *= x 9 | print sum([int(dig) for dig in str(fac)]) 10 | -------------------------------------------------------------------------------- /#48.py: -------------------------------------------------------------------------------- 1 | def _sum(num): 2 | """ 3 | Returns the sum of numbers upto `num`. 4 | """ 5 | x = 0 6 | for i in range(1, num+1): 7 | x += i**i 8 | return x 9 | 10 | # Prints last 10 digits of the sum. 11 | print str(_sum(1000))[-10:] 12 | -------------------------------------------------------------------------------- /#6.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | t = int(next(inputs)) 4 | 5 | for i in range(t): 6 | n = int(next(inputs)) 7 | sum_square = n*(n+1)*(2*n+1)/6 8 | sum_n = n*(n+1)/2 9 | square_sum = sum_n*sum_n 10 | print square_sum - sum_square -------------------------------------------------------------------------------- /#2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs=sys.stdin 3 | t = int(next(inputs)) 4 | for i in range(t): 5 | n,a,b,sum = int(next(inputs)),1,2,0 6 | while b < n: 7 | if b % 2 == 0: 8 | sum += b 9 | c = a+b 10 | a,b = b,c 11 | print sum 12 | -------------------------------------------------------------------------------- /#44.py: -------------------------------------------------------------------------------- 1 | def x(): 2 | ps = set() 3 | i = 0 4 | while True: 5 | i += 1 6 | p = (3*i*i - i) / 2 7 | ps.add(p) 8 | for n in ps: 9 | if p-n in ps and p-2*n in ps: 10 | return p-2*n 11 | 12 | 13 | print x() 14 | -------------------------------------------------------------------------------- /#40.py: -------------------------------------------------------------------------------- 1 | fraction, i = '.', 1 2 | 3 | while True: 4 | fraction += str(i) 5 | i += 1 6 | if len(fraction) > 1000000: 7 | break 8 | 9 | prod, index = 1, 1 10 | 11 | while index <= 1000000: 12 | prod *= int(fraction[index]) 13 | index *= 10 14 | 15 | print prod 16 | -------------------------------------------------------------------------------- /p52.py: -------------------------------------------------------------------------------- 1 | x = 1 2 | while True: 3 | x2, x3, x4, x5, x6 = 2*x,3*x, 4*x, 5*x, 6*x 4 | x2_list, x3_list, x4_list, x5_list, x6_list = sorted(list(str(x2))), sorted(list(str(x3))), sorted(list(str(x4))), sorted(list(str(x5))), sorted(list(str(x6))) 5 | if x2_list == x3_list == x4_list == x5_list == x6_list: 6 | print(x) 7 | break 8 | x += 1 9 | -------------------------------------------------------------------------------- /#34.py: -------------------------------------------------------------------------------- 1 | 2 | Facs = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880] 3 | 4 | 5 | def sum_Fac_digits(num): 6 | sum_dig_fac = 0 7 | for x in str(num): 8 | sum_dig_fac += Facs[int(x)] 9 | return sum_dig_fac 10 | 11 | sum_ = 0 12 | for i in range(3, 50000): 13 | if i == sum_Fac_digits(i): 14 | sum_ += i 15 | 16 | print sum_ 17 | -------------------------------------------------------------------------------- /#25.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | t = int(next(inputs)) 4 | for x in xrange(t): 5 | n = int(next(inputs)) 6 | a,b=1,1 7 | digit = 1 8 | index = 1 9 | while digit: 10 | if len(str(a)) == n: 11 | print index 12 | digit = 0 13 | else: 14 | c = a + b 15 | a = b 16 | b = c 17 | index += 1 18 | -------------------------------------------------------------------------------- /#9.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | t = int(next(inputs)) 4 | for i in range(t): 5 | n = int(next(inputs)) 6 | flag = 0 7 | prod = [] 8 | for a in range(1, n/2): 9 | b = n*(n-2*a)/(2*(n-a)) 10 | if a+b < n and pow(a, 2) + pow(b, 2) == pow(n-a-b, 2): 11 | flag = 1 12 | prod.append(a*b*(n-a-b)) 13 | if flag == 0: 14 | print -1 15 | else: 16 | print max(prod) 17 | -------------------------------------------------------------------------------- /#39.py: -------------------------------------------------------------------------------- 1 | # key: perimeter 2 | # value: no. of solutions 3 | peri_dicts = {} 4 | 5 | # sides : a, b, c of triangle 6 | for n in xrange(120, 1001): 7 | peri_dicts[n] = 0 8 | for a in xrange(1, n/3): 9 | b = n*(n-2*a)/(2*(n-a)) 10 | if a+b < n and pow(a, 2) + pow(b, 2) == pow(n-a-b, 2): 11 | peri_dicts[n] += 1 12 | 13 | max_val = max(peri_dicts.values()) 14 | for key, value in peri_dicts.items(): 15 | if value == max_val: 16 | print key 17 | -------------------------------------------------------------------------------- /#30.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | digit = int(next(inputs)) 4 | num = 10 5 | pos = [] 6 | # 9^n x (n-1) is actually more than adequate, which reduces the search range 7 | # and improves the performance of the program. 8 | maximum = 9**digit * (digit-1) 9 | while num < maximum: 10 | n = num 11 | test = 0 12 | while n > 0: 13 | test += pow(n % 10, digit) 14 | n = n/10 15 | if num == test: 16 | pos.append(num) 17 | num += 1 18 | print sum(pos) 19 | -------------------------------------------------------------------------------- /p53.py: -------------------------------------------------------------------------------- 1 | ncr_map = dict() 2 | 3 | def NCR(n, r): 4 | global ncr_map 5 | if n == r or r == 0: 6 | return 1 7 | if (n, n - r) in ncr_map: 8 | return ncr_map[ (n, n - r) ] 9 | if (n, r) in ncr_map: 10 | return ncr_map[ (n, r) ] 11 | value = NCR(n-1, r-1) + NCR(n - 1, r) 12 | ncr_map[ (n, r) ] = value 13 | return value 14 | 15 | 16 | count = 0 17 | for n in range(1, 101): 18 | print(n) 19 | for r in range(1, n + 1): 20 | ncr = NCR(n, r) 21 | if ncr > 1000000: 22 | count+=1 23 | 24 | print(count) -------------------------------------------------------------------------------- /#5.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def egcd(a, b): 4 | "To find gcd of two numbers." 5 | if a == 0: 6 | return (b, 0, 1) 7 | else: 8 | g, y, x = egcd(b % a, a) 9 | return (g, x - (b // a) * y, y) 10 | 11 | inputs = sys.stdin 12 | t = int(next(inputs)) 13 | for i in range(t): 14 | n = int(next(inputs)) 15 | i,res = 1,2 16 | while res <= n: 17 | if i % res != 0: 18 | gcd, _, _ = egcd(i,res) 19 | i = i*res/gcd 20 | res += 1 21 | print i -------------------------------------------------------------------------------- /#8.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | t = int(next(inputs)) 4 | for i in range(t): 5 | n, k = next(inputs)[:-1].split(' ') 6 | n, k = int(n), int(k) 7 | num = next(inputs)[:-1] 8 | prod = [] 9 | textnum = [] 10 | for u in range(len(num)): 11 | if len(num[u:u+k]) == k: 12 | textnum.append(num[u:u+k]) 13 | for part in textnum: 14 | tmp = 1 15 | for j in range(len(part)): 16 | tmp *= int(part[j]) 17 | prod.append(tmp) 18 | 19 | print max(prod) 20 | -------------------------------------------------------------------------------- /#1.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | import sys 3 | inputs=sys.stdin 4 | t = int(next(inputs)) 5 | for i in range(t): 6 | n = int(next(inputs)) 7 | three,five,both = 0,0,0 8 | if n%3 == 0: 9 | three = n/3 - 1 10 | else: 11 | three = n/3 12 | if n%5 == 0: 13 | five = n/5 - 1 14 | else: 15 | five = n/5 16 | if n%15 == 0: 17 | both = n/15 - 1 18 | else: 19 | both = n/15 20 | print 3*three*(three+1)/2 + 5*five*(five+1)/2 - 15*both*(both+1)/2 21 | -------------------------------------------------------------------------------- /#3.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from math import sqrt 3 | 4 | # take input from stdin 5 | inputs=sys.stdin 6 | # no. of test cases. 7 | t = int(next(inputs)) 8 | 9 | for i in range(t): 10 | n = int(next(inputs)) 11 | # List of prime factors 12 | pf = [] 13 | if n%2 == 0: 14 | pf.append(2) 15 | while n%2 == 0: 16 | n = n/2 17 | for i in range(3,int(sqrt(n))+1,2): 18 | if n%i == 0: 19 | pf.append(i) 20 | while n%i == 0: 21 | n = n/i 22 | if n > 2: 23 | pf.append(n) 24 | print max(pf) -------------------------------------------------------------------------------- /#4.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def findpal(n): 4 | pals,i = 0,999 5 | while i > 99: 6 | j = 999 7 | while j >= i: 8 | prod = i*j 9 | j = j-1 10 | if prod < pals: 11 | break 12 | if str(prod)==(str(prod)[::-1]) and prod < n: 13 | if prod > pals: 14 | pals = prod 15 | 16 | i = i-1 17 | return pals 18 | inputs = sys.stdin 19 | t = int(next(inputs)) 20 | 21 | for i in range(t): 22 | n = int(next(inputs)) 23 | print findpal(n) -------------------------------------------------------------------------------- /#14.py: -------------------------------------------------------------------------------- 1 | import time 2 | from itertools import count,islice 3 | start = time.time() 4 | 5 | def collatz(n, count=1): 6 | while n > 1: 7 | count += 1 8 | if n % 2 == 0: 9 | n = n/2 10 | else: 11 | count += 1 12 | n = (3*n + 1)/2 13 | return count 14 | 15 | max = [0,0] 16 | for i in islice(count(2),0,20): 17 | c = collatz(i) 18 | if c > max[0]: 19 | max[0] = c 20 | max[1] = i 21 | 22 | elapsed = (time.time() - start) 23 | print "found %s at length %s in %s seconds" % (max[1],max[0],elapsed) -------------------------------------------------------------------------------- /#22.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import string 3 | alphas = {} 4 | for alpha in string.uppercase: 5 | alphas[alpha] = ord(alpha) - 65 + 1 6 | 7 | inputs = sys.stdin 8 | t = int(next(inputs)) 9 | names = [] 10 | for i in range(t): 11 | n = next(inputs)[:-1] 12 | names.append(n) 13 | 14 | names.sort() 15 | q = int(next(inputs)) 16 | for i in range(q): 17 | if i == q-1: 18 | word = next(inputs) 19 | else: 20 | word = next(inputs)[:-1] 21 | ele_score = 0 22 | for element in word: 23 | ele_score += alphas[element] 24 | print ele_score*(names.index(word)+1) 25 | -------------------------------------------------------------------------------- /#18.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | t = int(next(inputs)) 4 | for i in range(t): 5 | n = int(next(inputs)) 6 | a = [] 7 | for j in range(n): 8 | k = next(inputs) 9 | if j < n-1: 10 | k = k[:-1] 11 | k = k.split(' ') 12 | a.append(k) 13 | for row in range(len(a)): 14 | for col in range(len(a[row])): 15 | a[row][col] = int(a[row][col]) 16 | row = len(a) - 2 17 | while row >= 0: 18 | for i in range(len(a[row])): 19 | a[row][i] += max(a[row+1][i],a[row+1][i+1]) 20 | row -= 1 21 | print a[0][0] -------------------------------------------------------------------------------- /#27.py: -------------------------------------------------------------------------------- 1 | from itertools import islice,count 2 | from math import sqrt 3 | import sys 4 | def isPrime(n): 5 | if n < 2: return False 6 | for i in islice(count(3,2), int(sqrt(n)-1)//2): 7 | if n%i ==0: 8 | return False 9 | return True 10 | inputs = sys.stdin 11 | t = int(next(inputs)) 12 | nmax = 0 13 | best_a = 0 14 | best_b = 0 15 | for b in xrange(3,t,2): 16 | if isPrime(b): 17 | for a in xrange(-b,b,2): 18 | n = 1 19 | while isPrime(n*n + a*n + b): n += 1 20 | if n>nmax: 21 | nmax = n 22 | best_a = a 23 | best_b = b 24 | print best_a,best_b -------------------------------------------------------------------------------- /#31.py: -------------------------------------------------------------------------------- 1 | import sys 2 | def count(S, m, n): 3 | """ 4 | S -> A list of coins 5 | m -> No. of coins 6 | n -> Target coin 7 | """ 8 | Matrix = [1]+[0]*n 9 | for coin in S: 10 | for i in range(coin, n+1): 11 | Matrix[i] += Matrix[i-coin] 12 | return Matrix[n] 13 | inputs=sys.stdin 14 | t = int(next(inputs)) 15 | coins = [1, 2, 5, 10, 20, 50, 100, 200] 16 | results = {} 17 | for i in xrange(t): 18 | target = int(next(inputs)) 19 | if target in results: 20 | print results[target] 21 | else: 22 | results[target] = count(coins,len(coins),target)%(10**9+7) 23 | print results[target] -------------------------------------------------------------------------------- /#11.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | mat = [] 4 | for i in range(20): 5 | mat.append([]) 6 | if i <19:row = next(inputs)[:-1].split(' ') 7 | else:row = next(inputs).split(' ') 8 | for j in row: mat[i].append(int(j)) 9 | maxp = 0 10 | for i in range(20): 11 | for j in range(17): 12 | up_down = max(mat[i][j] * mat[i][j+1] * mat[i][j+2] * mat[i][j+3], 13 | mat[j][i] * mat[j+1][i] * mat[j+2][i] * mat[j+3][i]) 14 | if i < 16: 15 | diag = max(mat[i][j] * mat[i+1][j+1] * mat[i+2][j+2] * mat[i+3][j+3], 16 | mat[i][j+3] * mat[i+1][j+2] * mat[i+2][j+1] * mat[i+3][j]) 17 | maxp = max(maxp, up_down, diag) 18 | print maxp -------------------------------------------------------------------------------- /#26.py: -------------------------------------------------------------------------------- 1 | from itertools import islice,count 2 | from math import sqrt 3 | import sys 4 | def isPrime(n): 5 | if n < 2: return False 6 | for i in islice(count(3,2), int(sqrt(n)-1)//2): 7 | if n%i ==0: 8 | return False 9 | return True 10 | 11 | inputs = sys.stdin 12 | t = int(next(inputs)) 13 | for x in xrange(t): 14 | l = int(next(inputs)) 15 | if l%2 == 1: 16 | l -= 1 17 | for d in xrange(l-1,1,-2): 18 | if d == 5: 19 | pass 20 | elif isPrime(d): 21 | power = 1 22 | while pow(10,power,d) != 1: 23 | power += 1 24 | if d-1 == power: 25 | break 26 | 27 | print d -------------------------------------------------------------------------------- /#15.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | t = int(next(inputs)) 4 | for i in range(t): 5 | if t == 1: 6 | m,n = next(inputs).split(' ') 7 | m,n=int(m)+1,int(n)+1 8 | elif i == 0: 9 | m,n = next(inputs).split(' ') 10 | m,n = int(m)+1,int(n[:-1])+1 11 | else: 12 | m,n = next(inputs).split(' ') 13 | m,n=int(m)+1,int(n)+1 14 | path = [[0]*n for _ in xrange(m)] 15 | for i in xrange(m): 16 | path[i][0] = 1 17 | for j in xrange(n): 18 | path[0][j] = 1 19 | 20 | for i in xrange(1,m): 21 | for j in xrange(1,n): 22 | path[i][j] = path[i-1][j] + path[i][j-1] 23 | 24 | print path[m-1][n-1]%(pow(10,9)+7) 25 | -------------------------------------------------------------------------------- /#35.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | from itertools import count, islice 3 | 4 | 5 | def allRot(num): 6 | # Gives all rotation for this num. 7 | rotations = [] 8 | for n in range(len(num)): 9 | rotations.append(num[n:]+num[:n]) 10 | return rotations 11 | 12 | 13 | def isPrime(num_list): 14 | for num in num_list: 15 | n = int(num) 16 | if n < 2 or n % 2 == 0: 17 | return False 18 | for i in islice(count(3, 2), int(sqrt(n)-1)//2): 19 | if n % i == 0: 20 | return False 21 | return True 22 | 23 | counter = 1 24 | for num in xrange(3, 10**6, 2): 25 | if isPrime(allRot(str(num))): 26 | counter += 1 27 | 28 | print counter 29 | -------------------------------------------------------------------------------- /39/hrank.py: -------------------------------------------------------------------------------- 1 | import sys 2 | inputs = sys.stdin 3 | tests = int(next(inputs)) 4 | # key: perimeter 5 | # value: no. of solutions 6 | peri_dicts = {0: 0} 7 | for i in range(tests): 8 | val = int(next(inputs)) 9 | 10 | # sides : a, b, c of triangle 11 | if max(peri_dicts.keys()) < val: 12 | for n in xrange(max(peri_dicts.keys())+1, val+1): 13 | peri_dicts[n] = 0 14 | for a in xrange(1, n/3): 15 | b = n*(n-2*a)/(2*(n-a)) 16 | if a+b < n and pow(a, 2) + pow(b, 2) == pow(n-a-b, 2): 17 | peri_dicts[n] += 1 18 | 19 | max_val = max(peri_dicts.values()) 20 | for key, value in peri_dicts.items(): 21 | if value == max_val: 22 | print key 23 | break 24 | -------------------------------------------------------------------------------- /#41.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | from itertools import count, islice 3 | 4 | 5 | def check_pandigital(string, s=9): 6 | string = str(string) 7 | return len(string) == s and not '1234567890'[:s].strip(string) 8 | 9 | 10 | def isPrime(n): 11 | if n < 2 or n % 2 == 0: 12 | return False 13 | for i in islice(count(3, 2), int(sqrt(n)-1)//2): 14 | if n % i == 0: 15 | return False 16 | return True 17 | 18 | 19 | def PrimePandigital(): 20 | """The pandigital for n=9,8,6,5 is always a multiple of 3. 21 | Returns 22 | ------- 23 | Maximum pandigital prime. 24 | """ 25 | for x in xrange(7654321, 7123456+1, -2): 26 | if isPrime(x) and check_pandigital(x, 7): 27 | return x 28 | 29 | print PrimePandigital() 30 | -------------------------------------------------------------------------------- /#45.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | 3 | 4 | def isPentagonal(num): 5 | """Checks if the given number is a Pentagonal number or not.""" 6 | n = (1 + sqrt(1 + 24*num))/6 7 | if int(n) == n: 8 | return True 9 | return False 10 | 11 | 12 | def isHexagonal(num): 13 | """Checks if the given number is a Hexagonal number or not.""" 14 | n = (1 + sqrt(1 + 8*num))/4 15 | if int(n) == n: 16 | return True 17 | return False 18 | 19 | 20 | def findTriangular(): 21 | """ 22 | Finds the triangular number which is Pentagonal and Hexagonal. 23 | """ 24 | term = 285 25 | while True: 26 | term += 1 27 | triang = term*(term+1)/2 28 | if isPentagonal(triang) and isHexagonal(triang): 29 | return triang 30 | 31 | 32 | print findTriangular() 33 | -------------------------------------------------------------------------------- /#32.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def check_pandigital(string, s=9): 5 | string = str(string) 6 | return len(string) == s and not '1234567890'[:s].strip(string) 7 | 8 | inputs = sys.stdin 9 | n = int(next(inputs)) 10 | pandigital = set() 11 | if n > 7: 12 | for i in range(2, 60): 13 | if i < 10: 14 | start = 1234 15 | else: 16 | start = 123 17 | for j in range(start, 10000//i): 18 | string = str(i)+str(j)+str(i*j) 19 | if check_pandigital(string, n): 20 | pandigital.add(i*j) 21 | else: 22 | pandigital = set() 23 | for i in range(1, 10): 24 | for j in range(1000): 25 | string = str(i)+str(j)+str(i*j) 26 | if check_pandigital(string, n): 27 | pandigital.add(i*j) 28 | print sum(pandigital) 29 | -------------------------------------------------------------------------------- /#37.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | from itertools import count, islice 3 | 4 | 5 | def isPrime(num_list): 6 | for num in num_list: 7 | n = int(num) 8 | if n < 2 or n % 2 == 0 and n != 2: 9 | return False 10 | for i in islice(count(3, 2), int(sqrt(n)-1)//2): 11 | if n % i == 0: 12 | return False 13 | return True 14 | 15 | 16 | def all_subs(num): 17 | str_num = str(num) 18 | all_substrings = set() 19 | for i in range(len(str_num)): 20 | all_substrings.add(str_num[i:]) 21 | all_substrings.add(str_num[:i]) 22 | all_substrings.remove('') 23 | return all_substrings 24 | 25 | counter, sum_, num = 0, 0, 11 26 | while counter < 11: 27 | if isPrime(all_subs(num)): 28 | sum_ += num 29 | counter += 1 30 | num += 2 31 | print sum_ 32 | -------------------------------------------------------------------------------- /#24.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def Factor(num): 5 | if num < 0: 6 | return 0 7 | fac = 1 8 | for p in range(2, num+1): 9 | fac *= p 10 | return fac 11 | 12 | perm = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'] 13 | N = len(perm) 14 | inputs = sys.stdin 15 | t = int(next(inputs)) 16 | for x in xrange(t): 17 | num = int(next(inputs)) 18 | permNum = [] 19 | remain = num - 1 20 | 21 | numbers = [] 22 | for i in range(N): 23 | numbers.append(i) 24 | 25 | for i in range(1, N): 26 | j = remain / Factor(N - i) 27 | remain = remain % Factor(N - i) 28 | permNum.append(perm[numbers[j]]) 29 | numbers.pop(j) 30 | if remain == 0: 31 | break 32 | 33 | for i in range(len(numbers)): 34 | permNum.append(perm[numbers[i]]) 35 | 36 | print ''.join(permNum) 37 | -------------------------------------------------------------------------------- /#42.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | 3 | 4 | def wordValue(word): 5 | """Calculate the word value. 6 | 7 | Converting each letter in a word to a number corresponding to its 8 | alphabetical position and adding these values we form a word value. 9 | """ 10 | val = 0 11 | for alpha in word[1:len(word)-1]: 12 | val += ord(alpha)-64 13 | return val 14 | 15 | 16 | def isTriangular(val): 17 | """ 18 | val -- word value 19 | n -- nth term 20 | """ 21 | n = (sqrt(1+8*val)-1)/2 22 | if int(n) == n: 23 | return True 24 | return False 25 | 26 | 27 | with open('p042_words.txt') as f: 28 | all_words = f.read().split(",") 29 | count = 0 30 | for word in all_words: 31 | # Calculate the value of word and if it's triangular! 32 | if isTriangular(wordValue(word)): 33 | count += 1 34 | print count 35 | -------------------------------------------------------------------------------- /#38.py: -------------------------------------------------------------------------------- 1 | """ 2 | If the fixed number is 2 digit we wont be able to generate a 9 digit number 3 | since n = 3 yields an 8 digit number and n=4 yields an 11 digit number. 4 | Same goes for 3 digit numbers where we end at 7 or 11 digits in the result. 5 | That leaves us with a four digit number starting with 9. 6 | """ 7 | 8 | 9 | def check_pandigital(string, s=9): 10 | """ 11 | Checks if the passed `string` is pandigital or not. 12 | """ 13 | return len(string) == s and not '1234567890'[:s].strip(string) 14 | 15 | 16 | def largest(): 17 | """ 18 | Gives the largest pandigital number. 19 | """ 20 | max_pan = 918273645 21 | for x in range(9123, 9999+1): 22 | possible_pan = str(x) + str(x*2) 23 | if check_pandigital(possible_pan) and int(possible_pan) > max_pan: 24 | max_pan = int(possible_pan) 25 | return max_pan 26 | 27 | 28 | print(largest()) 29 | -------------------------------------------------------------------------------- /#10.py: -------------------------------------------------------------------------------- 1 | # Todo: for better result, precompute sums! 2 | import sys 3 | 4 | def sumPrime(n): 5 | sieve = [True] * n 6 | for i in xrange(3,int(n**0.5)+1,2): 7 | if sieve[i]: 8 | sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1) 9 | return [2] + [i for i in xrange(3,n,2) if sieve[i]] 10 | 11 | inputs = sys.stdin 12 | t = int(next(inputs)) 13 | primes = [2] 14 | prime_million = sumPrime(6000000+1) 15 | sums = {1:0} 16 | for i in range(t): 17 | n = int(next(inputs)) 18 | if n == 1: 19 | print 0 20 | elif n < 6000000: 21 | if n in sums: 22 | print sums[n] 23 | else: 24 | sum_p = 0 25 | for x in prime_million: 26 | if x <= n: 27 | sum_p += x 28 | else: 29 | break 30 | sums[n] = sum_p 31 | print sum_p 32 | else: 33 | print sum(sumPrime(n+1)) -------------------------------------------------------------------------------- /#7.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from math import sqrt 3 | from itertools import count, islice 4 | 5 | 6 | def isPrime(n): 7 | if n < 2: 8 | return False 9 | for i in islice(count(3, 2), int(sqrt(n)-1)//2): 10 | if n % i == 0: 11 | return False 12 | return True 13 | 14 | inputs = sys.stdin 15 | t = int(next(inputs)) 16 | primes = [2] 17 | for i in range(t): 18 | n = int(next(inputs)) 19 | if n == 1: 20 | print 2 21 | elif n < len(primes): 22 | print primes[n-1] 23 | else: 24 | if max(primes) == 2: 25 | start = 3 26 | prime = 3 27 | else: 28 | start = max(primes) 29 | prime = max(primes)+2 30 | while True: 31 | if n <= len(primes): 32 | break 33 | if isPrime(prime): 34 | primes.append(prime) 35 | prime += 2 36 | print primes[n-1] 37 | -------------------------------------------------------------------------------- /#12.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from math import sqrt 3 | inputs = sys.stdin 4 | t = int(next(inputs)) 5 | l = [{1:1}] 6 | for i in range(t): 7 | c = int(next(inputs)) 8 | if c==1: 9 | print 3 10 | elif c + 1 <= max(l).values()[0]: 11 | flag = 0 12 | for data in l: 13 | for dicta in data: 14 | if c+1 <= data[dicta]: 15 | print dicta 16 | flag = 1 17 | break 18 | if flag == 1: break 19 | else: 20 | element = max(l).keys()[0] 21 | if element == 1: 22 | n = 2 23 | else: 24 | n = (int(sqrt(1+4*element*2))-1)/2 + 1 25 | fac = 1 26 | num =1 27 | while fac < c+1: 28 | num = n*(n+1)/2 29 | fac = sum(2 for i in xrange(1, int(sqrt(num)+1)) if not num % i) 30 | l.append({num:fac}) 31 | n += 1 32 | print max(l).keys()[0] -------------------------------------------------------------------------------- /33/hrank.py: -------------------------------------------------------------------------------- 1 | n = int(raw_input("N")) 2 | k = int(raw_input("k")) 3 | d = 1 4 | for i in range(10**(n-1), 10**n): 5 | for j in range(i, 10**n): 6 | num = i 7 | den = j 8 | if num % 10 == 0 or den % 10 == 0: 9 | continue 10 | if num == den: 11 | continue 12 | if str(num)[0] == str(den)[0]: 13 | if num/float(den) == int(str(num)[-1])/float(int(str(den)[-1])): 14 | d *= num/float(den) 15 | elif str(num)[0] == str(den)[-1]: 16 | if num/float(den) == int(str(num)[-1])/float(int(str(den)[0])): 17 | d *= num/float(den) 18 | elif str(num)[-1] == str(den)[0]: 19 | if num/float(den) == int(str(num)[0])/float(int(str(den)[-1])): 20 | d *= num/float(den) 21 | elif str(num)[-1] == str(den)[-1]: 22 | if num/float(den) == int(str(num)[0])/float(int(str(den)[0])): 23 | d *= num/float(den) 24 | else: 25 | continue 26 | print 1/d 27 | -------------------------------------------------------------------------------- /#50.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | 3 | 4 | def sieve(limit): 5 | """Computes the sieve of Eratosthenes for values up to limit included.""" 6 | primes = [True] * (limit + 1) 7 | primes[0] = primes[1] = False 8 | for i in range(2, int(sqrt(limit)) + 1): 9 | if primes[i]: 10 | for j in range(i * i, limit + 1, i): 11 | primes[j] = False 12 | return primes 13 | 14 | 15 | def sum_primes(): 16 | """ 17 | """ 18 | limit = 1000000 19 | primes = sieve(limit) 20 | list_primes = [i for i, p in enumerate(primes) if p] 21 | max_len, max_sum = 0, 0 22 | for i in range(len(list_primes)): 23 | for j in range(i + max_len + 1, len(list_primes)): 24 | s = sum(list_primes[i:j]) # could use sum array here 25 | if s > limit: 26 | break 27 | elif primes[s]: 28 | assert j - i > max_len 29 | max_len, max_sum = j - i, s 30 | return max_sum 31 | 32 | 33 | if __name__ == '__main__': 34 | print sum_primes() 35 | -------------------------------------------------------------------------------- /#43.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | 3 | 4 | def getPerm(): 5 | """Get all the permutations of 0-9""" 6 | return permutations(range(10)) 7 | pass 8 | 9 | 10 | def substring_divisible(pandigital): 11 | """ 12 | Check substring divisiblity as per problem statement here 13 | https://projecteuler.net/problem=43 14 | """ 15 | Prime_list = [2, 3, 5, 7, 11, 13, 17] 16 | for x in range(1, len(pandigital)-2): 17 | if int(pandigital[x:x+3]) % Prime_list[x-1] != 0: 18 | return False 19 | return True 20 | 21 | 22 | def sumall(): 23 | """ 24 | Calculate sum of all 0-9 pandigital no.s with substring divisiblity. 25 | """ 26 | # all perms 27 | perms = getPerm() 28 | count = 0 29 | while True: 30 | try: 31 | # Get next permutation from generator! 32 | next_permutation = next(perms) 33 | num = ''.join(str(x) for x in next_permutation) 34 | if substring_divisible(num): 35 | count += int(num) 36 | except: 37 | return count 38 | 39 | print sumall() 40 | -------------------------------------------------------------------------------- /#21.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from math import sqrt 3 | def sum_divisors(num): 4 | """ 5 | Gives a dict of prime factors of the number num, along with their powers. 6 | 7 | Then calculates the sum of all possible divisors from the prime factors. 8 | """ 9 | n = num 10 | prime_fac = {} 11 | if num%2 == 0: 12 | prime_fac[2] = 0 13 | while num%2 == 0: 14 | prime_fac[2] += 1 15 | num /= 2 16 | for i in range(3,int(sqrt(num))+1,2): 17 | if num%i == 0: 18 | prime_fac[i] = 0 19 | while num%i == 0: 20 | prime_fac[i] += 1 21 | num /= i 22 | if num > 2: 23 | prime_fac[num] = 1 24 | sum_factors = 1 25 | for prime in prime_fac: 26 | sum_factors *= (pow(prime, prime_fac[prime]+1)-1)/(prime -1) 27 | return sum_factors-n 28 | 29 | inputs = sys.stdin 30 | t = int(next(inputs)) 31 | for i in range(t): 32 | n = int(next(inputs)) 33 | am = [] 34 | for i in xrange(2,n+1): 35 | num = sum_divisors(i) 36 | if i == sum_divisors(num) and num != i and i not in am and num <= n: 37 | am.append(i) 38 | am.append(num) 39 | print sum(am) -------------------------------------------------------------------------------- /#46.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | from itertools import count, islice 3 | 4 | 5 | def isPrime(n): 6 | if n < 2 or n % 2 == 0: 7 | return False 8 | for i in islice(count(3, 2), int(sqrt(n)-1)//2): 9 | if n % i == 0: 10 | return False 11 | return True 12 | 13 | 14 | def getodd_prime(num): 15 | """ 16 | Returns all odd prime no.s less than num. 17 | """ 18 | num -= 2 19 | while num >= 3: 20 | if isPrime(num): 21 | yield num 22 | num -= 2 23 | 24 | 25 | def conjecture(num): 26 | """ 27 | Parameter 28 | --------- 29 | num : int 30 | """ 31 | if isPrime(num): 32 | return True 33 | odds = getodd_prime(num) 34 | n = num 35 | while n > 3: 36 | try: 37 | prime = next(odds) 38 | if (num - prime) % 2 == 0: 39 | diff = (num - prime)/2 40 | if sqrt(diff) == int(sqrt(diff)): 41 | return True 42 | n -= 2 43 | except: 44 | break 45 | return False 46 | 47 | if __name__ == '__main__': 48 | num = 33 49 | while conjecture(num): 50 | num += 2 51 | print num 52 | -------------------------------------------------------------------------------- /#47.py: -------------------------------------------------------------------------------- 1 | # Find the first four consecutive integers to have four distinct prime factor4. 2 | from math import sqrt, ceil 3 | 4 | 5 | def getPrimefactors(num): 6 | """ 7 | Returns a set of prime factors of the `num`. 8 | """ 9 | primes = set() 10 | while num % 2 == 0: 11 | primes.add(2) 12 | num /= 2 13 | for i in xrange(3, int(ceil(sqrt(num)+1)), 2): 14 | while num % i == 0: 15 | primes.add(i) 16 | num /= i 17 | if num > 2: 18 | primes.add(num) 19 | return len(primes) 20 | 21 | 22 | def distinctPrimes(num, count): 23 | """ 24 | Parameter 25 | --------- 26 | num -- int 27 | The no. of distinct primes the consecutive numbers should have. 28 | count -- int 29 | The no. of consecutive numbers. 30 | """ 31 | # Count of consecutive numbers found with `num` distinct primes. 32 | count_cons = 1 33 | # First number with 4 distinct primes. 34 | start = 2*3*5*7 35 | while count_cons != count: 36 | start += 1 37 | if getPrimefactors(start) == num: 38 | count_cons += 1 39 | else: 40 | count_cons = 0 41 | return start - count + 1 42 | 43 | 44 | if __name__ == '__main__': 45 | # 4 consecutive numbers with 4 distinct primes. 46 | print distinctPrimes(4, 4) 47 | -------------------------------------------------------------------------------- /#49.py: -------------------------------------------------------------------------------- 1 | from itertools import count, islice 2 | from math import sqrt 3 | 4 | 5 | def isPrime(n): 6 | if n < 2 or n % 2 == 0: 7 | return False 8 | for i in islice(count(3, 2), int(sqrt(n)-1)//2): 9 | if n % i == 0: 10 | return False 11 | return True 12 | 13 | 14 | def getPrimes(): 15 | """ 16 | Returns : list 17 | A list of all 4-digit prime numbers. 18 | """ 19 | list_primes = [] 20 | for num in range(1000, 10000): 21 | if isPrime(num): 22 | list_primes.append(num) 23 | return list_primes 24 | 25 | 26 | def Prime_permutations(): 27 | """ 28 | Returns : str 29 | A string of 3 numbers which are prime as well as permutations of 30 | each other. 31 | """ 32 | all_primes = getPrimes() 33 | first, second, third = 0, 0, 0 34 | for index in range(len(all_primes)): 35 | first = all_primes[index] 36 | if first != 1487: 37 | second, third = first + 3330, first + 6660 38 | if sorted(set(str(first))) == sorted(set(str(second))) and \ 39 | sorted(set(str(second))) == sorted(set(str(third))): 40 | if second in all_primes and third in all_primes: 41 | return str(first) + str(second) + str(third) 42 | 43 | 44 | print(Prime_permutations()) 45 | -------------------------------------------------------------------------------- /#19.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from math import floor 3 | 4 | def day_of_week(year, month, day): 5 | d = day 6 | m = (month - 3) % 12 + 1 7 | if m > 10: Y = year - 1 8 | else: Y = year 9 | y = Y % 100 10 | c = (Y - (Y % 100)) / 100 11 | 12 | w = (d + floor(2.6 * m - 0.2) + y + floor(y/4) + floor(c/4) - 2*c) % 7 13 | 14 | return int(w) 15 | 16 | def months_start_range(day,year_start,year_end): 17 | total = 0 18 | for year in range(year_start, year_end + 1): 19 | for month in range(1,13): 20 | if day_of_week(year, month, 1) == day: total += 1 21 | return total 22 | 23 | inputs = sys.stdin 24 | t = int(next(inputs)) 25 | for i in range(t): 26 | if i != t-1: 27 | y1,m1,d1 = next(inputs)[:-1].split(' ') 28 | y2,m2,d2 = next(inputs)[:-1].split(' ') 29 | else: 30 | y1,m1,d1 = next(inputs)[:-1].split(' ') 31 | y2,m2,d2 = next(inputs).split(' ') 32 | y1,m1,d1 = int(y1),int(m1),int(d1) 33 | y2,m2,d2 = int(y2),int(m2),int(d2) 34 | if y2 >= y1: 35 | total = 0 36 | if m1 > 1: 37 | total = months_start_range(0,y1+1,y2-1) 38 | for i in range(m1,13): 39 | if day_of_week(y1, i, 1) == 0: total += 1 40 | else: 41 | total = months_start_range(0,y1,y2-1) 42 | for i in range(1,m2+1): 43 | if day_of_week(y2, i, 1) == 0: total += 1 44 | print total 45 | else: 46 | print 0 -------------------------------------------------------------------------------- /33/euler.py: -------------------------------------------------------------------------------- 1 | # Logic: 2 | # For each potential fraction (10..99 / 10..99 ) 3 | # If the num or denom are mod 10, skip it 4 | # if the num and denom share a common digit 5 | # divide num and denom and store the result in 'expected' 6 | # remove the common digit from num and denom (generating num' and denom') 7 | # divide num' and denom' and compare the result to 'expected' 8 | # if the results match then I have found one of the four answers 9 | # next 10 | # Multiply the four fractions 11 | # Report the denominator in lowest common terms to the website 12 | d = 1 13 | for i in range(10, 100): 14 | for j in range(i, 100): 15 | if i % 10 == 0 or j % 10 == 0: 16 | continue 17 | if i == j: 18 | continue 19 | if str(i)[0] == str(j)[0]: 20 | if i/float(j) == int(str(i)[-1])/float(int(str(j)[-1])): 21 | d *= i/float(j) 22 | elif str(i)[0] == str(j)[-1]: 23 | if i/float(j) == int(str(i)[-1])/float(int(str(j)[0])): 24 | d *= i/float(j) 25 | elif str(i)[-1] == str(j)[0]: 26 | if i/float(j) == int(str(i)[0])/float(int(str(j)[-1])): 27 | d *= i/float(j) 28 | elif str(i)[-1] == str(j)[-1]: 29 | if i/float(j) == int(str(i)[0])/float(int(str(j)[0])): 30 | d *= i/float(j) 31 | else: 32 | continue 33 | print 1/d 34 | # d = 1 35 | # for i in range(1, 10): 36 | # for j in range(1, i): 37 | # q, r = divmod(9*j*i, 10*j-i) 38 | # if not r and q <= 9: 39 | # d *= i/float(j) 40 | # print d 41 | -------------------------------------------------------------------------------- /#23.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from math import sqrt 3 | 4 | 5 | def sum_divisors(num): 6 | """ 7 | Gives a dict of prime factors of the number num, along with their powers. 8 | 9 | Then calculates the sum of all possible divisors from the prime factors. 10 | """ 11 | n = num 12 | prime_fac = {} 13 | if num % 2 == 0: 14 | prime_fac[2] = 0 15 | while num % 2 == 0: 16 | prime_fac[2] += 1 17 | num /= 2 18 | for i in range(3, int(sqrt(num))+1, 2): 19 | if num % i == 0: 20 | prime_fac[i] = 0 21 | while num % i == 0: 22 | prime_fac[i] += 1 23 | num /= i 24 | if num > 2: 25 | prime_fac[num] = 1 26 | sum_factors = 1 27 | for prime in prime_fac: 28 | sum_factors *= (pow(prime, prime_fac[prime]+1)-1)/(prime - 1) 29 | return sum_factors-n 30 | 31 | abundant = [] 32 | for i in xrange(12, 28112): 33 | if i < sum_divisors(i): 34 | abundant.append(i) 35 | inputs = sys.stdin 36 | t = int(next(inputs)) 37 | for x in xrange(t): 38 | num = int(next(inputs)) 39 | is_anundant = 1 40 | if num > 28123: 41 | print 'YES' 42 | is_anundant = 0 43 | else: 44 | # if direct multiple of an abundant! 45 | i = num 46 | flag = 0 47 | for x in abundant: 48 | if i % x == 0 and x != i: 49 | flag = 1 50 | print 'YES' 51 | is_anundant = 0 52 | break 53 | if flag == 0: 54 | # check for pair in abundant[] with sum as i 55 | left, right = 0, len(abundant)-1 56 | while left < right: 57 | if abundant[left] + abundant[right] == i: 58 | print 'YES' 59 | is_anundant = 0 60 | break 61 | elif abundant[left] + abundant[right] < i: 62 | left += 1 63 | else: 64 | right -= 1 65 | 66 | if is_anundant: 67 | print 'NO' 68 | --------------------------------------------------------------------------------