From 401ed00f99981209efb1548d1137b3878e2f0141 Mon Sep 17 00:00:00 2001 From: Bruno Date: Sat, 13 Jul 2019 22:03:40 -0300 Subject: [PATCH 01/21] Added doctest and more explanation about Dijkstra execution. --- graphs/dijkstra.py | 117 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 94 insertions(+), 23 deletions(-) diff --git a/graphs/dijkstra.py b/graphs/dijkstra.py index 4b6bc347b061..52354b5c916b 100644 --- a/graphs/dijkstra.py +++ b/graphs/dijkstra.py @@ -1,24 +1,50 @@ """pseudo-code""" """ -DIJKSTRA(graph G, start vertex s,destination vertex d): -// all nodes initially unexplored -let H = min heap data structure, initialized with 0 and s [here 0 indicates the distance from start vertex] -while H is non-empty: - remove the first node and cost of H, call it U and cost - if U is not explored - mark U as explored - if U is d: - return cost // total cost from start to destination vertex - for each edge(U, V): c=cost of edge(u,V) // for V in graph[U] - if V unexplored: - next=cost+c - add next,V to H (at the end) +DIJKSTRA(graph G, start vertex s, destination vertex d): + +//all nodes initially unexplored + +1 - let H = min heap data structure, initialized with 0 and s [here 0 indicates + the distance from start vertex s] +2 - while H is non-empty: +3 - remove the first node and cost of H, call it U and cost +4 - if U has been previously explored: +5 - go to the while loop, line 2 //Once a node is explored there is no need + to make it again +6 - mark U as explored +7 - if U is d: +8 - return cost // total cost from start to destination vertex +9 - for each edge(U, V): c=cost of edge(U,V) // for V in graph[U] +10 - if V explored: +11 - go to next V in line 9 +12 - total_cost = cost + c +13 - add (total_cost,V) to H + +You can think at cost as a distance where Dijkstra finds the shortest distance +between vertexes s and v in a graph G. The use of a min heap as H guarantees +that if a vertex has already been explored there will be no other path with +shortest distance, that happens because heapq.heappop will always return the +next vertex with the shortest distance, considering that the heap stores not +only the distance between previous vertex and current vertex but the entire +distance between each vertex that makes up the path from start vertex to target +vertex. """ + import heapq def dijkstra(graph, start, end): + """Return the cost of the shortest path between vertexes start and end. + + >>> dijkstra(G, "E", "C") + 6 + >>> dijkstra(G2, "E", "F") + 3 + >>> dijkstra(G3, "E", "F") + 3 + """ + heap = [(0, start)] # cost from start node,end node visited = set() while heap: @@ -28,20 +54,65 @@ def dijkstra(graph, start, end): visited.add(u) if u == end: return cost - for v, c in G[u]: + for v, c in graph[u]: if v in visited: continue next = cost + c heapq.heappush(heap, (next, v)) - return (-1, -1) + return -1 + + +G = { + "A": [["B", 2], ["C", 5]], + "B": [["A", 2], ["D", 3], ["E", 1], ["F", 1]], + "C": [["A", 5], ["F", 3]], + "D": [["B", 3]], + "E": [["B", 4], ["F", 3]], + "F": [["C", 3], ["E", 3]], +} + +""" +Layout of G2: + +E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F + \ /\ + \ || + ----------------- 3 -------------------- +""" +G2 = { + "B": [["C", 1]], + "C": [["D", 1]], + "D": [["F", 1]], + "E": [["B", 1], ["F", 3]], + "F": [], +} + +""" +Layout of G3: + +E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F + \ /\ + \ || + -------- 2 ---------> G ------- 1 ------ +""" +G3 = { + "B": [["C", 1]], + "C": [["D", 1]], + "D": [["F", 1]], + "E": [["B", 1], ["G", 2]], + "F": [], + "G": [["F", 1]], +} + +shortDistance = dijkstra(G, "E", "C") +print(shortDistance) # E -- 3 --> F -- 3 --> C == 6 +shortDistance = dijkstra(G2, "E", "F") +print(shortDistance) # E -- 3 --> F == 3 -G = {'A': [['B', 2], ['C', 5]], - 'B': [['A', 2], ['D', 3], ['E', 1]], - 'C': [['A', 5], ['F', 3]], - 'D': [['B', 3]], - 'E': [['B', 1], ['F', 3]], - 'F': [['C', 3], ['E', 3]]} +shortDistance = dijkstra(G3, "E", "F") +print(shortDistance) # E -- 2 --> G -- 1 --> F == 3 -shortDistance = dijkstra(G, 'E', 'C') -print(shortDistance) +if __name__ == "__main__": + import doctest + doctest.testmod() From d13eb2cbdaebcc50dd3cef0e95b719304ce8b093 Mon Sep 17 00:00:00 2001 From: Bruno Date: Sat, 13 Jul 2019 23:49:49 -0300 Subject: [PATCH 02/21] tests were not passing with python2 due to missing __init__.py file at number_theory folder --- data_structures/hashing/number_theory/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 data_structures/hashing/number_theory/__init__.py diff --git a/data_structures/hashing/number_theory/__init__.py b/data_structures/hashing/number_theory/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 From 0e4e788e81c3a6ce41024fead246ba0d09c5eb1d Mon Sep 17 00:00:00 2001 From: Bruno Date: Sat, 13 Jul 2019 23:54:02 -0300 Subject: [PATCH 03/21] Removed the dot at the beginning of the imported modules names because 'python3 -m doctest -v data_structures/hashing/*.py' and 'python3 -m doctest -v data_structures/stacks/*.py' were failing not finding hash_table.py and stack.py modules. --- data_structures/hashing/double_hash.py | 2 +- data_structures/hashing/hash_table_with_linked_list.py | 2 +- data_structures/hashing/quadratic_probing.py | 2 +- data_structures/stacks/infix_to_postfix_conversion.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/hashing/double_hash.py b/data_structures/hashing/double_hash.py index 60098cda0ce1..7a0ce0b3a67b 100644 --- a/data_structures/hashing/double_hash.py +++ b/data_structures/hashing/double_hash.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from .hash_table import HashTable +from hash_table import HashTable from number_theory.prime_numbers import next_prime, check_prime diff --git a/data_structures/hashing/hash_table_with_linked_list.py b/data_structures/hashing/hash_table_with_linked_list.py index 9689e4fc9fcf..a45876df49bd 100644 --- a/data_structures/hashing/hash_table_with_linked_list.py +++ b/data_structures/hashing/hash_table_with_linked_list.py @@ -1,4 +1,4 @@ -from .hash_table import HashTable +from hash_table import HashTable from collections import deque diff --git a/data_structures/hashing/quadratic_probing.py b/data_structures/hashing/quadratic_probing.py index f7a9ac1ae347..1e61100a81fa 100644 --- a/data_structures/hashing/quadratic_probing.py +++ b/data_structures/hashing/quadratic_probing.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from .hash_table import HashTable +from hash_table import HashTable class QuadraticProbing(HashTable): diff --git a/data_structures/stacks/infix_to_postfix_conversion.py b/data_structures/stacks/infix_to_postfix_conversion.py index 75211fed258d..e71dccf1f45c 100644 --- a/data_structures/stacks/infix_to_postfix_conversion.py +++ b/data_structures/stacks/infix_to_postfix_conversion.py @@ -2,7 +2,7 @@ from __future__ import absolute_import import string -from .Stack import Stack +from stack import Stack __author__ = 'Omkar Pathak' From 8c4c820be0bd6d4846d56812d7036324d342a260 Mon Sep 17 00:00:00 2001 From: Bruno Date: Sun, 14 Jul 2019 20:11:59 -0300 Subject: [PATCH 04/21] Moved global code to main scope and added doctest for project euler problems 1 to 14. --- project_euler/problem_01/sol1.py | 20 +++++- project_euler/problem_01/sol2.py | 34 ++++++--- project_euler/problem_01/sol3.py | 89 +++++++++++++----------- project_euler/problem_01/sol4.py | 40 ++++++++--- project_euler/problem_01/sol5.py | 22 ++++-- project_euler/problem_01/sol6.py | 46 ++++++++++--- project_euler/problem_02/sol1.py | 35 +++++++--- project_euler/problem_02/sol2.py | 38 ++++++++-- project_euler/problem_02/sol3.py | 49 +++++++++---- project_euler/problem_02/sol4.py | 42 +++++++++-- project_euler/problem_03/sol1.py | 53 +++++++++----- project_euler/problem_03/sol2.py | 42 +++++++---- project_euler/problem_04/sol1.py | 45 +++++++----- project_euler/problem_04/sol2.py | 33 ++++++--- project_euler/problem_05/sol1.py | 45 ++++++++---- project_euler/problem_05/sol2.py | 31 +++++++-- project_euler/problem_06/sol1.py | 35 +++++++--- project_euler/problem_06/sol2.py | 30 ++++++-- project_euler/problem_06/sol3.py | 30 ++++++-- project_euler/problem_07/sol1.py | 50 ++++++++++---- project_euler/problem_07/sol2.py | 61 ++++++++++++---- project_euler/problem_07/sol3.py | 33 +++++++-- project_euler/problem_08/sol1.py | 68 +++++++++++++++--- project_euler/problem_08/sol2.py | 65 +++++++++++++++-- project_euler/problem_09/sol1.py | 44 ++++++++---- project_euler/problem_09/sol2.py | 55 ++++++++++----- project_euler/problem_09/sol3.py | 31 +++++++-- project_euler/problem_10/sol1.py | 43 ++++++++---- project_euler/problem_10/sol2.py | 36 ++++++++-- project_euler/problem_11/sol1.py | 83 +++++++++++++--------- project_euler/problem_11/sol2.py | 115 ++++++++++++++++++++----------- project_euler/problem_12/sol1.py | 30 +++++--- project_euler/problem_12/sol2.py | 35 +++++++++- project_euler/problem_13/sol1.py | 30 ++++++-- project_euler/problem_13/sol2.py | 5 -- project_euler/problem_14/sol1.py | 78 +++++++++++++++------ project_euler/problem_14/sol2.py | 71 +++++++++++++++---- 37 files changed, 1257 insertions(+), 435 deletions(-) delete mode 100644 project_euler/problem_13/sol2.py diff --git a/project_euler/problem_01/sol1.py b/project_euler/problem_01/sol1.py index c9a8c0f1ebeb..22ee57c989a9 100644 --- a/project_euler/problem_01/sol1.py +++ b/project_euler/problem_01/sol1.py @@ -9,5 +9,21 @@ raw_input # Python 2 except NameError: raw_input = input # Python 3 -n = int(raw_input().strip()) -print(sum([e for e in range(3, n) if e % 3 == 0 or e % 5 == 0])) + +def solution(n): + """Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + return sum([e for e in range(3, n) if e % 3 == 0 or e % 5 == 0]) + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_01/sol2.py b/project_euler/problem_01/sol2.py index 2b7760e0bfff..4c45318b1fb2 100644 --- a/project_euler/problem_01/sol2.py +++ b/project_euler/problem_01/sol2.py @@ -9,12 +9,28 @@ raw_input # Python 2 except NameError: raw_input = input # Python 3 -n = int(raw_input().strip()) -sum = 0 -terms = (n-1)//3 -sum+= ((terms)*(6+(terms-1)*3))//2 #sum of an A.P. -terms = (n-1)//5 -sum+= ((terms)*(10+(terms-1)*5))//2 -terms = (n-1)//15 -sum-= ((terms)*(30+(terms-1)*15))//2 -print(sum) + +def solution(n): + """Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + sum = 0 + terms = (n-1)//3 + sum+= ((terms)*(6+(terms-1)*3))//2 #sum of an A.P. + terms = (n-1)//5 + sum+= ((terms)*(10+(terms-1)*5))//2 + terms = (n-1)//15 + sum-= ((terms)*(30+(terms-1)*15))//2 + return sum + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_01/sol3.py b/project_euler/problem_01/sol3.py index f4f3aefcc5de..bbeac8b84e22 100644 --- a/project_euler/problem_01/sol3.py +++ b/project_euler/problem_01/sol3.py @@ -1,50 +1,61 @@ -from __future__ import print_function - ''' Problem Statement: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. ''' -''' -This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3. -''' - +from __future__ import print_function try: raw_input # Python 2 except NameError: raw_input = input # Python 3 -n = int(raw_input().strip()) -sum=0 -num=0 -while(1): - num+=3 - if(num>=n): - break - sum+=num - num+=2 - if(num>=n): - break - sum+=num - num+=1 - if(num>=n): - break - sum+=num - num+=3 - if(num>=n): - break - sum+=num - num+=1 - if(num>=n): - break - sum+=num - num+=2 - if(num>=n): - break - sum+=num - num+=3 - if(num>=n): - break - sum+=num -print(sum); +"""This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3.""" +def solution(n): + """Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + sum=0 + num=0 + while(1): + num+=3 + if(num>=n): + break + sum+=num + num+=2 + if(num>=n): + break + sum+=num + num+=1 + if(num>=n): + break + sum+=num + num+=3 + if(num>=n): + break + sum+=num + num+=1 + if(num>=n): + break + sum+=num + num+=2 + if(num>=n): + break + sum+=num + num+=3 + if(num>=n): + break + sum+=num + return sum + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_01/sol4.py b/project_euler/problem_01/sol4.py index 7941f5fcd3fe..f295b8d37b2b 100644 --- a/project_euler/problem_01/sol4.py +++ b/project_euler/problem_01/sol4.py @@ -1,4 +1,28 @@ -def mulitples(limit): +''' +Problem Statement: +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3,5,6 and 9. The sum of these multiples is 23. +Find the sum of all the multiples of 3 or 5 below N. +''' +from __future__ import print_function +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +def solution(n): + """Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + xmulti = [] zmulti = [] z = 3 @@ -6,7 +30,7 @@ def mulitples(limit): temp = 1 while True: result = z * temp - if (result < limit): + if (result < n): zmulti.append(result) temp += 1 else: @@ -14,17 +38,13 @@ def mulitples(limit): break while True: result = x * temp - if (result < limit): + if (result < n): xmulti.append(result) temp += 1 else: break collection = list(set(xmulti+zmulti)) return (sum(collection)) - - - - - - -print (mulitples(1000)) + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_01/sol5.py b/project_euler/problem_01/sol5.py index e261cc8fc729..c38e133afaa8 100644 --- a/project_euler/problem_01/sol5.py +++ b/project_euler/problem_01/sol5.py @@ -6,11 +6,25 @@ ''' from __future__ import print_function try: - input = raw_input #python3 + raw_input # Python 2 except NameError: - pass #python 2 + raw_input = input # Python 3 """A straightforward pythonic solution using list comprehension""" -n = int(input().strip()) -print(sum([i for i in range(n) if i%3==0 or i%5==0])) +def solution(n): + """Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + return sum([i for i in range(n) if i%3==0 or i%5==0]) + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_01/sol6.py b/project_euler/problem_01/sol6.py index 54c3073f3897..27145bfb40ec 100644 --- a/project_euler/problem_01/sol6.py +++ b/project_euler/problem_01/sol6.py @@ -1,9 +1,37 @@ -a = 3 -result = 0 -while a < 1000: - if(a % 3 == 0 or a % 5 == 0): - result += a - elif(a % 15 == 0): - result -= a - a += 1 -print(result) +''' +Problem Statement: +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3,5,6 and 9. The sum of these multiples is 23. +Find the sum of all the multiples of 3 or 5 below N. +''' +from __future__ import print_function +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +def solution(n): + """Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + a = 3 + result = 0 + while a < n: + if(a % 3 == 0 or a % 5 == 0): + result += a + elif(a % 15 == 0): + result -= a + a += 1 + return result + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_02/sol1.py b/project_euler/problem_02/sol1.py index 44ea980f2df0..97a68127f91c 100644 --- a/project_euler/problem_02/sol1.py +++ b/project_euler/problem_02/sol1.py @@ -13,12 +13,29 @@ except NameError: raw_input = input # Python 3 -n = int(raw_input().strip()) -i=1 -j=2 -sum=0 -while(j<=n): - if j%2 == 0: - sum+=j - i , j = j, i+j -print(sum) +def solution(n): + """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. + + >>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + """ + i=1 + j=2 + sum=0 + while(j<=n): + if j%2 == 0: + sum+=j + i , j = j, i+j + + return sum + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_02/sol2.py b/project_euler/problem_02/sol2.py index a2772697bb79..751e1d1e8cd0 100644 --- a/project_euler/problem_02/sol2.py +++ b/project_euler/problem_02/sol2.py @@ -1,15 +1,39 @@ -def fib(n): - """ - Returns a list of all the even terms in the Fibonacci sequence that are less than n. +''' +Problem: +Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, +the first 10 terms will be: + 1,2,3,5,8,13,21,34,55,89,.. +By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. +e.g. for n=10, we have {2,8}, sum is 10. +''' +from __future__ import print_function + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +def solution(n): + """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. + + >>> solution(10) + [2, 8] + >>> solution(15) + [2, 8] + >>> solution(2) + [2] + >>> solution(1) + [] + >>> solution(34) + [2, 8, 34] """ ls = [] a, b = 0, 1 - while b < n: + while b <= n: if b % 2 == 0: ls.append(b) a, b = b, a+b return ls -if __name__ == '__main__': - n = int(input("Enter max number: ").strip()) - print(sum(fib(n))) +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_02/sol3.py b/project_euler/problem_02/sol3.py index 0eb46d879704..4502fa4950f8 100644 --- a/project_euler/problem_02/sol3.py +++ b/project_euler/problem_02/sol3.py @@ -1,18 +1,41 @@ ''' Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two terms. - 0,1,1,2,3,5,8,13,21,34,55,89,.. -Every third term from 0 is even So using this I have written a simple code +Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, +the first 10 terms will be: + 1,2,3,5,8,13,21,34,55,89,.. By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is 10. ''' -"""Python 3""" -n = int(input()) -a=0 -b=2 -count=0 -while 4*b+a>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + """ + if n <= 1: + return 0 + a=0 + b=2 + count=0 + while 4*b+a<=n: + a, b = b, 4*b+a + count+= a + return count + b + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_02/sol4.py b/project_euler/problem_02/sol4.py index 64bae65f49b4..b00068ca73bf 100644 --- a/project_euler/problem_02/sol4.py +++ b/project_euler/problem_02/sol4.py @@ -1,13 +1,41 @@ +''' +Problem: +Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, +the first 10 terms will be: + 1,2,3,5,8,13,21,34,55,89,.. +By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. +e.g. for n=10, we have {2,8}, sum is 10. +''' +from __future__ import print_function import math from decimal import * -getcontext().prec = 100 -phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2) +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 -n = Decimal(int(input()) - 1) +def solution(n): + """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. + + >>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + """ + getcontext().prec = 100 + phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2) -index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2 -num = round(phi ** Decimal(index + 1)) / (phi + 2) -sum = num // 2 + index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2 + num = Decimal(round(phi ** Decimal(index + 1))) / (phi + 2) + sum = num // 2 + return int(sum) -print(int(sum)) +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_03/sol1.py b/project_euler/problem_03/sol1.py index bb9f8ca9ad12..b0a9ab03040c 100644 --- a/project_euler/problem_03/sol1.py +++ b/project_euler/problem_03/sol1.py @@ -4,9 +4,13 @@ e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. ''' from __future__ import print_function, division - import math +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + def isprime(no): if(no==2): return True @@ -18,22 +22,35 @@ def isprime(no): return False return True -maxNumber = 0 -n=int(input()) -if(isprime(n)): - print(n) -else: - while (n%2==0): - n=n/2 +def solution(n): + """Returns the largest prime factor of a given number n. + + >>> solution(13195) + 29 + >>> solution(10) + 5 + >>> solution(17) + 17 + """ + maxNumber = 0 if(isprime(n)): - print(n) + return n else: - n1 = int(math.sqrt(n))+1 - for i in range(3,n1,2): - if(n%i==0): - if(isprime(n/i)): - maxNumber = n/i - break - elif(isprime(i)): - maxNumber = i - print(maxNumber) + while (n%2==0): + n=n/2 + if(isprime(n)): + return int(n) + else: + n1 = int(math.sqrt(n))+1 + for i in range(3,n1,2): + if(n%i==0): + if(isprime(n/i)): + maxNumber = n/i + break + elif(isprime(i)): + maxNumber = i + return maxNumber + return int(sum) + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_03/sol2.py b/project_euler/problem_03/sol2.py index 44f9c63dfb6a..f9472b2fdf24 100644 --- a/project_euler/problem_03/sol2.py +++ b/project_euler/problem_03/sol2.py @@ -3,16 +3,34 @@ The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. ''' +from __future__ import print_function, division +import math -from __future__ import print_function -n=int(input()) -prime=1 -i=2 -while(i*i<=n): - while(n%i==0): - prime=i - n//=i - i+=1 -if(n>1): - prime=n -print(prime) +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +def solution(n): + """Returns the largest prime factor of a given number n. + + >>> solution(13195) + 29 + >>> solution(10) + 5 + >>> solution(17) + 17 + """ + prime=1 + i=2 + while(i*i<=n): + while(n%i==0): + prime=i + n//=i + i+=1 + if(n>1): + prime=n + return int(prime) + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_04/sol1.py b/project_euler/problem_04/sol1.py index 05fdd9ebab55..d4dbf10539fe 100644 --- a/project_euler/problem_04/sol1.py +++ b/project_euler/problem_04/sol1.py @@ -4,26 +4,39 @@ Find the largest palindrome made from the product of two 3-digit numbers which is less than N. ''' from __future__ import print_function -limit = int(input("limit? ")) -# fetchs the next number -for number in range(limit-1,10000,-1): +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 - # converts number into string. - strNumber = str(number) +def solution(n): + """Returns the largest palindrome made from the product of two 3-digit numbers which is less than n. + + >>> solution(20000) + 19591 + >>> solution(30000) + 29992 + >>> solution(40000) + 39893 + """ + # fetchs the next number + for number in range(n-1,10000,-1): - # checks whether 'strNumber' is a palindrome. - if(strNumber == strNumber[::-1]): + # converts number into string. + strNumber = str(number) - divisor = 999 + # checks whether 'strNumber' is a palindrome. + if(strNumber == strNumber[::-1]): - # if 'number' is a product of two 3-digit numbers - # then number is the answer otherwise fetch next number. - while(divisor != 99): - - if((number % divisor == 0) and (len(str(number / divisor)) == 3)): + divisor = 999 - print(number) - exit(0) + # if 'number' is a product of two 3-digit numbers + # then number is the answer otherwise fetch next number. + while(divisor != 99): + if((number % divisor == 0) and (len(str(int(number / divisor))) == 3)): + return number + divisor -=1 - divisor -=1 +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_04/sol2.py b/project_euler/problem_04/sol2.py index 70810c38986f..10e46025a782 100644 --- a/project_euler/problem_04/sol2.py +++ b/project_euler/problem_04/sol2.py @@ -4,14 +4,29 @@ Find the largest palindrome made from the product of two 3-digit numbers which is less than N. ''' from __future__ import print_function -n = int(input().strip()) -answer = 0 -for i in range(999,99,-1): #3 digit nimbers range from 999 down to 100 - for j in range(999,99,-1): - t = str(i*j) - if t == t[::-1] and i*j < n: - answer = max(answer,i*j) -print(answer) -exit(0) +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 +def solution(n): + """Returns the largest palindrome made from the product of two 3-digit numbers which is less than n. + + >>> solution(20000) + 19591 + >>> solution(30000) + 29992 + >>> solution(40000) + 39893 + """ + answer = 0 + for i in range(999,99,-1): #3 digit nimbers range from 999 down to 100 + for j in range(999,99,-1): + t = str(i*j) + if t == t[::-1] and i*j < n: + answer = max(answer, i * j) + return answer + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_05/sol1.py b/project_euler/problem_05/sol1.py index 7896d75e3456..38b98816e9f2 100644 --- a/project_euler/problem_05/sol1.py +++ b/project_euler/problem_05/sol1.py @@ -5,17 +5,36 @@ ''' from __future__ import print_function -n = int(input()) -i = 0 -while 1: - i+=n*(n-1) - nfound=0 - for j in range(2,n): - if (i%j != 0): - nfound=1 +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +def solution(n): + """Returns the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to n. + + >>> solution(10) + 2520 + >>> solution(15) + 360360 + >>> solution(20) + 232792560 + >>> solution(22) + 232792560 + """ + i = 0 + while 1: + i+=n*(n-1) + nfound=0 + for j in range(2,n): + if (i%j != 0): + nfound=1 + break + if(nfound==0): + if(i==0): + i=1 + return i break - if(nfound==0): - if(i==0): - i=1 - print(i) - break + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_05/sol2.py b/project_euler/problem_05/sol2.py index cd11437f30db..4964e65c02e2 100644 --- a/project_euler/problem_05/sol2.py +++ b/project_euler/problem_05/sol2.py @@ -1,9 +1,14 @@ -#!/bin/python3 ''' Problem: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N? ''' +from __future__ import print_function + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 """ Euclidean GCD Algorithm """ def gcd(x,y): @@ -13,8 +18,22 @@ def gcd(x,y): def lcm(x,y): return (x*y)//gcd(x,y) -n = int(input()) -g=1 -for i in range(1,n+1): - g=lcm(g,i) -print(g) +def solution(n): + """Returns the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to n. + + >>> solution(10) + 2520 + >>> solution(15) + 360360 + >>> solution(20) + 232792560 + >>> solution(22) + 232792560 + """ + g=1 + for i in range(1,n+1): + g=lcm(g,i) + return g + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_06/sol1.py b/project_euler/problem_06/sol1.py index 852d4e2f9fc4..f9433d20fa04 100644 --- a/project_euler/problem_06/sol1.py +++ b/project_euler/problem_06/sol1.py @@ -10,11 +10,30 @@ ''' from __future__ import print_function -suma = 0 -sumb = 0 -n = int(input()) -for i in range(1,n+1): - suma += i**2 - sumb += i -sum = sumb**2 - suma -print(sum) +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +def solution(n): + """Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. + + >>> solution(10) + 2640 + >>> solution(15) + 13160 + >>> solution(20) + 41230 + >>> solution(50) + 1582700 + """ + suma = 0 + sumb = 0 + for i in range(1,n+1): + suma += i**2 + sumb += i + sum = sumb**2 - suma + return sum + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_06/sol2.py b/project_euler/problem_06/sol2.py index aa8aea58fd7b..e261a51f38ad 100644 --- a/project_euler/problem_06/sol2.py +++ b/project_euler/problem_06/sol2.py @@ -9,8 +9,28 @@ Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. ''' from __future__ import print_function -n = int(input()) -suma = n*(n+1)/2 -suma **= 2 -sumb = n*(n+1)*(2*n+1)/6 -print(suma-sumb) + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +def solution(n): + """Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. + + >>> solution(10) + 2640 + >>> solution(15) + 13160 + >>> solution(20) + 41230 + >>> solution(50) + 1582700 + """ + suma = n*(n+1)/2 + suma **= 2 + sumb = n*(n+1)*(2*n+1)/6 + return int(suma-sumb) + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_06/sol3.py b/project_euler/problem_06/sol3.py index b2d9f444d9a9..5c9eca3130d8 100644 --- a/project_euler/problem_06/sol3.py +++ b/project_euler/problem_06/sol3.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- ''' Problem: The sum of the squares of the first ten natural numbers is, @@ -9,12 +10,27 @@ ''' from __future__ import print_function import math -def problem6(number=100): - sum_of_squares = sum([i*i for i in range(1,number+1)]) - square_of_sum = int(math.pow(sum(range(1,number+1)),2)) + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +def solution(n): + """Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. + + >>> solution(10) + 2640 + >>> solution(15) + 13160 + >>> solution(20) + 41230 + >>> solution(50) + 1582700 + """ + sum_of_squares = sum([i*i for i in range(1,n+1)]) + square_of_sum = int(math.pow(sum(range(1,n+1)),2)) return square_of_sum - sum_of_squares -def main(): - print(problem6()) -if __name__ == '__main__': - main() \ No newline at end of file +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_07/sol1.py b/project_euler/problem_07/sol1.py index ea31d0b2bb2c..041807fb2f9f 100644 --- a/project_euler/problem_07/sol1.py +++ b/project_euler/problem_07/sol1.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- ''' By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. @@ -5,6 +6,12 @@ ''' from __future__ import print_function from math import sqrt + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + def isprime(n): if (n==2): return True @@ -16,15 +23,34 @@ def isprime(n): if(n%i==0): return False return True -n = int(input()) -i=0 -j=1 -while(i!=n and j<3): - j+=1 - if (isprime(j)): - i+=1 -while(i!=n): - j+=2 - if(isprime(j)): - i+=1 -print(j) + +def solution(n): + """Returns the n-th prime number. + + >>> solution(6) + 13 + >>> solution(1) + 2 + >>> solution(3) + 5 + >>> solution(20) + 71 + >>> solution(50) + 229 + >>> solution(100) + 541 + """ + i=0 + j=1 + while(i!=n and j<3): + j+=1 + if (isprime(j)): + i+=1 + while(i!=n): + j+=2 + if(isprime(j)): + i+=1 + return j + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_07/sol2.py b/project_euler/problem_07/sol2.py index fdf39cbc4d26..c0dbae209878 100644 --- a/project_euler/problem_07/sol2.py +++ b/project_euler/problem_07/sol2.py @@ -1,16 +1,49 @@ +# -*- coding: utf-8 -*- +''' +By listing the first six prime numbers: +2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. +What is the Nth prime number? +''' +from __future__ import print_function +from math import sqrt + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + # By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the Nth prime number? def isprime(number): - for i in range(2,int(number**0.5)+1): - if number%i==0: - return False - return True -n = int(input('Enter The N\'th Prime Number You Want To Get: ')) # Ask For The N'th Prime Number Wanted -primes = [] -num = 2 -while len(primes) < n: - if isprime(num): - primes.append(num) - num += 1 - else: - num += 1 -print(primes[len(primes) - 1]) + for i in range(2,int(number**0.5)+1): + if number%i==0: + return False + return True + +def solution(n): + """Returns the n-th prime number. + + >>> solution(6) + 13 + >>> solution(1) + 2 + >>> solution(3) + 5 + >>> solution(20) + 71 + >>> solution(50) + 229 + >>> solution(100) + 541 + """ + primes = [] + num = 2 + while len(primes) < n: + if isprime(num): + primes.append(num) + num += 1 + else: + num += 1 + return primes[len(primes) - 1] + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_07/sol3.py b/project_euler/problem_07/sol3.py index 0001e4318cc9..0f23d6c65187 100644 --- a/project_euler/problem_07/sol3.py +++ b/project_euler/problem_07/sol3.py @@ -1,12 +1,18 @@ +# -*- coding: utf-8 -*- ''' By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the Nth prime number? ''' from __future__ import print_function -# from Python.Math import PrimeCheck import math import itertools + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + def primeCheck(number): if number % 2 == 0 and number > 2: return False @@ -19,10 +25,23 @@ def prime_generator(): yield num num+=1 -def main(): - n = int(input('Enter The N\'th Prime Number You Want To Get: ')) # Ask For The N'th Prime Number Wanted - print(next(itertools.islice(prime_generator(),n-1,n))) - +def solution(n): + """Returns the n-th prime number. + + >>> solution(6) + 13 + >>> solution(1) + 2 + >>> solution(3) + 5 + >>> solution(20) + 71 + >>> solution(50) + 229 + >>> solution(100) + 541 + """ + return next(itertools.islice(prime_generator(),n-1,n)) -if __name__ == '__main__': - main() \ No newline at end of file +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_08/sol1.py b/project_euler/problem_08/sol1.py index 817fd3f87507..65f44a0854e1 100644 --- a/project_euler/problem_08/sol1.py +++ b/project_euler/problem_08/sol1.py @@ -1,15 +1,67 @@ +# -*- coding: utf-8 -*- +''' +The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. + +73167176531330624919225119674426574742355349194934 +96983520312774506326239578318016984801869478851843 +85861560789112949495459501737958331952853208805511 +12540698747158523863050715693290963295227443043557 +66896648950445244523161731856403098711121722383113 +62229893423380308135336276614282806444486645238749 +30358907296290491560440772390713810515859307960866 +70172427121883998797908792274921901699720888093776 +65727333001053367881220235421809751254540594752243 +52584907711670556013604839586446706324415722155397 +53697817977846174064955149290862569321978468622482 +83972241375657056057490261407972968652414535100474 +82166370484403199890008895243450658541227588666881 +16427171479924442928230863465674813919123162824586 +17866458359124566529476545682848912883142607690042 +24219022671055626321111109370544217506941658960408 +07198403850962455444362981230987879927244284909188 +84580156166097919133875499200524063689912560717606 +05886116467109405077541002256983155200055935729725 +71636269561882670428252483600823257530420752963450 + +Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? +''' import sys -def main(): + +N = """73167176531330624919225119674426574742355349194934\ +96983520312774506326239578318016984801869478851843\ +85861560789112949495459501737958331952853208805511\ +12540698747158523863050715693290963295227443043557\ +66896648950445244523161731856403098711121722383113\ +62229893423380308135336276614282806444486645238749\ +30358907296290491560440772390713810515859307960866\ +70172427121883998797908792274921901699720888093776\ +65727333001053367881220235421809751254540594752243\ +52584907711670556013604839586446706324415722155397\ +53697817977846174064955149290862569321978468622482\ +83972241375657056057490261407972968652414535100474\ +82166370484403199890008895243450658541227588666881\ +16427171479924442928230863465674813919123162824586\ +17866458359124566529476545682848912883142607690042\ +24219022671055626321111109370544217506941658960408\ +07198403850962455444362981230987879927244284909188\ +84580156166097919133875499200524063689912560717606\ +05886116467109405077541002256983155200055935729725\ +71636269561882670428252483600823257530420752963450""" + +def solution(n): + """Find the thirteen adjacent digits in the 1000-digit number n that have the greatest product and returns it. + + >>> solution(N) + 23514624000 + """ LargestProduct = -sys.maxsize-1 - number=input().strip() - for i in range(len(number)-12): + for i in range(len(n)-12): product=1 for j in range(13): - product *= int(number[i+j]) + product *= int(n[i+j]) if product > LargestProduct: LargestProduct = product - print(LargestProduct) - + return LargestProduct -if __name__ == '__main__': - main() +if __name__ == "__main__": + print(solution(N)) diff --git a/project_euler/problem_08/sol2.py b/project_euler/problem_08/sol2.py index ae03f3ad0aa6..9c25f16d87bb 100644 --- a/project_euler/problem_08/sol2.py +++ b/project_euler/problem_08/sol2.py @@ -1,8 +1,61 @@ +# -*- coding: utf-8 -*- +''' +The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. + +73167176531330624919225119674426574742355349194934 +96983520312774506326239578318016984801869478851843 +85861560789112949495459501737958331952853208805511 +12540698747158523863050715693290963295227443043557 +66896648950445244523161731856403098711121722383113 +62229893423380308135336276614282806444486645238749 +30358907296290491560440772390713810515859307960866 +70172427121883998797908792274921901699720888093776 +65727333001053367881220235421809751254540594752243 +52584907711670556013604839586446706324415722155397 +53697817977846174064955149290862569321978468622482 +83972241375657056057490261407972968652414535100474 +82166370484403199890008895243450658541227588666881 +16427171479924442928230863465674813919123162824586 +17866458359124566529476545682848912883142607690042 +24219022671055626321111109370544217506941658960408 +07198403850962455444362981230987879927244284909188 +84580156166097919133875499200524063689912560717606 +05886116467109405077541002256983155200055935729725 +71636269561882670428252483600823257530420752963450 + +Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? +''' + from functools import reduce -def main(): - number=input().strip() - print(max([reduce(lambda x,y: int(x)*int(y),number[i:i+13]) for i in range(len(number)-12)])) - -if __name__ == '__main__': - main() +N = """73167176531330624919225119674426574742355349194934\ +96983520312774506326239578318016984801869478851843\ +85861560789112949495459501737958331952853208805511\ +12540698747158523863050715693290963295227443043557\ +66896648950445244523161731856403098711121722383113\ +62229893423380308135336276614282806444486645238749\ +30358907296290491560440772390713810515859307960866\ +70172427121883998797908792274921901699720888093776\ +65727333001053367881220235421809751254540594752243\ +52584907711670556013604839586446706324415722155397\ +53697817977846174064955149290862569321978468622482\ +83972241375657056057490261407972968652414535100474\ +82166370484403199890008895243450658541227588666881\ +16427171479924442928230863465674813919123162824586\ +17866458359124566529476545682848912883142607690042\ +24219022671055626321111109370544217506941658960408\ +07198403850962455444362981230987879927244284909188\ +84580156166097919133875499200524063689912560717606\ +05886116467109405077541002256983155200055935729725\ +71636269561882670428252483600823257530420752963450""" + +def solution(n): + """Find the thirteen adjacent digits in the 1000-digit number n that have the greatest product and returns it. + + >>> solution(N) + 23514624000 + """ + return max([reduce(lambda x,y: int(x)*int(y),n[i:i+13]) for i in range(len(n)-12)]) + +if __name__ == "__main__": + print(solution(N)) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index e54c543b4721..7dfe2f9f1ea2 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -1,15 +1,33 @@ +''' +Problem Statement: +A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + a^2 + b^2 = c^2 +For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. + +There exists exactly one Pythagorean triplet for which a + b + c = 1000. +Find the product abc. +''' from __future__ import print_function -# Program to find the product of a,b,c which are Pythagorean Triplet that satisfice the following: -# 1. a < b < c -# 2. a**2 + b**2 = c**2 -# 3. a + b + c = 1000 -print("Please Wait...") -for a in range(300): - for b in range(400): - for c in range(500): - if(a < b < c): - if((a**2) + (b**2) == (c**2)): - if((a+b+c) == 1000): - print(("Product of",a,"*",b,"*",c,"=",(a*b*c))) - break +def solution(): + """ + Returns the product of a,b,c which are Pythagorean Triplet that satisfies the following: + 1. a < b < c + 2. a**2 + b**2 = c**2 + 3. a + b + c = 1000 + + >>> solution() + 31875000 + """ + for a in range(300): + for b in range(400): + for c in range(500): + if(a < b < c): + if((a**2) + (b**2) == (c**2)): + if((a+b+c) == 1000): + return a * b * c + break + +if __name__ == "__main__": + print("Please Wait...") + print(solution()) diff --git a/project_euler/problem_09/sol2.py b/project_euler/problem_09/sol2.py index 933f5c557d71..12e4f9d7811d 100644 --- a/project_euler/problem_09/sol2.py +++ b/project_euler/problem_09/sol2.py @@ -1,18 +1,39 @@ -"""A Pythagorean triplet is a set of three natural numbers, for which, -a^2+b^2=c^2 -Given N, Check if there exists any Pythagorean triplet for which a+b+c=N -Find maximum possible value of product of a,b,c among all such Pythagorean triplets, If there is no such Pythagorean triplet print -1.""" -#!/bin/python3 +''' +Problem Statement: +A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + a^2 + b^2 = c^2 +For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. -product=-1 -d=0 -N = int(input()) -for a in range(1,N//3): - """Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c """ - b=(N*N-2*a*N)//(2*N-2*a) - c=N-a-b - if c*c==(a*a+b*b): - d=(a*b*c) - if d>=product: - product=d -print(product) +There exists exactly one Pythagorean triplet for which a + b + c = 1000. +Find the product abc. +''' +from __future__ import print_function +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +def solution(n): + """ + Return the product of a,b,c which are Pythagorean Triplet that satisfies the following: + 1. a < b < c + 2. a**2 + b**2 = c**2 + 3. a + b + c = 1000 + + >>> solution(1000) + 31875000 + """ + product=-1 + d=0 + for a in range(1,n//3): + """Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c """ + b=(n*n-2*a*n)//(2*n-2*a) + c=n-a-b + if c*c==(a*a+b*b): + d=(a*b*c) + if d>=product: + product=d + return product + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_09/sol3.py b/project_euler/problem_09/sol3.py index 5ebf38e76e1a..42cb5ee0be31 100644 --- a/project_euler/problem_09/sol3.py +++ b/project_euler/problem_09/sol3.py @@ -1,6 +1,25 @@ -def main(): - print([a*b*c for a in range(1,999) for b in range(a,999) for c in range(b,999) - if (a*a+b*b==c*c) and (a+b+c==1000 ) ][0]) - -if __name__ == '__main__': - main() +''' +Problem Statement: +A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + a^2 + b^2 = c^2 +For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. + +There exists exactly one Pythagorean triplet for which a + b + c = 1000. +Find the product abc. +''' +from __future__ import print_function + +def solution(): + """ + Returns the product of a,b,c which are Pythagorean Triplet that satisfies the following: + 1. a**2 + b**2 = c**2 + 2. a + b + c = 1000 + + >>> solution() + 31875000 + """ + return ([a*b*c for a in range(1,999) for b in range(a,999) for c in range(b,999) + if (a*a+b*b==c*c) and (a+b+c==1000 )][0]) + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_10/sol1.py b/project_euler/problem_10/sol1.py index 94e5b7362114..f01f27803726 100644 --- a/project_euler/problem_10/sol1.py +++ b/project_euler/problem_10/sol1.py @@ -1,10 +1,20 @@ +''' +Problem Statement: +The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. + +Find the sum of all the primes below two million. +''' from __future__ import print_function from math import sqrt +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 try: - xrange #Python 2 + xrange # Python 2 except NameError: - xrange = range #Python 3 + xrange = range # Python 3 def is_prime(n): for i in xrange(2, int(sqrt(n))+1): @@ -25,14 +35,21 @@ def sum_of_primes(n): return sumOfPrimes -if __name__ == '__main__': - import sys - - if len(sys.argv) == 1: - print(sum_of_primes(2000000)) - else: - try: - n = int(sys.argv[1]) - print(sum_of_primes(n)) - except ValueError: - print('Invalid entry - please enter a number.') +def solution(n): + """Returns the sum of all the primes below n. + + >>> solution(2000000) + 142913828922 + >>> solution(1000) + 76127 + >>> solution(5000) + 1548136 + >>> solution(10000) + 5736396 + >>> solution(7) + 10 + """ + return sum_of_primes(n) + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_10/sol2.py b/project_euler/problem_10/sol2.py index 22df95c063e2..fd5eaf9c9ee6 100644 --- a/project_euler/problem_10/sol2.py +++ b/project_euler/problem_10/sol2.py @@ -1,6 +1,16 @@ -#from Python.Math import prime_generator +''' +Problem Statement: +The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. + +Find the sum of all the primes below two million. +''' +from __future__ import print_function import math from itertools import takewhile +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 def primeCheck(number): if number % 2 == 0 and number > 2: @@ -13,10 +23,22 @@ def prime_generator(): if primeCheck(num): yield num num+=1 - -def main(): - n = int(input('Enter The upper limit of prime numbers: ')) - print(sum(takewhile(lambda x: x < n,prime_generator()))) + +def solution(n): + """Returns the sum of all the primes below n. -if __name__ == '__main__': - main() + >>> solution(2000000) + 142913828922 + >>> solution(1000) + 76127 + >>> solution(5000) + 1548136 + >>> solution(10000) + 5736396 + >>> solution(7) + 10 + """ + return sum(takewhile(lambda x: x < n,prime_generator())) + +if __name__ == "__main__": + print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_11/sol1.py b/project_euler/problem_11/sol1.py index b882dc449156..60dcb080c598 100644 --- a/project_euler/problem_11/sol1.py +++ b/project_euler/problem_11/sol1.py @@ -1,5 +1,4 @@ -from __future__ import print_function -''' +""" What is the greatest product of four adjacent numbers (horizontally, vertically, or diagonally) in this 20x20 array? 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 @@ -22,47 +21,67 @@ 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 -''' +""" + +from __future__ import print_function try: - xrange #Python 2 + xrange # Python 2 except NameError: - xrange = range #Python 2 + xrange = range # Python 2 def largest_product(grid): - nColumns = len(grid[0]) - nRows = len(grid) + nColumns = len(grid[0]) + nRows = len(grid) + + largest = 0 + lrDiagProduct = 0 + rlDiagProduct = 0 - largest = 0 - lrDiagProduct = 0 - rlDiagProduct = 0 + # Check vertically, horizontally, diagonally at the same time (only works for nxn grid) + for i in xrange(nColumns): + for j in xrange(nRows - 3): + vertProduct = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i] + horzProduct = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3] - #Check vertically, horizontally, diagonally at the same time (only works for nxn grid) - for i in xrange(nColumns): - for j in xrange(nRows-3): - vertProduct = grid[j][i]*grid[j+1][i]*grid[j+2][i]*grid[j+3][i] - horzProduct = grid[i][j]*grid[i][j+1]*grid[i][j+2]*grid[i][j+3] + # Left-to-right diagonal (\) product + if i < nColumns - 3: + lrDiagProduct = ( + grid[i][j] + * grid[i + 1][j + 1] + * grid[i + 2][j + 2] + * grid[i + 3][j + 3] + ) - #Left-to-right diagonal (\) product - if (i < nColumns-3): - lrDiagProduct = grid[i][j]*grid[i+1][j+1]*grid[i+2][j+2]*grid[i+3][j+3] + # Right-to-left diagonal(/) product + if i > 2: + rlDiagProduct = ( + grid[i][j] + * grid[i - 1][j + 1] + * grid[i - 2][j + 2] + * grid[i - 3][j + 3] + ) - #Right-to-left diagonal(/) product - if (i > 2): - rlDiagProduct = grid[i][j]*grid[i-1][j+1]*grid[i-2][j+2]*grid[i-3][j+3] + maxProduct = max(vertProduct, horzProduct, lrDiagProduct, rlDiagProduct) + if maxProduct > largest: + largest = maxProduct - maxProduct = max(vertProduct, horzProduct, lrDiagProduct, rlDiagProduct) - if maxProduct > largest: - largest = maxProduct + return largest - return largest +def solution(): + """Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution() + 70600674 + """ + grid = [] + with open("grid.txt") as file: + for line in file: + grid.append(line.strip("\n").split(" ")) -if __name__ == '__main__': - grid = [] - with open('grid.txt') as file: - for line in file: - grid.append(line.strip('\n').split(' ')) + grid = [[int(i) for i in grid[j]] for j in xrange(len(grid))] - grid = [[int(i) for i in grid[j]] for j in xrange(len(grid))] + return largest_product(grid) - print(largest_product(grid)) \ No newline at end of file +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_11/sol2.py b/project_euler/problem_11/sol2.py index b03395f01697..45ecb20c110a 100644 --- a/project_euler/problem_11/sol2.py +++ b/project_euler/problem_11/sol2.py @@ -1,39 +1,76 @@ -def main(): - with open ("grid.txt", "r") as f: - l = [] - for i in range(20): - l.append([int(x) for x in f.readline().split()]) - - maximum = 0 - - # right - for i in range(20): - for j in range(17): - temp = l[i][j] * l[i][j+1] * l[i][j+2] * l[i][j+3] - if temp > maximum: - maximum = temp - - # down - for i in range(17): - for j in range(20): - temp = l[i][j] * l[i+1][j] * l[i+2][j] * l[i+3][j] - if temp > maximum: - maximum = temp - - #diagonal 1 - for i in range(17): - for j in range(17): - temp = l[i][j] * l[i+1][j+1] * l[i+2][j+2] * l[i+3][j+3] - if temp > maximum: - maximum = temp - - #diagonal 2 - for i in range(17): - for j in range(3, 20): - temp = l[i][j] * l[i+1][j-1] * l[i+2][j-2] * l[i+3][j-3] - if temp > maximum: - maximum = temp - print(maximum) - -if __name__ == '__main__': - main() \ No newline at end of file +""" +What is the greatest product of four adjacent numbers (horizontally, vertically, or diagonally) in this 20x20 array? + +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 +""" + +from __future__ import print_function + +try: + xrange # Python 2 +except NameError: + xrange = range # Python 2 + +def solution(): + """Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution() + 70600674 + """ + with open("grid.txt", "r") as f: + l = [] + for i in xrange(20): + l.append([int(x) for x in f.readline().split()]) + + maximum = 0 + + # right + for i in xrange(20): + for j in xrange(17): + temp = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3] + if temp > maximum: + maximum = temp + + # down + for i in xrange(17): + for j in xrange(20): + temp = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j] + if temp > maximum: + maximum = temp + + # diagonal 1 + for i in xrange(17): + for j in xrange(17): + temp = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] + if temp > maximum: + maximum = temp + + # diagonal 2 + for i in xrange(17): + for j in xrange(3, 20): + temp = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] + if temp > maximum: + maximum = temp + return maximum + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_12/sol1.py b/project_euler/problem_12/sol1.py index 73d48a2ec897..ee43ffdf01fd 100644 --- a/project_euler/problem_12/sol1.py +++ b/project_euler/problem_12/sol1.py @@ -1,5 +1,3 @@ -from __future__ import print_function -from math import sqrt ''' Highly divisible triangular numbers Problem 12 @@ -20,6 +18,8 @@ What is the value of the first triangle number to have over five hundred divisors? ''' +from __future__ import print_function +from math import sqrt try: xrange #Python 2 except NameError: @@ -35,14 +35,24 @@ def count_divisors(n): nDivisors -= 1 return nDivisors -tNum = 1 -i = 1 -while True: - i += 1 - tNum += i +def solution(): + """Returns the value of the first triangle number to have over five hundred divisors. + + >>> solution() + 76576500 + """ + tNum = 1 + i = 1 + + while True: + i += 1 + tNum += i + + if count_divisors(tNum) > 500: + break - if count_divisors(tNum) > 500: - break + return tNum -print(tNum) +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_12/sol2.py b/project_euler/problem_12/sol2.py index 479ab2b900cb..03e54c38e15b 100644 --- a/project_euler/problem_12/sol2.py +++ b/project_euler/problem_12/sol2.py @@ -1,8 +1,39 @@ +''' +Highly divisible triangular numbers +Problem 12 +The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: + +1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... + +Let us list the factors of the first seven triangle numbers: + + 1: 1 + 3: 1,3 + 6: 1,2,3,6 +10: 1,2,5,10 +15: 1,3,5,15 +21: 1,3,7,21 +28: 1,2,4,7,14,28 +We can see that 28 is the first triangle number to have over five divisors. + +What is the value of the first triangle number to have over five hundred divisors? +''' +from __future__ import print_function + def triangle_number_generator(): for n in range(1,1000000): yield n*(n+1)//2 - + def count_divisors(n): return sum([2 for i in range(1,int(n**0.5)+1) if n%i==0 and i*i != n]) -print(next(i for i in triangle_number_generator() if count_divisors(i) > 500)) +def solution(): + """Returns the value of the first triangle number to have over five hundred divisors. + + >>> solution() + 76576500 + """ + return next(i for i in triangle_number_generator() if count_divisors(i) > 500) + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_13/sol1.py b/project_euler/problem_13/sol1.py index faaaad5e88c1..605b89a556f8 100644 --- a/project_euler/problem_13/sol1.py +++ b/project_euler/problem_13/sol1.py @@ -1,14 +1,32 @@ ''' Problem Statement: -Work out the first ten digits of the sum of the N 50-digit numbers. +Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. ''' from __future__ import print_function -n = int(input().strip()) +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 -array = [] -for i in range(n): - array.append(int(input().strip())) +def solution(array): + """Returns the first ten digits of the sum of the array elements. + + >>> sum = 0 + >>> array = [] + >>> with open("num.txt",'r') as f: + ... for line in f: + ... array.append(int(line)) + ... + >>> solution(array) + '5537376230' + """ + return str(sum(array))[:10] -print(str(sum(array))[:10]) +if __name__ == "__main__": + n = int(input().strip()) + array = [] + for i in range(n): + array.append(int(input().strip())) + print(solution(array)) diff --git a/project_euler/problem_13/sol2.py b/project_euler/problem_13/sol2.py deleted file mode 100644 index c1416bcd6e7d..000000000000 --- a/project_euler/problem_13/sol2.py +++ /dev/null @@ -1,5 +0,0 @@ -sum = 0 -with open("num.txt",'r') as f: - for line in f: - sum += int(line) -print(str(sum)[:10]) diff --git a/project_euler/problem_14/sol1.py b/project_euler/problem_14/sol1.py index 9037f6eb8bd5..a85eaf770aaf 100644 --- a/project_euler/problem_14/sol1.py +++ b/project_euler/problem_14/sol1.py @@ -1,21 +1,59 @@ +# -*- coding: utf-8 -*- +''' +Problem Statement: +The following iterative sequence is defined for the set of positive integers: + + n → n/2 (n is even) + n → 3n + 1 (n is odd) + +Using the rule above and starting with 13, we generate the following sequence: + + 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 + +It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. + +Which starting number, under one million, produces the longest chain? +''' from __future__ import print_function -largest_number = 0 -pre_counter = 0 - -for input1 in range(750000,1000000): - counter = 1 - number = input1 - - while number > 1: - if number % 2 == 0: - number /=2 - counter += 1 - else: - number = (3*number)+1 - counter += 1 - - if counter > pre_counter: - largest_number = input1 - pre_counter = counter - -print(('Largest Number:',largest_number,'->',pre_counter,'digits')) +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +def solution(n): + """Returns the number under n that generates the longest sequence using the formula: + n → n/2 (n is even) + n → 3n + 1 (n is odd) + + >>> solution(1000000) + {'counter': 525, 'largest_number': 837799} + >>> solution(200) + {'counter': 125, 'largest_number': 171} + >>> solution(5000) + {'counter': 238, 'largest_number': 3711} + >>> solution(15000) + {'counter': 276, 'largest_number': 13255} + """ + largest_number = 0 + pre_counter = 0 + + for input1 in range(n): + counter = 1 + number = input1 + + while number > 1: + if number % 2 == 0: + number /=2 + counter += 1 + else: + number = (3*number)+1 + counter += 1 + + if counter > pre_counter: + largest_number = input1 + pre_counter = counter + return {'counter': pre_counter, 'largest_number': largest_number} + +if __name__ == "__main__": + result = solution(int(raw_input().strip())) + print(('Largest Number:', result['largest_number'], '->', result['counter'], 'digits')) diff --git a/project_euler/problem_14/sol2.py b/project_euler/problem_14/sol2.py index b9de42be1108..f0296831fbb1 100644 --- a/project_euler/problem_14/sol2.py +++ b/project_euler/problem_14/sol2.py @@ -1,16 +1,57 @@ +# -*- coding: utf-8 -*- +''' +Collatz conjecture: start with any positive integer n. Next term obtained from the previous term as follows: +If the previous term is even, the next term is one half the previous term. +If the previous term is odd, the next term is 3 times the previous term plus 1. +The conjecture states the sequence will always reach 1 regardless of starting n. + +Problem Statement: +The following iterative sequence is defined for the set of positive integers: + + n → n/2 (n is even) + n → 3n + 1 (n is odd) + +Using the rule above and starting with 13, we generate the following sequence: + + 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 + +It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. + +Which starting number, under one million, produces the longest chain? +''' +from __future__ import print_function +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + def collatz_sequence(n): - """Collatz conjecture: start with any positive integer n.Next termis obtained from the previous term as follows: - if the previous term is even, the next term is one half the previous term. - If the previous term is odd, the next term is 3 times the previous term plus 1. - The conjecture states the sequence will always reach 1 regaardess of starting n.""" - sequence = [n] - while n != 1: - if n % 2 == 0:# even - n //= 2 - else: - n = 3*n +1 - sequence.append(n) - return sequence - -answer = max([(len(collatz_sequence(i)), i) for i in range(1,1000000)]) -print("Longest Collatz sequence under one million is %d with length %d" % (answer[1],answer[0])) \ No newline at end of file + """Returns the Collatz sequence for n.""" + sequence = [n] + while n != 1: + if n % 2 == 0: + n //= 2 + else: + n = 3*n +1 + sequence.append(n) + return sequence + +def solution(n): + """Returns the number under n that generates the longest Collatz sequence. + + >>> solution(1000000) + {'counter': 525, 'largest_number': 837799} + >>> solution(200) + {'counter': 125, 'largest_number': 171} + >>> solution(5000) + {'counter': 238, 'largest_number': 3711} + >>> solution(15000) + {'counter': 276, 'largest_number': 13255} + """ + + result = max([(len(collatz_sequence(i)), i) for i in range(1, n)]) + return {'counter': result[0], 'largest_number': result[1]} + +if __name__ == "__main__": + result = solution(int(raw_input().strip())) + print("Longest Collatz sequence under one million is %d with length %d" % (result['largest_number'], result['counter'])) From 9fa20b3574ed7d989822efabd1ced069ea657172 Mon Sep 17 00:00:00 2001 From: Bruno Date: Mon, 15 Jul 2019 00:55:05 -0300 Subject: [PATCH 05/21] Added test case for negative input. --- project_euler/problem_01/sol1.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project_euler/problem_01/sol1.py b/project_euler/problem_01/sol1.py index 22ee57c989a9..0632451e7343 100644 --- a/project_euler/problem_01/sol1.py +++ b/project_euler/problem_01/sol1.py @@ -21,6 +21,8 @@ def solution(n): 23 >>> solution(600) 83700 + >>> solution(-7) + 0 """ return sum([e for e in range(3, n) if e % 3 == 0 or e % 5 == 0]) From 8b17220201f9fac95e895064e0b8feb3456e8cac Mon Sep 17 00:00:00 2001 From: Bruno Date: Mon, 15 Jul 2019 01:07:13 -0300 Subject: [PATCH 06/21] Changed N variable to do not use end of line scape because in case there is a space after it the script will break making it much more error prone. --- project_euler/problem_08/sol2.py | 42 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/project_euler/problem_08/sol2.py b/project_euler/problem_08/sol2.py index 9c25f16d87bb..964fa70a35e4 100644 --- a/project_euler/problem_08/sol2.py +++ b/project_euler/problem_08/sol2.py @@ -28,26 +28,26 @@ from functools import reduce -N = """73167176531330624919225119674426574742355349194934\ -96983520312774506326239578318016984801869478851843\ -85861560789112949495459501737958331952853208805511\ -12540698747158523863050715693290963295227443043557\ -66896648950445244523161731856403098711121722383113\ -62229893423380308135336276614282806444486645238749\ -30358907296290491560440772390713810515859307960866\ -70172427121883998797908792274921901699720888093776\ -65727333001053367881220235421809751254540594752243\ -52584907711670556013604839586446706324415722155397\ -53697817977846174064955149290862569321978468622482\ -83972241375657056057490261407972968652414535100474\ -82166370484403199890008895243450658541227588666881\ -16427171479924442928230863465674813919123162824586\ -17866458359124566529476545682848912883142607690042\ -24219022671055626321111109370544217506941658960408\ -07198403850962455444362981230987879927244284909188\ -84580156166097919133875499200524063689912560717606\ -05886116467109405077541002256983155200055935729725\ -71636269561882670428252483600823257530420752963450""" +N = ('73167176531330624919225119674426574742355349194934' + '96983520312774506326239578318016984801869478851843' + '85861560789112949495459501737958331952853208805511' + '12540698747158523863050715693290963295227443043557' + '66896648950445244523161731856403098711121722383113' + '62229893423380308135336276614282806444486645238749' + '30358907296290491560440772390713810515859307960866' + '70172427121883998797908792274921901699720888093776' + '65727333001053367881220235421809751254540594752243' + '52584907711670556013604839586446706324415722155397' + '53697817977846174064955149290862569321978468622482' + '83972241375657056057490261407972968652414535100474' + '82166370484403199890008895243450658541227588666881' + '16427171479924442928230863465674813919123162824586' + '17866458359124566529476545682848912883142607690042' + '24219022671055626321111109370544217506941658960408' + '07198403850962455444362981230987879927244284909188' + '84580156166097919133875499200524063689912560717606' + '05886116467109405077541002256983155200055935729725' + '71636269561882670428252483600823257530420752963450') def solution(n): """Find the thirteen adjacent digits in the 1000-digit number n that have the greatest product and returns it. @@ -58,4 +58,4 @@ def solution(n): return max([reduce(lambda x,y: int(x)*int(y),n[i:i+13]) for i in range(len(n)-12)]) if __name__ == "__main__": - print(solution(N)) + print(solution(str(N))) From 6c1181163e06cfee25bd4b31349446c8e4b5d4f0 Mon Sep 17 00:00:00 2001 From: Bruno Date: Mon, 15 Jul 2019 19:44:53 -0300 Subject: [PATCH 07/21] Added problems description and doctests to the ones that were missing. Limited line length to 79 and executed python black over all scripts. --- project_euler/problem_01/sol1.py | 9 +- project_euler/problem_01/sol2.py | 21 +- project_euler/problem_01/sol3.py | 63 +-- project_euler/problem_01/sol4.py | 17 +- project_euler/problem_01/sol5.py | 12 +- project_euler/problem_01/sol6.py | 13 +- project_euler/problem_02/sol1.py | 38 +- project_euler/problem_02/sol2.py | 26 +- project_euler/problem_02/sol3.py | 36 +- project_euler/problem_02/sol4.py | 26 +- project_euler/problem_03/sol1.py | 43 +- project_euler/problem_03/sol2.py | 30 +- project_euler/problem_04/sol1.py | 30 +- project_euler/problem_04/sol2.py | 26 +- project_euler/problem_05/sol1.py | 34 +- project_euler/problem_05/sol2.py | 37 +- project_euler/problem_06/sol1.py | 27 +- project_euler/problem_06/sol2.py | 27 +- project_euler/problem_06/sol3.py | 25 +- project_euler/problem_07/sol1.py | 45 ++- project_euler/problem_07/sol2.py | 30 +- project_euler/problem_07/sol3.py | 20 +- project_euler/problem_08/sol1.py | 23 +- project_euler/problem_08/sol2.py | 64 +-- project_euler/problem_09/sol1.py | 15 +- project_euler/problem_09/sol2.py | 33 +- project_euler/problem_09/sol3.py | 24 +- project_euler/problem_10/sol1.py | 41 +- project_euler/problem_10/sol2.py | 23 +- project_euler/problem_11/sol1.py | 21 +- project_euler/problem_11/sol2.py | 19 +- project_euler/problem_12/sol1.py | 45 ++- project_euler/problem_12/sol2.py | 36 +- project_euler/problem_13/sol1.py | 11 +- project_euler/problem_14/sol1.py | 32 +- project_euler/problem_14/sol2.py | 32 +- project_euler/problem_15/sol1.py | 71 +++- project_euler/problem_16/sol1.py | 37 +- project_euler/problem_16/sol2.py | 34 +- project_euler/problem_17/sol1.py | 92 +++-- project_euler/problem_19/sol1.py | 75 ++-- project_euler/problem_20/sol1.py | 50 ++- project_euler/problem_20/sol2.py | 36 +- project_euler/problem_21/sol1.py | 69 +++- project_euler/problem_22/sol1.py | 56 +-- project_euler/problem_22/sol2.py | 571 ++------------------------- project_euler/problem_234/sol1.py | 52 ++- project_euler/problem_24/sol1.py | 30 +- project_euler/problem_25/sol1.py | 84 +++- project_euler/problem_25/sol2.py | 67 +++- project_euler/problem_28/sol1.py | 77 ++-- project_euler/problem_29/solution.py | 64 +-- project_euler/problem_31/sol1.py | 34 +- project_euler/problem_36/sol1.py | 61 ++- project_euler/problem_40/sol1.py | 47 ++- project_euler/problem_48/sol1.py | 26 +- project_euler/problem_52/sol1.py | 46 ++- project_euler/problem_53/sol1.py | 46 ++- project_euler/problem_76/sol1.py | 57 ++- 59 files changed, 1558 insertions(+), 1278 deletions(-) diff --git a/project_euler/problem_01/sol1.py b/project_euler/problem_01/sol1.py index 0632451e7343..1433129af303 100644 --- a/project_euler/problem_01/sol1.py +++ b/project_euler/problem_01/sol1.py @@ -1,15 +1,17 @@ -''' +""" Problem Statement: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. -''' +""" from __future__ import print_function + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): """Returns the sum of all the multiples of 3 or 5 below n. @@ -27,5 +29,6 @@ def solution(n): return sum([e for e in range(3, n) if e % 3 == 0 or e % 5 == 0]) + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_01/sol2.py b/project_euler/problem_01/sol2.py index 4c45318b1fb2..e58fb03a8fb0 100644 --- a/project_euler/problem_01/sol2.py +++ b/project_euler/problem_01/sol2.py @@ -1,15 +1,17 @@ -''' +""" Problem Statement: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. -''' +""" from __future__ import print_function + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): """Returns the sum of all the multiples of 3 or 5 below n. @@ -24,13 +26,14 @@ def solution(n): """ sum = 0 - terms = (n-1)//3 - sum+= ((terms)*(6+(terms-1)*3))//2 #sum of an A.P. - terms = (n-1)//5 - sum+= ((terms)*(10+(terms-1)*5))//2 - terms = (n-1)//15 - sum-= ((terms)*(30+(terms-1)*15))//2 + terms = (n - 1) // 3 + sum += ((terms) * (6 + (terms - 1) * 3)) // 2 # sum of an A.P. + terms = (n - 1) // 5 + sum += ((terms) * (10 + (terms - 1) * 5)) // 2 + terms = (n - 1) // 15 + sum -= ((terms) * (30 + (terms - 1) * 15)) // 2 return sum + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_01/sol3.py b/project_euler/problem_01/sol3.py index bbeac8b84e22..013ce5e54fdf 100644 --- a/project_euler/problem_01/sol3.py +++ b/project_euler/problem_01/sol3.py @@ -1,18 +1,22 @@ -''' +""" Problem Statement: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. -''' +""" from __future__ import print_function + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 -"""This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3.""" + def solution(n): - """Returns the sum of all the multiples of 3 or 5 below n. + """ + This solution is based on the pattern that the successive numbers in the + series follow: 0+3,+2,+1,+3,+1,+2,+3. + Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) 0 @@ -24,38 +28,39 @@ def solution(n): 83700 """ - sum=0 - num=0 - while(1): - num+=3 - if(num>=n): + sum = 0 + num = 0 + while 1: + num += 3 + if num >= n: break - sum+=num - num+=2 - if(num>=n): + sum += num + num += 2 + if num >= n: break - sum+=num - num+=1 - if(num>=n): + sum += num + num += 1 + if num >= n: break - sum+=num - num+=3 - if(num>=n): + sum += num + num += 3 + if num >= n: break - sum+=num - num+=1 - if(num>=n): + sum += num + num += 1 + if num >= n: break - sum+=num - num+=2 - if(num>=n): + sum += num + num += 2 + if num >= n: break - sum+=num - num+=3 - if(num>=n): + sum += num + num += 3 + if num >= n: break - sum+=num + sum += num return sum + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_01/sol4.py b/project_euler/problem_01/sol4.py index f295b8d37b2b..90403c3bd6a3 100644 --- a/project_euler/problem_01/sol4.py +++ b/project_euler/problem_01/sol4.py @@ -1,15 +1,17 @@ -''' +""" Problem Statement: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. -''' +""" from __future__ import print_function + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): """Returns the sum of all the multiples of 3 or 5 below n. @@ -30,7 +32,7 @@ def solution(n): temp = 1 while True: result = z * temp - if (result < n): + if result < n: zmulti.append(result) temp += 1 else: @@ -38,13 +40,14 @@ def solution(n): break while True: result = x * temp - if (result < n): + if result < n: xmulti.append(result) temp += 1 else: break - collection = list(set(xmulti+zmulti)) - return (sum(collection)) + collection = list(set(xmulti + zmulti)) + return sum(collection) + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_01/sol5.py b/project_euler/problem_01/sol5.py index c38e133afaa8..302fe44f8bfa 100644 --- a/project_euler/problem_01/sol5.py +++ b/project_euler/problem_01/sol5.py @@ -1,16 +1,19 @@ -''' +""" Problem Statement: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. -''' +""" from __future__ import print_function + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 """A straightforward pythonic solution using list comprehension""" + + def solution(n): """Returns the sum of all the multiples of 3 or 5 below n. @@ -24,7 +27,8 @@ def solution(n): 83700 """ - return sum([i for i in range(n) if i%3==0 or i%5==0]) + return sum([i for i in range(n) if i % 3 == 0 or i % 5 == 0]) + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_01/sol6.py b/project_euler/problem_01/sol6.py index 27145bfb40ec..cf6e751d4c05 100644 --- a/project_euler/problem_01/sol6.py +++ b/project_euler/problem_01/sol6.py @@ -1,15 +1,17 @@ -''' +""" Problem Statement: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below N. -''' +""" from __future__ import print_function + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): """Returns the sum of all the multiples of 3 or 5 below n. @@ -26,12 +28,13 @@ def solution(n): a = 3 result = 0 while a < n: - if(a % 3 == 0 or a % 5 == 0): + if a % 3 == 0 or a % 5 == 0: result += a - elif(a % 15 == 0): + elif a % 15 == 0: result -= a a += 1 return result + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_02/sol1.py b/project_euler/problem_02/sol1.py index 97a68127f91c..f61d04e3dfce 100644 --- a/project_euler/problem_02/sol1.py +++ b/project_euler/problem_02/sol1.py @@ -1,20 +1,25 @@ -''' +""" Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, -the first 10 terms will be: - 1,2,3,5,8,13,21,34,55,89,.. -By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. -e.g. for n=10, we have {2,8}, sum is 10. -''' +Each new term in the Fibonacci sequence is generated by adding the previous two +terms. By starting with 1 and 2, the first 10 terms will be: + + 1,2,3,5,8,13,21,34,55,89,.. + +By considering the terms in the Fibonacci sequence whose values do not exceed +n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is +10. +""" from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. + """Returns the sum of all fibonacci sequence even elements that are lower + or equals to n. >>> solution(10) 10 @@ -27,15 +32,16 @@ def solution(n): >>> solution(34) 44 """ - i=1 - j=2 - sum=0 - while(j<=n): - if j%2 == 0: - sum+=j - i , j = j, i+j + i = 1 + j = 2 + sum = 0 + while j <= n: + if j % 2 == 0: + sum += j + i, j = j, i + j return sum + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_02/sol2.py b/project_euler/problem_02/sol2.py index 751e1d1e8cd0..3e103a6a4373 100644 --- a/project_euler/problem_02/sol2.py +++ b/project_euler/problem_02/sol2.py @@ -1,20 +1,25 @@ -''' +""" Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, -the first 10 terms will be: - 1,2,3,5,8,13,21,34,55,89,.. -By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. -e.g. for n=10, we have {2,8}, sum is 10. -''' +Each new term in the Fibonacci sequence is generated by adding the previous two +terms. By starting with 1 and 2, the first 10 terms will be: + + 1,2,3,5,8,13,21,34,55,89,.. + +By considering the terms in the Fibonacci sequence whose values do not exceed +n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is +10. +""" from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. + """Returns the sum of all fibonacci sequence even elements that are lower + or equals to n. >>> solution(10) [2, 8] @@ -32,8 +37,9 @@ def solution(n): while b <= n: if b % 2 == 0: ls.append(b) - a, b = b, a+b + a, b = b, a + b return ls + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_02/sol3.py b/project_euler/problem_02/sol3.py index 4502fa4950f8..abd9d6c753b8 100644 --- a/project_euler/problem_02/sol3.py +++ b/project_euler/problem_02/sol3.py @@ -1,20 +1,25 @@ -''' +""" Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, -the first 10 terms will be: - 1,2,3,5,8,13,21,34,55,89,.. -By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. -e.g. for n=10, we have {2,8}, sum is 10. -''' +Each new term in the Fibonacci sequence is generated by adding the previous +two terms. By starting with 1 and 2, the first 10 terms will be: + + 1,2,3,5,8,13,21,34,55,89,.. + +By considering the terms in the Fibonacci sequence whose values do not exceed +n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is +10. +""" from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. + """Returns the sum of all fibonacci sequence even elements that are lower + or equals to n. >>> solution(10) 10 @@ -29,13 +34,14 @@ def solution(n): """ if n <= 1: return 0 - a=0 - b=2 - count=0 - while 4*b+a<=n: - a, b = b, 4*b+a - count+= a + a = 0 + b = 2 + count = 0 + while 4 * b + a <= n: + a, b = b, 4 * b + a + count += a return count + b + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_02/sol4.py b/project_euler/problem_02/sol4.py index b00068ca73bf..ba13b12a15e9 100644 --- a/project_euler/problem_02/sol4.py +++ b/project_euler/problem_02/sol4.py @@ -1,22 +1,27 @@ -''' +""" Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, -the first 10 terms will be: - 1,2,3,5,8,13,21,34,55,89,.. -By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. -e.g. for n=10, we have {2,8}, sum is 10. -''' +Each new term in the Fibonacci sequence is generated by adding the previous two +terms. By starting with 1 and 2, the first 10 terms will be: + + 1,2,3,5,8,13,21,34,55,89,.. + +By considering the terms in the Fibonacci sequence whose values do not exceed +n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is +10. +""" from __future__ import print_function import math from decimal import * try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. + """Returns the sum of all fibonacci sequence even elements that are lower + or equals to n. >>> solution(10) 10 @@ -32,10 +37,11 @@ def solution(n): getcontext().prec = 100 phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2) - index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2 + index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2 num = Decimal(round(phi ** Decimal(index + 1))) / (phi + 2) sum = num // 2 return int(sum) + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_03/sol1.py b/project_euler/problem_03/sol1.py index b0a9ab03040c..c2e601bd0040 100644 --- a/project_euler/problem_03/sol1.py +++ b/project_euler/problem_03/sol1.py @@ -1,27 +1,31 @@ -''' +""" Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? +The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor +of a given number N? + e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. -''' +""" from __future__ import print_function, division import math try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def isprime(no): - if(no==2): + if no == 2: return True - elif (no%2==0): + elif no % 2 == 0: return False - sq = int(math.sqrt(no))+1 - for i in range(3,sq,2): - if(no%i==0): + sq = int(math.sqrt(no)) + 1 + for i in range(3, sq, 2): + if no % i == 0: return False return True + def solution(n): """Returns the largest prime factor of a given number n. @@ -33,24 +37,25 @@ def solution(n): 17 """ maxNumber = 0 - if(isprime(n)): + if isprime(n): return n else: - while (n%2==0): - n=n/2 - if(isprime(n)): + while n % 2 == 0: + n = n / 2 + if isprime(n): return int(n) else: - n1 = int(math.sqrt(n))+1 - for i in range(3,n1,2): - if(n%i==0): - if(isprime(n/i)): - maxNumber = n/i + n1 = int(math.sqrt(n)) + 1 + for i in range(3, n1, 2): + if n % i == 0: + if isprime(n / i): + maxNumber = n / i break - elif(isprime(i)): + elif isprime(i): maxNumber = i return maxNumber return int(sum) + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_03/sol2.py b/project_euler/problem_03/sol2.py index f9472b2fdf24..497db3965cc3 100644 --- a/project_euler/problem_03/sol2.py +++ b/project_euler/problem_03/sol2.py @@ -1,16 +1,19 @@ -''' +""" Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? +The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor +of a given number N? + e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. -''' +""" from __future__ import print_function, division import math try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): """Returns the largest prime factor of a given number n. @@ -21,16 +24,17 @@ def solution(n): >>> solution(17) 17 """ - prime=1 - i=2 - while(i*i<=n): - while(n%i==0): - prime=i - n//=i - i+=1 - if(n>1): - prime=n + prime = 1 + i = 2 + while i * i <= n: + while n % i == 0: + prime = i + n //= i + i += 1 + if n > 1: + prime = n return int(prime) + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_04/sol1.py b/project_euler/problem_04/sol1.py index d4dbf10539fe..7a255f7308e6 100644 --- a/project_euler/problem_04/sol1.py +++ b/project_euler/problem_04/sol1.py @@ -1,17 +1,22 @@ -''' +""" Problem: -A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99. -Find the largest palindrome made from the product of two 3-digit numbers which is less than N. -''' +A palindromic number reads the same both ways. The largest palindrome made from +the product of two 2-digit numbers is 9009 = 91 x 99. + +Find the largest palindrome made from the product of two 3-digit numbers which +is less than N. +""" from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the largest palindrome made from the product of two 3-digit numbers which is less than n. + """Returns the largest palindrome made from the product of two 3-digit + numbers which is less than n. >>> solution(20000) 19591 @@ -21,22 +26,25 @@ def solution(n): 39893 """ # fetchs the next number - for number in range(n-1,10000,-1): + for number in range(n - 1, 10000, -1): # converts number into string. strNumber = str(number) # checks whether 'strNumber' is a palindrome. - if(strNumber == strNumber[::-1]): + if strNumber == strNumber[::-1]: divisor = 999 # if 'number' is a product of two 3-digit numbers # then number is the answer otherwise fetch next number. - while(divisor != 99): - if((number % divisor == 0) and (len(str(int(number / divisor))) == 3)): + while divisor != 99: + if (number % divisor == 0) and ( + len(str(int(number / divisor))) == 3 + ): return number - divisor -=1 + divisor -= 1 + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_04/sol2.py b/project_euler/problem_04/sol2.py index 10e46025a782..45c6b256daf8 100644 --- a/project_euler/problem_04/sol2.py +++ b/project_euler/problem_04/sol2.py @@ -1,17 +1,22 @@ -''' +""" Problem: -A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99. -Find the largest palindrome made from the product of two 3-digit numbers which is less than N. -''' +A palindromic number reads the same both ways. The largest palindrome made from +the product of two 2-digit numbers is 9009 = 91 x 99. + +Find the largest palindrome made from the product of two 3-digit numbers which +is less than N. +""" from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the largest palindrome made from the product of two 3-digit numbers which is less than n. + """Returns the largest palindrome made from the product of two 3-digit + numbers which is less than n. >>> solution(20000) 19591 @@ -21,12 +26,13 @@ def solution(n): 39893 """ answer = 0 - for i in range(999,99,-1): #3 digit nimbers range from 999 down to 100 - for j in range(999,99,-1): - t = str(i*j) - if t == t[::-1] and i*j < n: + for i in range(999, 99, -1): # 3 digit nimbers range from 999 down to 100 + for j in range(999, 99, -1): + t = str(i * j) + if t == t[::-1] and i * j < n: answer = max(answer, i * j) return answer + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_05/sol1.py b/project_euler/problem_05/sol1.py index 38b98816e9f2..609f02102a08 100644 --- a/project_euler/problem_05/sol1.py +++ b/project_euler/problem_05/sol1.py @@ -1,17 +1,22 @@ -''' +""" Problem: -2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. -What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N? -''' +2520 is the smallest number that can be divided by each of the numbers from 1 +to 10 without any remainder. + +What is the smallest positive number that is evenly divisible(divisible with no +remainder) by all of the numbers from 1 to N? +""" from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to n. + """Returns the smallest positive number that is evenly divisible(divisible + with no remainder) by all of the numbers from 1 to n. >>> solution(10) 2520 @@ -24,17 +29,18 @@ def solution(n): """ i = 0 while 1: - i+=n*(n-1) - nfound=0 - for j in range(2,n): - if (i%j != 0): - nfound=1 + i += n * (n - 1) + nfound = 0 + for j in range(2, n): + if i % j != 0: + nfound = 1 break - if(nfound==0): - if(i==0): - i=1 + if nfound == 0: + if i == 0: + i = 1 return i break + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_05/sol2.py b/project_euler/problem_05/sol2.py index 4964e65c02e2..293dd96f2294 100644 --- a/project_euler/problem_05/sol2.py +++ b/project_euler/problem_05/sol2.py @@ -1,25 +1,35 @@ -''' +""" Problem: -2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. -What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N? -''' +2520 is the smallest number that can be divided by each of the numbers from 1 +to 10 without any remainder. + +What is the smallest positive number that is evenly divisible(divisible with no +remainder) by all of the numbers from 1 to N? +""" from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 """ Euclidean GCD Algorithm """ -def gcd(x,y): - return x if y==0 else gcd(y,x%y) + + +def gcd(x, y): + return x if y == 0 else gcd(y, x % y) + """ Using the property lcm*gcd of two numbers = product of them """ -def lcm(x,y): - return (x*y)//gcd(x,y) + + +def lcm(x, y): + return (x * y) // gcd(x, y) + def solution(n): - """Returns the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to n. + """Returns the smallest positive number that is evenly divisible(divisible + with no remainder) by all of the numbers from 1 to n. >>> solution(10) 2520 @@ -30,10 +40,11 @@ def solution(n): >>> solution(22) 232792560 """ - g=1 - for i in range(1,n+1): - g=lcm(g,i) + g = 1 + for i in range(1, n + 1): + g = lcm(g, i) return g + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_06/sol1.py b/project_euler/problem_06/sol1.py index f9433d20fa04..728701e167c3 100644 --- a/project_euler/problem_06/sol1.py +++ b/project_euler/problem_06/sol1.py @@ -1,22 +1,30 @@ # -*- coding: utf-8 -*- -''' +""" Problem: + The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385 + The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)^2 = 552 = 3025 -Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. -Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. -''' + +Hence the difference between the sum of the squares of the first ten natural +numbers and the square of the sum is 3025 − 385 = 2640. + +Find the difference between the sum of the squares of the first N natural +numbers and the square of the sum. +""" from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. + """Returns the difference between the sum of the squares of the first n + natural numbers and the square of the sum. >>> solution(10) 2640 @@ -29,11 +37,12 @@ def solution(n): """ suma = 0 sumb = 0 - for i in range(1,n+1): - suma += i**2 + for i in range(1, n + 1): + suma += i ** 2 sumb += i - sum = sumb**2 - suma + sum = sumb ** 2 - suma return sum + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_06/sol2.py b/project_euler/problem_06/sol2.py index e261a51f38ad..2c64812d56f8 100644 --- a/project_euler/problem_06/sol2.py +++ b/project_euler/problem_06/sol2.py @@ -1,22 +1,30 @@ # -*- coding: utf-8 -*- -''' +""" Problem: + The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385 + The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)^2 = 552 = 3025 -Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. -Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. -''' + +Hence the difference between the sum of the squares of the first ten natural +numbers and the square of the sum is 3025 − 385 = 2640. + +Find the difference between the sum of the squares of the first N natural +numbers and the square of the sum. +""" from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. + """Returns the difference between the sum of the squares of the first n + natural numbers and the square of the sum. >>> solution(10) 2640 @@ -27,10 +35,11 @@ def solution(n): >>> solution(50) 1582700 """ - suma = n*(n+1)/2 + suma = n * (n + 1) / 2 suma **= 2 - sumb = n*(n+1)*(2*n+1)/6 - return int(suma-sumb) + sumb = n * (n + 1) * (2 * n + 1) / 6 + return int(suma - sumb) + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_06/sol3.py b/project_euler/problem_06/sol3.py index 5c9eca3130d8..7d94b1e2254f 100644 --- a/project_euler/problem_06/sol3.py +++ b/project_euler/problem_06/sol3.py @@ -1,23 +1,31 @@ # -*- coding: utf-8 -*- -''' +""" Problem: + The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385 + The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)^2 = 552 = 3025 -Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. -Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. -''' + +Hence the difference between the sum of the squares of the first ten natural +numbers and the square of the sum is 3025 − 385 = 2640. + +Find the difference between the sum of the squares of the first N natural +numbers and the square of the sum. +""" from __future__ import print_function import math try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. + """Returns the difference between the sum of the squares of the first n + natural numbers and the square of the sum. >>> solution(10) 2640 @@ -28,9 +36,10 @@ def solution(n): >>> solution(50) 1582700 """ - sum_of_squares = sum([i*i for i in range(1,n+1)]) - square_of_sum = int(math.pow(sum(range(1,n+1)),2)) + sum_of_squares = sum([i * i for i in range(1, n + 1)]) + square_of_sum = int(math.pow(sum(range(1, n + 1)), 2)) return square_of_sum - sum_of_squares + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_07/sol1.py b/project_euler/problem_07/sol1.py index 041807fb2f9f..403ded568dda 100644 --- a/project_euler/problem_07/sol1.py +++ b/project_euler/problem_07/sol1.py @@ -1,29 +1,33 @@ # -*- coding: utf-8 -*- -''' +""" By listing the first six prime numbers: -2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. -What is the Nth prime number? -''' + + 2, 3, 5, 7, 11, and 13 + +We can see that the 6th prime is 13. What is the Nth prime number? +""" from __future__ import print_function from math import sqrt try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def isprime(n): - if (n==2): + if n == 2: return True - elif (n%2==0): + elif n % 2 == 0: return False else: - sq = int(sqrt(n))+1 - for i in range(3,sq,2): - if(n%i==0): + sq = int(sqrt(n)) + 1 + for i in range(3, sq, 2): + if n % i == 0: return False return True + def solution(n): """Returns the n-th prime number. @@ -40,17 +44,18 @@ def solution(n): >>> solution(100) 541 """ - i=0 - j=1 - while(i!=n and j<3): - j+=1 - if (isprime(j)): - i+=1 - while(i!=n): - j+=2 - if(isprime(j)): - i+=1 + i = 0 + j = 1 + while i != n and j < 3: + j += 1 + if isprime(j): + i += 1 + while i != n: + j += 2 + if isprime(j): + i += 1 return j + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_07/sol2.py b/project_euler/problem_07/sol2.py index c0dbae209878..630e5196796d 100644 --- a/project_euler/problem_07/sol2.py +++ b/project_euler/problem_07/sol2.py @@ -1,24 +1,27 @@ # -*- coding: utf-8 -*- -''' +""" By listing the first six prime numbers: -2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. -What is the Nth prime number? -''' + + 2, 3, 5, 7, 11, and 13 + +We can see that the 6th prime is 13. What is the Nth prime number? +""" from __future__ import print_function from math import sqrt try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 -# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the Nth prime number? + def isprime(number): - for i in range(2,int(number**0.5)+1): - if number%i==0: + for i in range(2, int(number ** 0.5) + 1): + if number % i == 0: return False return True + def solution(n): """Returns the n-th prime number. @@ -38,12 +41,13 @@ def solution(n): primes = [] num = 2 while len(primes) < n: - if isprime(num): - primes.append(num) - num += 1 - else: - num += 1 + if isprime(num): + primes.append(num) + num += 1 + else: + num += 1 return primes[len(primes) - 1] + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_07/sol3.py b/project_euler/problem_07/sol3.py index 0f23d6c65187..bc94762604b3 100644 --- a/project_euler/problem_07/sol3.py +++ b/project_euler/problem_07/sol3.py @@ -1,29 +1,34 @@ # -*- coding: utf-8 -*- -''' +""" By listing the first six prime numbers: -2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. -What is the Nth prime number? -''' + + 2, 3, 5, 7, 11, and 13 + +We can see that the 6th prime is 13. What is the Nth prime number? +""" from __future__ import print_function import math import itertools try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def primeCheck(number): if number % 2 == 0 and number > 2: return False return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) + def prime_generator(): num = 2 while True: if primeCheck(num): yield num - num+=1 + num += 1 + def solution(n): """Returns the n-th prime number. @@ -41,7 +46,8 @@ def solution(n): >>> solution(100) 541 """ - return next(itertools.islice(prime_generator(),n-1,n)) + return next(itertools.islice(prime_generator(), n - 1, n)) + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_08/sol1.py b/project_euler/problem_08/sol1.py index 65f44a0854e1..6752fae3de60 100644 --- a/project_euler/problem_08/sol1.py +++ b/project_euler/problem_08/sol1.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- -''' -The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. +""" +The four adjacent digits in the 1000-digit number that have the greatest +product are 9 × 9 × 8 × 9 = 5832. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 @@ -23,8 +24,9 @@ 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 -Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? -''' +Find the thirteen adjacent digits in the 1000-digit number that have the +greatest product. What is the value of this product? +""" import sys N = """73167176531330624919225119674426574742355349194934\ @@ -48,20 +50,23 @@ 05886116467109405077541002256983155200055935729725\ 71636269561882670428252483600823257530420752963450""" + def solution(n): - """Find the thirteen adjacent digits in the 1000-digit number n that have the greatest product and returns it. + """Find the thirteen adjacent digits in the 1000-digit number n that have + the greatest product and returns it. >>> solution(N) 23514624000 """ - LargestProduct = -sys.maxsize-1 - for i in range(len(n)-12): - product=1 + LargestProduct = -sys.maxsize - 1 + for i in range(len(n) - 12): + product = 1 for j in range(13): - product *= int(n[i+j]) + product *= int(n[i + j]) if product > LargestProduct: LargestProduct = product return LargestProduct + if __name__ == "__main__": print(solution(N)) diff --git a/project_euler/problem_08/sol2.py b/project_euler/problem_08/sol2.py index 964fa70a35e4..bae96e373d6c 100644 --- a/project_euler/problem_08/sol2.py +++ b/project_euler/problem_08/sol2.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- -''' -The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. +""" +The four adjacent digits in the 1000-digit number that have the greatest +product are 9 × 9 × 8 × 9 = 5832. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 @@ -23,39 +24,50 @@ 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 -Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? -''' +Find the thirteen adjacent digits in the 1000-digit number that have the +greatest product. What is the value of this product? +""" from functools import reduce -N = ('73167176531330624919225119674426574742355349194934' - '96983520312774506326239578318016984801869478851843' - '85861560789112949495459501737958331952853208805511' - '12540698747158523863050715693290963295227443043557' - '66896648950445244523161731856403098711121722383113' - '62229893423380308135336276614282806444486645238749' - '30358907296290491560440772390713810515859307960866' - '70172427121883998797908792274921901699720888093776' - '65727333001053367881220235421809751254540594752243' - '52584907711670556013604839586446706324415722155397' - '53697817977846174064955149290862569321978468622482' - '83972241375657056057490261407972968652414535100474' - '82166370484403199890008895243450658541227588666881' - '16427171479924442928230863465674813919123162824586' - '17866458359124566529476545682848912883142607690042' - '24219022671055626321111109370544217506941658960408' - '07198403850962455444362981230987879927244284909188' - '84580156166097919133875499200524063689912560717606' - '05886116467109405077541002256983155200055935729725' - '71636269561882670428252483600823257530420752963450') +N = ( + "73167176531330624919225119674426574742355349194934" + "96983520312774506326239578318016984801869478851843" + "85861560789112949495459501737958331952853208805511" + "12540698747158523863050715693290963295227443043557" + "66896648950445244523161731856403098711121722383113" + "62229893423380308135336276614282806444486645238749" + "30358907296290491560440772390713810515859307960866" + "70172427121883998797908792274921901699720888093776" + "65727333001053367881220235421809751254540594752243" + "52584907711670556013604839586446706324415722155397" + "53697817977846174064955149290862569321978468622482" + "83972241375657056057490261407972968652414535100474" + "82166370484403199890008895243450658541227588666881" + "16427171479924442928230863465674813919123162824586" + "17866458359124566529476545682848912883142607690042" + "24219022671055626321111109370544217506941658960408" + "07198403850962455444362981230987879927244284909188" + "84580156166097919133875499200524063689912560717606" + "05886116467109405077541002256983155200055935729725" + "71636269561882670428252483600823257530420752963450" +) + def solution(n): - """Find the thirteen adjacent digits in the 1000-digit number n that have the greatest product and returns it. + """Find the thirteen adjacent digits in the 1000-digit number n that have + the greatest product and returns it. >>> solution(N) 23514624000 """ - return max([reduce(lambda x,y: int(x)*int(y),n[i:i+13]) for i in range(len(n)-12)]) + return max( + [ + reduce(lambda x, y: int(x) * int(y), n[i : i + 13]) + for i in range(len(n) - 12) + ] + ) + if __name__ == "__main__": print(solution(str(N))) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index 7dfe2f9f1ea2..0f368e48d2e3 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -1,4 +1,4 @@ -''' +""" Problem Statement: A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 @@ -6,12 +6,14 @@ There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. -''' +""" from __future__ import print_function + def solution(): """ - Returns the product of a,b,c which are Pythagorean Triplet that satisfies the following: + Returns the product of a,b,c which are Pythagorean Triplet that satisfies + the following: 1. a < b < c 2. a**2 + b**2 = c**2 3. a + b + c = 1000 @@ -22,12 +24,13 @@ def solution(): for a in range(300): for b in range(400): for c in range(500): - if(a < b < c): - if((a**2) + (b**2) == (c**2)): - if((a+b+c) == 1000): + if a < b < c: + if (a ** 2) + (b ** 2) == (c ** 2): + if (a + b + c) == 1000: return a * b * c break + if __name__ == "__main__": print("Please Wait...") print(solution()) diff --git a/project_euler/problem_09/sol2.py b/project_euler/problem_09/sol2.py index 12e4f9d7811d..674daae9ec8e 100644 --- a/project_euler/problem_09/sol2.py +++ b/project_euler/problem_09/sol2.py @@ -1,4 +1,4 @@ -''' +""" Problem Statement: A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 @@ -6,16 +6,19 @@ There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. -''' +""" from __future__ import print_function + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): """ - Return the product of a,b,c which are Pythagorean Triplet that satisfies the following: + Return the product of a,b,c which are Pythagorean Triplet that satisfies + the following: 1. a < b < c 2. a**2 + b**2 = c**2 3. a + b + c = 1000 @@ -23,17 +26,19 @@ def solution(n): >>> solution(1000) 31875000 """ - product=-1 - d=0 - for a in range(1,n//3): - """Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c """ - b=(n*n-2*a*n)//(2*n-2*a) - c=n-a-b - if c*c==(a*a+b*b): - d=(a*b*c) - if d>=product: - product=d + product = -1 + d = 0 + for a in range(1, n // 3): + """Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c + """ + b = (n * n - 2 * a * n) // (2 * n - 2 * a) + c = n - a - b + if c * c == (a * a + b * b): + d = a * b * c + if d >= product: + product = d return product + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_09/sol3.py b/project_euler/problem_09/sol3.py index 42cb5ee0be31..f749b8a61f11 100644 --- a/project_euler/problem_09/sol3.py +++ b/project_euler/problem_09/sol3.py @@ -1,25 +1,37 @@ -''' +""" Problem Statement: + A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + a^2 + b^2 = c^2 + For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. -''' +""" from __future__ import print_function + def solution(): """ - Returns the product of a,b,c which are Pythagorean Triplet that satisfies the following: + Returns the product of a,b,c which are Pythagorean Triplet that satisfies + the following: + 1. a**2 + b**2 = c**2 2. a + b + c = 1000 - + >>> solution() 31875000 """ - return ([a*b*c for a in range(1,999) for b in range(a,999) for c in range(b,999) - if (a*a+b*b==c*c) and (a+b+c==1000 )][0]) + return [ + a * b * c + for a in range(1, 999) + for b in range(a, 999) + for c in range(b, 999) + if (a * a + b * b == c * c) and (a + b + c == 1000) + ][0] + if __name__ == "__main__": print(solution()) diff --git a/project_euler/problem_10/sol1.py b/project_euler/problem_10/sol1.py index f01f27803726..038da96e6352 100644 --- a/project_euler/problem_10/sol1.py +++ b/project_euler/problem_10/sol1.py @@ -1,39 +1,43 @@ -''' +""" Problem Statement: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. -''' +""" from __future__ import print_function from math import sqrt + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: - raw_input = input # Python 3 + raw_input = input # Python 3 try: - xrange # Python 2 + xrange # Python 2 except NameError: - xrange = range # Python 3 + xrange = range # Python 3 + def is_prime(n): - for i in xrange(2, int(sqrt(n))+1): - if n%i == 0: - return False + for i in xrange(2, int(sqrt(n)) + 1): + if n % i == 0: + return False + + return True - return True def sum_of_primes(n): - if n > 2: - sumOfPrimes = 2 - else: - return 0 + if n > 2: + sumOfPrimes = 2 + else: + return 0 - for i in xrange(3, n, 2): - if is_prime(i): - sumOfPrimes += i + for i in xrange(3, n, 2): + if is_prime(i): + sumOfPrimes += i + + return sumOfPrimes - return sumOfPrimes def solution(n): """Returns the sum of all the primes below n. @@ -51,5 +55,6 @@ def solution(n): """ return sum_of_primes(n) + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_10/sol2.py b/project_euler/problem_10/sol2.py index fd5eaf9c9ee6..9e51d61b8749 100644 --- a/project_euler/problem_10/sol2.py +++ b/project_euler/problem_10/sol2.py @@ -1,28 +1,32 @@ -''' +""" Problem Statement: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. -''' +""" from __future__ import print_function -import math -from itertools import takewhile +import math +from itertools import takewhile + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: - raw_input = input # Python 3 + raw_input = input # Python 3 + def primeCheck(number): if number % 2 == 0 and number > 2: return False return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) - + + def prime_generator(): num = 2 while True: if primeCheck(num): yield num - num+=1 + num += 1 + def solution(n): """Returns the sum of all the primes below n. @@ -38,7 +42,8 @@ def solution(n): >>> solution(7) 10 """ - return sum(takewhile(lambda x: x < n,prime_generator())) + return sum(takewhile(lambda x: x < n, prime_generator())) + if __name__ == "__main__": print(solution(int(raw_input().strip()))) diff --git a/project_euler/problem_11/sol1.py b/project_euler/problem_11/sol1.py index 60dcb080c598..6c1ae855dae2 100644 --- a/project_euler/problem_11/sol1.py +++ b/project_euler/problem_11/sol1.py @@ -1,5 +1,6 @@ """ -What is the greatest product of four adjacent numbers (horizontally, vertically, or diagonally) in this 20x20 array? +What is the greatest product of four adjacent numbers (horizontally, +vertically, or diagonally) in this 20x20 array? 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 @@ -30,6 +31,7 @@ except NameError: xrange = range # Python 2 + def largest_product(grid): nColumns = len(grid[0]) nRows = len(grid) @@ -38,11 +40,16 @@ def largest_product(grid): lrDiagProduct = 0 rlDiagProduct = 0 - # Check vertically, horizontally, diagonally at the same time (only works for nxn grid) + # Check vertically, horizontally, diagonally at the same time (only works + # for nxn grid) for i in xrange(nColumns): for j in xrange(nRows - 3): - vertProduct = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i] - horzProduct = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3] + vertProduct = ( + grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i] + ) + horzProduct = ( + grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3] + ) # Left-to-right diagonal (\) product if i < nColumns - 3: @@ -62,12 +69,15 @@ def largest_product(grid): * grid[i - 3][j + 3] ) - maxProduct = max(vertProduct, horzProduct, lrDiagProduct, rlDiagProduct) + maxProduct = max( + vertProduct, horzProduct, lrDiagProduct, rlDiagProduct + ) if maxProduct > largest: largest = maxProduct return largest + def solution(): """Returns the sum of all the multiples of 3 or 5 below n. @@ -83,5 +93,6 @@ def solution(): return largest_product(grid) + if __name__ == "__main__": print(solution()) diff --git a/project_euler/problem_11/sol2.py b/project_euler/problem_11/sol2.py index 45ecb20c110a..7ed4a97d26fe 100644 --- a/project_euler/problem_11/sol2.py +++ b/project_euler/problem_11/sol2.py @@ -1,5 +1,6 @@ """ -What is the greatest product of four adjacent numbers (horizontally, vertically, or diagonally) in this 20x20 array? +What is the greatest product of four adjacent numbers (horizontally, +vertically, or diagonally) in this 20x20 array? 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 @@ -30,6 +31,7 @@ except NameError: xrange = range # Python 2 + def solution(): """Returns the sum of all the multiples of 3 or 5 below n. @@ -60,17 +62,28 @@ def solution(): # diagonal 1 for i in xrange(17): for j in xrange(17): - temp = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] + temp = ( + l[i][j] + * l[i + 1][j + 1] + * l[i + 2][j + 2] + * l[i + 3][j + 3] + ) if temp > maximum: maximum = temp # diagonal 2 for i in xrange(17): for j in xrange(3, 20): - temp = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] + temp = ( + l[i][j] + * l[i + 1][j - 1] + * l[i + 2][j - 2] + * l[i + 3][j - 3] + ) if temp > maximum: maximum = temp return maximum + if __name__ == "__main__": print(solution()) diff --git a/project_euler/problem_12/sol1.py b/project_euler/problem_12/sol1.py index ee43ffdf01fd..baf9babab686 100644 --- a/project_euler/problem_12/sol1.py +++ b/project_euler/problem_12/sol1.py @@ -1,7 +1,9 @@ -''' +""" Highly divisible triangular numbers Problem 12 -The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: +The sequence of triangle numbers is generated by adding the natural numbers. So +the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten +terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... @@ -16,28 +18,32 @@ 28: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. -What is the value of the first triangle number to have over five hundred divisors? -''' +What is the value of the first triangle number to have over five hundred +divisors? +""" from __future__ import print_function from math import sqrt + try: - xrange #Python 2 + xrange # Python 2 except NameError: - xrange = range #Python 3 + xrange = range # Python 3 + def count_divisors(n): - nDivisors = 0 - for i in xrange(1, int(sqrt(n))+1): - if n%i == 0: - nDivisors += 2 - #check if n is perfect square - if n**0.5 == int(n**0.5): - nDivisors -= 1 - return nDivisors + nDivisors = 0 + for i in xrange(1, int(sqrt(n)) + 1): + if n % i == 0: + nDivisors += 2 + # check if n is perfect square + if n ** 0.5 == int(n ** 0.5): + nDivisors -= 1 + return nDivisors def solution(): - """Returns the value of the first triangle number to have over five hundred divisors. + """Returns the value of the first triangle number to have over five hundred + divisors. >>> solution() 76576500 @@ -46,13 +52,14 @@ def solution(): i = 1 while True: - i += 1 - tNum += i + i += 1 + tNum += i - if count_divisors(tNum) > 500: - break + if count_divisors(tNum) > 500: + break return tNum + if __name__ == "__main__": print(solution()) diff --git a/project_euler/problem_12/sol2.py b/project_euler/problem_12/sol2.py index 03e54c38e15b..071d7516ac0f 100644 --- a/project_euler/problem_12/sol2.py +++ b/project_euler/problem_12/sol2.py @@ -1,7 +1,9 @@ -''' +""" Highly divisible triangular numbers Problem 12 -The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: +The sequence of triangle numbers is generated by adding the natural numbers. So +the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten +terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... @@ -16,24 +18,34 @@ 28: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. -What is the value of the first triangle number to have over five hundred divisors? -''' +What is the value of the first triangle number to have over five hundred +divisors? +""" from __future__ import print_function -def triangle_number_generator(): - for n in range(1,1000000): - yield n*(n+1)//2 - -def count_divisors(n): - return sum([2 for i in range(1,int(n**0.5)+1) if n%i==0 and i*i != n]) + +def triangle_number_generator(): + for n in range(1, 1000000): + yield n * (n + 1) // 2 + + +def count_divisors(n): + return sum( + [2 for i in range(1, int(n ** 0.5) + 1) if n % i == 0 and i * i != n] + ) + def solution(): - """Returns the value of the first triangle number to have over five hundred divisors. + """Returns the value of the first triangle number to have over five hundred + divisors. >>> solution() 76576500 """ - return next(i for i in triangle_number_generator() if count_divisors(i) > 500) + return next( + i for i in triangle_number_generator() if count_divisors(i) > 500 + ) + if __name__ == "__main__": print(solution()) diff --git a/project_euler/problem_13/sol1.py b/project_euler/problem_13/sol1.py index 605b89a556f8..b7118a6150a7 100644 --- a/project_euler/problem_13/sol1.py +++ b/project_euler/problem_13/sol1.py @@ -1,14 +1,16 @@ -''' +""" Problem Statement: -Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. -''' +Work out the first ten digits of the sum of the following one-hundred 50-digit +numbers. +""" from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(array): """Returns the first ten digits of the sum of the array elements. @@ -23,6 +25,7 @@ def solution(array): """ return str(sum(array))[:10] + if __name__ == "__main__": n = int(input().strip()) diff --git a/project_euler/problem_14/sol1.py b/project_euler/problem_14/sol1.py index a85eaf770aaf..6b80cd7cb24b 100644 --- a/project_euler/problem_14/sol1.py +++ b/project_euler/problem_14/sol1.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -''' +""" Problem Statement: The following iterative sequence is defined for the set of positive integers: @@ -10,18 +10,23 @@ 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 -It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. +It can be seen that this sequence (starting at 13 and finishing at 1) contains +10 terms. Although it has not been proved yet (Collatz Problem), it is thought +that all starting numbers finish at 1. Which starting number, under one million, produces the longest chain? -''' +""" from __future__ import print_function + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def solution(n): - """Returns the number under n that generates the longest sequence using the formula: + """Returns the number under n that generates the longest sequence using the + formula: n → n/2 (n is even) n → 3n + 1 (n is odd) @@ -43,17 +48,26 @@ def solution(n): while number > 1: if number % 2 == 0: - number /=2 + number /= 2 counter += 1 else: - number = (3*number)+1 + number = (3 * number) + 1 counter += 1 if counter > pre_counter: largest_number = input1 pre_counter = counter - return {'counter': pre_counter, 'largest_number': largest_number} + return {"counter": pre_counter, "largest_number": largest_number} + if __name__ == "__main__": result = solution(int(raw_input().strip())) - print(('Largest Number:', result['largest_number'], '->', result['counter'], 'digits')) + print( + ( + "Largest Number:", + result["largest_number"], + "->", + result["counter"], + "digits", + ) + ) diff --git a/project_euler/problem_14/sol2.py b/project_euler/problem_14/sol2.py index f0296831fbb1..59fa79515148 100644 --- a/project_euler/problem_14/sol2.py +++ b/project_euler/problem_14/sol2.py @@ -1,9 +1,12 @@ # -*- coding: utf-8 -*- -''' -Collatz conjecture: start with any positive integer n. Next term obtained from the previous term as follows: +""" +Collatz conjecture: start with any positive integer n. Next term obtained from +the previous term as follows: + If the previous term is even, the next term is one half the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. -The conjecture states the sequence will always reach 1 regardless of starting n. +The conjecture states the sequence will always reach 1 regardless of starting +n. Problem Statement: The following iterative sequence is defined for the set of positive integers: @@ -15,16 +18,20 @@ 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 -It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. +It can be seen that this sequence (starting at 13 and finishing at 1) contains +10 terms. Although it has not been proved yet (Collatz Problem), it is thought +that all starting numbers finish at 1. Which starting number, under one million, produces the longest chain? -''' +""" from __future__ import print_function + try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 + def collatz_sequence(n): """Returns the Collatz sequence for n.""" sequence = [n] @@ -32,10 +39,11 @@ def collatz_sequence(n): if n % 2 == 0: n //= 2 else: - n = 3*n +1 + n = 3 * n + 1 sequence.append(n) return sequence + def solution(n): """Returns the number under n that generates the longest Collatz sequence. @@ -49,9 +57,13 @@ def solution(n): {'counter': 276, 'largest_number': 13255} """ - result = max([(len(collatz_sequence(i)), i) for i in range(1, n)]) - return {'counter': result[0], 'largest_number': result[1]} + result = max([(len(collatz_sequence(i)), i) for i in range(1, n)]) + return {"counter": result[0], "largest_number": result[1]} + if __name__ == "__main__": result = solution(int(raw_input().strip())) - print("Longest Collatz sequence under one million is %d with length %d" % (result['largest_number'], result['counter'])) + print( + "Longest Collatz sequence under one million is %d with length %d" + % (result["largest_number"], result["counter"]) + ) diff --git a/project_euler/problem_15/sol1.py b/project_euler/problem_15/sol1.py index d24748011ef9..de58bb436d68 100644 --- a/project_euler/problem_15/sol1.py +++ b/project_euler/problem_15/sol1.py @@ -1,20 +1,57 @@ -from __future__ import print_function +""" +Starting in the top left corner of a 2×2 grid, and only being able to move to +the right and down, there are exactly 6 routes to the bottom right corner. +How many such routes are there through a 20×20 grid? +""" from math import factorial + def lattice_paths(n): - n = 2*n #middle entry of odd rows starting at row 3 is the solution for n = 1, 2, 3,... - k = n/2 - - return factorial(n)/(factorial(k)*factorial(n-k)) - -if __name__ == '__main__': - import sys - - if len(sys.argv) == 1: - print(lattice_paths(20)) - else: - try: - n = int(sys.argv[1]) - print(lattice_paths(n)) - except ValueError: - print('Invalid entry - please enter a number.') + """ + Returns the number of paths possible in a n x n grid starting at top left + corner going to bottom right corner and being able to move right and down + only. + +bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 50 +1.008913445455642e+29 +bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 25 +126410606437752.0 +bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 23 +8233430727600.0 +bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 15 +155117520.0 +bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 1 +2.0 + + >>> lattice_paths(25) + 126410606437752 + >>> lattice_paths(23) + 8233430727600 + >>> lattice_paths(20) + 137846528820 + >>> lattice_paths(15) + 155117520 + >>> lattice_paths(1) + 2 + + """ + n = ( + 2 * n + ) # middle entry of odd rows starting at row 3 is the solution for n = 1, + # 2, 3,... + k = n / 2 + + return int(factorial(n) / (factorial(k) * factorial(n - k))) + + +if __name__ == "__main__": + import sys + + if len(sys.argv) == 1: + print(lattice_paths(20)) + else: + try: + n = int(sys.argv[1]) + print(lattice_paths(n)) + except ValueError: + print("Invalid entry - please enter a number.") diff --git a/project_euler/problem_16/sol1.py b/project_euler/problem_16/sol1.py index 05c7916bd10a..67c50ac87876 100644 --- a/project_euler/problem_16/sol1.py +++ b/project_euler/problem_16/sol1.py @@ -1,15 +1,34 @@ -power = int(input("Enter the power of 2: ")) -num = 2**power +""" +2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. -string_num = str(num) +What is the sum of the digits of the number 2^1000? +""" -list_num = list(string_num) -sum_of_num = 0 +def solution(power): + """Returns the sum of the digits of the number 2^power. + >>> solution(1000) + 1366 + >>> solution(50) + 76 + >>> solution(20) + 31 + >>> solution(15) + 26 + """ + num = 2 ** power + string_num = str(num) + list_num = list(string_num) + sum_of_num = 0 -print("2 ^",power,"=",num) + for i in list_num: + sum_of_num += int(i) -for i in list_num: - sum_of_num += int(i) + return sum_of_num -print("Sum of the digits are:",sum_of_num) + +if __name__ == "__main__": + power = int(input("Enter the power of 2: ").strip()) + print("2 ^ ", power, " = ", 2 ** power) + result = solution(power) + print("Sum of the digits is: ", result) diff --git a/project_euler/problem_16/sol2.py b/project_euler/problem_16/sol2.py index cce3d2354bb1..88672e9a9e54 100644 --- a/project_euler/problem_16/sol2.py +++ b/project_euler/problem_16/sol2.py @@ -1,6 +1,28 @@ -from __future__ import print_function -n = 2**1000 -r = 0 -while n: - r, n = r + n % 10, n // 10 -print(r) +""" +2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. + +What is the sum of the digits of the number 2^1000? +""" + + +def solution(power): + """Returns the sum of the digits of the number 2^power. + + >>> solution(1000) + 1366 + >>> solution(50) + 76 + >>> solution(20) + 31 + >>> solution(15) + 26 + """ + n = 2 ** power + r = 0 + while n: + r, n = r + n % 10, n // 10 + return r + + +if __name__ == "__main__": + print(solution(int(str(input()).strip()))) diff --git a/project_euler/problem_17/sol1.py b/project_euler/problem_17/sol1.py index 8dd6f1af2093..d585d81a0825 100644 --- a/project_euler/problem_17/sol1.py +++ b/project_euler/problem_17/sol1.py @@ -1,35 +1,63 @@ -from __future__ import print_function -''' +""" Number letter counts Problem 17 -If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. - -If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? - - -NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) -contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage. -''' - -ones_counts = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8] #number of letters in zero, one, two, ..., nineteen (0 for zero since it's never said aloud) -tens_counts = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6] #number of letters in twenty, thirty, ..., ninety (0 for numbers less than 20 due to inconsistency in teens) - -count = 0 - -for i in range(1, 1001): - if i < 1000: - if i >= 100: - count += ones_counts[i/100] + 7 #add number of letters for "n hundred" - - if i%100 != 0: - count += 3 #add number of letters for "and" if number is not multiple of 100 - - if 0 < i%100 < 20: - count += ones_counts[i%100] #add number of letters for one, two, three, ..., nineteen (could be combined with below if not for inconsistency in teens) - else: - count += ones_counts[i%10] + tens_counts[(i%100-i%10)/10] #add number of letters for twenty, twenty one, ..., ninety nine - else: - count += ones_counts[i/1000] + 8 - -print(count) +If the numbers 1 to 5 are written out in words: one, two, three, four, five, +then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. + +If all the numbers from 1 to 1000 (one thousand) inclusive were written out in +words, how many letters would be used? + + +NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and +forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 +letters. The use of "and" when writing out numbers is in compliance withBritish +usage. +""" + + +def solution(n): + """Returns the number of letters used to write all numbers from 1 to n. + where n is lower or equals to 1000. + >>> solution(1000) + 21124 + >>> solution(5) + 19 + """ + # number of letters in zero, one, two, ..., nineteen (0 for zero since it's + # never said aloud) + ones_counts = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8] + # number of letters in twenty, thirty, ..., ninety (0 for numbers less than + # 20 due to inconsistency in teens) + tens_counts = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6] + + count = 0 + + for i in range(1, n + 1): + if i < 1000: + if i >= 100: + # add number of letters for "n hundred" + count += ones_counts[i // 100] + 7 + + if i % 100 != 0: + # add number of letters for "and" if number is not multiple + # of 100 + count += 3 + + if 0 < i % 100 < 20: + # add number of letters for one, two, three, ..., nineteen + # (could be combined with below if not for inconsistency in + # teens) + count += ones_counts[i % 100] + else: + # add number of letters for twenty, twenty one, ..., ninety + # nine + count += ones_counts[i % 10] + count += tens_counts[(i % 100 - i % 10) // 10] + else: + count += ones_counts[i // 1000] + 8 + return count + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_19/sol1.py b/project_euler/problem_19/sol1.py index 13e520ca76e4..6e4e29ec19c6 100644 --- a/project_euler/problem_19/sol1.py +++ b/project_euler/problem_19/sol1.py @@ -1,9 +1,9 @@ -from __future__ import print_function -''' +""" Counting Sundays Problem 19 -You are given the following information, but you may prefer to do some research for yourself. +You are given the following information, but you may prefer to do some research +for yourself. 1 Jan 1900 was a Monday. Thirty days has September, @@ -13,39 +13,52 @@ 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. +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)? -''' +How many Sundays fell on the first of the month during the twentieth century +(1 Jan 1901 to 31 Dec 2000)? +""" -days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] -day = 6 -month = 1 -year = 1901 +def solution(): + """Returns the number of mondays that fall on the first of the month during + the twentieth century (1 Jan 1901 to 31 Dec 2000)? -sundays = 0 + >>> solution() + 171 + """ + days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] -while year < 2001: - day += 7 + day = 6 + month = 1 + year = 1901 - if (year%4 == 0 and not year%100 == 0) or (year%400 == 0): - if day > days_per_month[month-1] and month != 2: - month += 1 - day = day-days_per_month[month-2] - elif day > 29 and month == 2: - month += 1 - day = day-29 - else: - if day > days_per_month[month-1]: - month += 1 - day = day-days_per_month[month-2] - - if month > 12: - year += 1 - month = 1 + sundays = 0 - if year < 2001 and day == 1: - sundays += 1 + while year < 2001: + day += 7 -print(sundays) + if (year % 4 == 0 and not year % 100 == 0) or (year % 400 == 0): + if day > days_per_month[month - 1] and month != 2: + month += 1 + day = day - days_per_month[month - 2] + elif day > 29 and month == 2: + month += 1 + day = day - 29 + else: + if day > days_per_month[month - 1]: + month += 1 + day = day - days_per_month[month - 2] + + if month > 12: + year += 1 + month = 1 + + if year < 2001 and day == 1: + sundays += 1 + return sundays + + +if __name__ == "__main__": + print(solution(171)) diff --git a/project_euler/problem_20/sol1.py b/project_euler/problem_20/sol1.py index 73e41d5cc8fa..13b3c987f046 100644 --- a/project_euler/problem_20/sol1.py +++ b/project_euler/problem_20/sol1.py @@ -1,27 +1,51 @@ -# Finding the factorial. +""" +n! means n × (n − 1) × ... × 3 × 2 × 1 + +For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, +and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. + +Find the sum of the digits in the number 100! +""" + + def factorial(n): fact = 1 - for i in range(1,n+1): + for i in range(1, n + 1): fact *= i return fact -# Spliting the digits and adding it. + def split_and_add(number): + """Split number digits and add them.""" sum_of_digits = 0 - while(number>0): + while number > 0: last_digit = number % 10 sum_of_digits += last_digit - number = int(number/10) # Removing the last_digit from the given number. + number = number // 10 # Removing the last_digit from the given number return sum_of_digits -# Taking the user input. -number = int(input("Enter the Number: ")) -# Assigning the factorial from the factorial function. -factorial = factorial(number) +def solution(n): + """Returns the sum of the digits in the number 100! + >>> solution(100) + 648 + >>> solution(50) + 216 + >>> solution(10) + 27 + >>> solution(5) + 3 + >>> solution(3) + 6 + >>> solution(2) + 2 + >>> solution(1) + 1 + """ + f = factorial(n) + result = split_and_add(f) + return result -# Spliting and adding the factorial into answer. -answer = split_and_add(factorial) -# Printing the answer. -print(answer) +if __name__ == "__main__": + print(solution(int(input("Enter the Number: ").strip()))) diff --git a/project_euler/problem_20/sol2.py b/project_euler/problem_20/sol2.py index bca9af9cb9ef..14e591795292 100644 --- a/project_euler/problem_20/sol2.py +++ b/project_euler/problem_20/sol2.py @@ -1,5 +1,33 @@ +""" +n! means n × (n − 1) × ... × 3 × 2 × 1 + +For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, +and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. + +Find the sum of the digits in the number 100! +""" from math import factorial -def main(): - print(sum([int(x) for x in str(factorial(100))])) -if __name__ == '__main__': - main() \ No newline at end of file + + +def solution(n): + """Returns the sum of the digits in the number 100! + >>> solution(100) + 648 + >>> solution(50) + 216 + >>> solution(10) + 27 + >>> solution(5) + 3 + >>> solution(3) + 6 + >>> solution(2) + 2 + >>> solution(1) + 1 + """ + return sum([int(x) for x in str(factorial(n))]) + + +if __name__ == "__main__": + print(solution(int(input("Enter the Number: ").strip()))) diff --git a/project_euler/problem_21/sol1.py b/project_euler/problem_21/sol1.py index da29a5c7b631..9cf2a64cf2a9 100644 --- a/project_euler/problem_21/sol1.py +++ b/project_euler/problem_21/sol1.py @@ -1,30 +1,61 @@ -#-.- coding: latin-1 -.- -from __future__ import print_function +# -.- coding: latin-1 -.- from math import sqrt -''' + +""" Amicable Numbers Problem 21 -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. +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. +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. -''' +""" try: - xrange #Python 2 + xrange # Python 2 except NameError: - xrange = range #Python 3 + xrange = range # Python 3 + def sum_of_divisors(n): - total = 0 - for i in xrange(1, int(sqrt(n)+1)): - if n%i == 0 and i != sqrt(n): - total += i + n//i - elif i == sqrt(n): - total += i - return total-n - -total = [i for i in range(1,10000) if sum_of_divisors(sum_of_divisors(i)) == i and sum_of_divisors(i) != i] -print(sum(total)) + total = 0 + for i in xrange(1, int(sqrt(n) + 1)): + if n % i == 0 and i != sqrt(n): + total += i + n // i + elif i == sqrt(n): + total += i + return total - n + + +def solution(n): + """Returns the sum of all the amicable numbers under n. + + >>> solution(10000) + 31626 + >>> solution(5000) + 8442 + >>> solution(1000) + 504 + >>> solution(100) + 0 + >>> solution(50) + 0 + """ + total = sum( + [ + i + for i in range(1, n) + if sum_of_divisors(sum_of_divisors(i)) == i + and sum_of_divisors(i) != i + ] + ) + return total + + +if __name__ == "__main__": + print(solution(int(str(input()).strip()))) diff --git a/project_euler/problem_22/sol1.py b/project_euler/problem_22/sol1.py index 7754306583dc..c27dd12c9681 100644 --- a/project_euler/problem_22/sol1.py +++ b/project_euler/problem_22/sol1.py @@ -1,37 +1,49 @@ # -*- coding: latin-1 -*- -from __future__ import print_function -''' +""" Name scores Problem 22 -Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it -into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list -to obtain a name score. +Using names.txt (right click and 'Save Link/Target As...'), a 46K text file +containing over five-thousand first names, begin by sorting it into +alphabetical order. Then working out the alphabetical value for each name, +multiply this value by its alphabetical position in the list to obtain a name +score. -For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. -So, COLIN would obtain a score of 938 × 53 = 49714. +For example, when the list is sorted into alphabetical order, COLIN, which is +worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would +obtain a score of 938 × 53 = 49714. What is the total of all the name scores in the file? -''' +""" try: - xrange #Python 2 + xrange # Python 2 except NameError: - xrange = range #Python 3 + xrange = range # Python 3 -with open('p022_names.txt') as file: - names = str(file.readlines()[0]) - names = names.replace('"', '').split(',') -names.sort() +def solution(): + """Returns the total of all the name scores in the file. -name_score = 0 -total_score = 0 + >>> solution() + 871198282 + """ + with open("p022_names.txt") as file: + names = str(file.readlines()[0]) + names = names.replace('"', "").split(",") -for i, name in enumerate(names): - for letter in name: - name_score += ord(letter) - 64 + names.sort() - total_score += (i+1)*name_score - name_score = 0 + name_score = 0 + total_score = 0 -print(total_score) \ No newline at end of file + for i, name in enumerate(names): + for letter in name: + name_score += ord(letter) - 64 + + total_score += (i + 1) * name_score + name_score = 0 + return total_score + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_22/sol2.py b/project_euler/problem_22/sol2.py index d7f9abf09d49..bf55ee85fb0a 100644 --- a/project_euler/problem_22/sol2.py +++ b/project_euler/problem_22/sol2.py @@ -1,533 +1,42 @@ -def main(): - name = [ - "MARY", "PATRICIA", "LINDA", "BARBARA", "ELIZABETH", "JENNIFER", "MARIA", "SUSAN", "MARGARET", "DOROTHY", - "LISA", "NANCY", "KAREN", "BETTY", "HELEN", "SANDRA", "DONNA", "CAROL", "RUTH", "SHARON", - "MICHELLE", "LAURA", "SARAH", "KIMBERLY", "DEBORAH", "JESSICA", "SHIRLEY", "CYNTHIA", "ANGELA", "MELISSA", - "BRENDA", "AMY", "ANNA", "REBECCA", "VIRGINIA", "KATHLEEN", "PAMELA", "MARTHA", "DEBRA", "AMANDA", - "STEPHANIE", "CAROLYN", "CHRISTINE", "MARIE", "JANET", "CATHERINE", "FRANCES", "ANN", "JOYCE", "DIANE", - "ALICE", "JULIE", "HEATHER", "TERESA", "DORIS", "GLORIA", "EVELYN", "JEAN", "CHERYL", "MILDRED", - "KATHERINE", "JOAN", "ASHLEY", "JUDITH", "ROSE", "JANICE", "KELLY", "NICOLE", "JUDY", "CHRISTINA", - "KATHY", "THERESA", "BEVERLY", "DENISE", "TAMMY", "IRENE", "JANE", "LORI", "RACHEL", "MARILYN", - "ANDREA", "KATHRYN", "LOUISE", "SARA", "ANNE", "JACQUELINE", "WANDA", "BONNIE", "JULIA", "RUBY", - "LOIS", "TINA", "PHYLLIS", "NORMA", "PAULA", "DIANA", "ANNIE", "LILLIAN", "EMILY", "ROBIN", - "PEGGY", "CRYSTAL", "GLADYS", "RITA", "DAWN", "CONNIE", "FLORENCE", "TRACY", "EDNA", "TIFFANY", - "CARMEN", "ROSA", "CINDY", "GRACE", "WENDY", "VICTORIA", "EDITH", "KIM", "SHERRY", "SYLVIA", - "JOSEPHINE", "THELMA", "SHANNON", "SHEILA", "ETHEL", "ELLEN", "ELAINE", "MARJORIE", "CARRIE", "CHARLOTTE", - "MONICA", "ESTHER", "PAULINE", "EMMA", "JUANITA", "ANITA", "RHONDA", "HAZEL", "AMBER", "EVA", - "DEBBIE", "APRIL", "LESLIE", "CLARA", "LUCILLE", "JAMIE", "JOANNE", "ELEANOR", "VALERIE", "DANIELLE", - "MEGAN", "ALICIA", "SUZANNE", "MICHELE", "GAIL", "BERTHA", "DARLENE", "VERONICA", "JILL", "ERIN", - "GERALDINE", "LAUREN", "CATHY", "JOANN", "LORRAINE", "LYNN", "SALLY", "REGINA", "ERICA", "BEATRICE", - "DOLORES", "BERNICE", "AUDREY", "YVONNE", "ANNETTE", "JUNE", "SAMANTHA", "MARION", "DANA", "STACY", - "ANA", "RENEE", "IDA", "VIVIAN", "ROBERTA", "HOLLY", "BRITTANY", "MELANIE", "LORETTA", "YOLANDA", - "JEANETTE", "LAURIE", "KATIE", "KRISTEN", "VANESSA", "ALMA", "SUE", "ELSIE", "BETH", "JEANNE", - "VICKI", "CARLA", "TARA", "ROSEMARY", "EILEEN", "TERRI", "GERTRUDE", "LUCY", "TONYA", "ELLA", - "STACEY", "WILMA", "GINA", "KRISTIN", "JESSIE", "NATALIE", "AGNES", "VERA", "WILLIE", "CHARLENE", - "BESSIE", "DELORES", "MELINDA", "PEARL", "ARLENE", "MAUREEN", "COLLEEN", "ALLISON", "TAMARA", "JOY", - "GEORGIA", "CONSTANCE", "LILLIE", "CLAUDIA", "JACKIE", "MARCIA", "TANYA", "NELLIE", "MINNIE", "MARLENE", - "HEIDI", "GLENDA", "LYDIA", "VIOLA", "COURTNEY", "MARIAN", "STELLA", "CAROLINE", "DORA", "JO", - "VICKIE", "MATTIE", "TERRY", "MAXINE", "IRMA", "MABEL", "MARSHA", "MYRTLE", "LENA", "CHRISTY", - "DEANNA", "PATSY", "HILDA", "GWENDOLYN", "JENNIE", "NORA", "MARGIE", "NINA", "CASSANDRA", "LEAH", - "PENNY", "KAY", "PRISCILLA", "NAOMI", "CAROLE", "BRANDY", "OLGA", "BILLIE", "DIANNE", "TRACEY", - "LEONA", "JENNY", "FELICIA", "SONIA", "MIRIAM", "VELMA", "BECKY", "BOBBIE", "VIOLET", "KRISTINA", - "TONI", "MISTY", "MAE", "SHELLY", "DAISY", "RAMONA", "SHERRI", "ERIKA", "KATRINA", "CLAIRE", - "LINDSEY", "LINDSAY", "GENEVA", "GUADALUPE", "BELINDA", "MARGARITA", "SHERYL", "CORA", "FAYE", "ADA", - "NATASHA", "SABRINA", "ISABEL", "MARGUERITE", "HATTIE", "HARRIET", "MOLLY", "CECILIA", "KRISTI", "BRANDI", - "BLANCHE", "SANDY", "ROSIE", "JOANNA", "IRIS", "EUNICE", "ANGIE", "INEZ", "LYNDA", "MADELINE", - "AMELIA", "ALBERTA", "GENEVIEVE", "MONIQUE", "JODI", "JANIE", "MAGGIE", "KAYLA", "SONYA", "JAN", - "LEE", "KRISTINE", "CANDACE", "FANNIE", "MARYANN", "OPAL", "ALISON", "YVETTE", "MELODY", "LUZ", - "SUSIE", "OLIVIA", "FLORA", "SHELLEY", "KRISTY", "MAMIE", "LULA", "LOLA", "VERNA", "BEULAH", - "ANTOINETTE", "CANDICE", "JUANA", "JEANNETTE", "PAM", "KELLI", "HANNAH", "WHITNEY", "BRIDGET", "KARLA", - "CELIA", "LATOYA", "PATTY", "SHELIA", "GAYLE", "DELLA", "VICKY", "LYNNE", "SHERI", "MARIANNE", - "KARA", "JACQUELYN", "ERMA", "BLANCA", "MYRA", "LETICIA", "PAT", "KRISTA", "ROXANNE", "ANGELICA", - "JOHNNIE", "ROBYN", "FRANCIS", "ADRIENNE", "ROSALIE", "ALEXANDRA", "BROOKE", "BETHANY", "SADIE", "BERNADETTE", - "TRACI", "JODY", "KENDRA", "JASMINE", "NICHOLE", "RACHAEL", "CHELSEA", "MABLE", "ERNESTINE", "MURIEL", - "MARCELLA", "ELENA", "KRYSTAL", "ANGELINA", "NADINE", "KARI", "ESTELLE", "DIANNA", "PAULETTE", "LORA", - "MONA", "DOREEN", "ROSEMARIE", "ANGEL", "DESIREE", "ANTONIA", "HOPE", "GINGER", "JANIS", "BETSY", - "CHRISTIE", "FREDA", "MERCEDES", "MEREDITH", "LYNETTE", "TERI", "CRISTINA", "EULA", "LEIGH", "MEGHAN", - "SOPHIA", "ELOISE", "ROCHELLE", "GRETCHEN", "CECELIA", "RAQUEL", "HENRIETTA", "ALYSSA", "JANA", "KELLEY", - "GWEN", "KERRY", "JENNA", "TRICIA", "LAVERNE", "OLIVE", "ALEXIS", "TASHA", "SILVIA", "ELVIRA", - "CASEY", "DELIA", "SOPHIE", "KATE", "PATTI", "LORENA", "KELLIE", "SONJA", "LILA", "LANA", - "DARLA", "MAY", "MINDY", "ESSIE", "MANDY", "LORENE", "ELSA", "JOSEFINA", "JEANNIE", "MIRANDA", - "DIXIE", "LUCIA", "MARTA", "FAITH", "LELA", "JOHANNA", "SHARI", "CAMILLE", "TAMI", "SHAWNA", - "ELISA", "EBONY", "MELBA", "ORA", "NETTIE", "TABITHA", "OLLIE", "JAIME", "WINIFRED", "KRISTIE", - "MARINA", "ALISHA", "AIMEE", "RENA", "MYRNA", "MARLA", "TAMMIE", "LATASHA", "BONITA", "PATRICE", - "RONDA", "SHERRIE", "ADDIE", "FRANCINE", "DELORIS", "STACIE", "ADRIANA", "CHERI", "SHELBY", "ABIGAIL", - "CELESTE", "JEWEL", "CARA", "ADELE", "REBEKAH", "LUCINDA", "DORTHY", "CHRIS", "EFFIE", "TRINA", - "REBA", "SHAWN", "SALLIE", "AURORA", "LENORA", "ETTA", "LOTTIE", "KERRI", "TRISHA", "NIKKI", - "ESTELLA", "FRANCISCA", "JOSIE", "TRACIE", "MARISSA", "KARIN", "BRITTNEY", "JANELLE", "LOURDES", "LAUREL", - "HELENE", "FERN", "ELVA", "CORINNE", "KELSEY", "INA", "BETTIE", "ELISABETH", "AIDA", "CAITLIN", - "INGRID", "IVA", "EUGENIA", "CHRISTA", "GOLDIE", "CASSIE", "MAUDE", "JENIFER", "THERESE", "FRANKIE", - "DENA", "LORNA", "JANETTE", "LATONYA", "CANDY", "MORGAN", "CONSUELO", "TAMIKA", "ROSETTA", "DEBORA", - "CHERIE", "POLLY", "DINA", "JEWELL", "FAY", "JILLIAN", "DOROTHEA", "NELL", "TRUDY", "ESPERANZA", - "PATRICA", "KIMBERLEY", "SHANNA", "HELENA", "CAROLINA", "CLEO", "STEFANIE", "ROSARIO", "OLA", "JANINE", - "MOLLIE", "LUPE", "ALISA", "LOU", "MARIBEL", "SUSANNE", "BETTE", "SUSANA", "ELISE", "CECILE", - "ISABELLE", "LESLEY", "JOCELYN", "PAIGE", "JONI", "RACHELLE", "LEOLA", "DAPHNE", "ALTA", "ESTER", - "PETRA", "GRACIELA", "IMOGENE", "JOLENE", "KEISHA", "LACEY", "GLENNA", "GABRIELA", "KERI", "URSULA", - "LIZZIE", "KIRSTEN", "SHANA", "ADELINE", "MAYRA", "JAYNE", "JACLYN", "GRACIE", "SONDRA", "CARMELA", - "MARISA", "ROSALIND", "CHARITY", "TONIA", "BEATRIZ", "MARISOL", "CLARICE", "JEANINE", "SHEENA", "ANGELINE", - "FRIEDA", "LILY", "ROBBIE", "SHAUNA", "MILLIE", "CLAUDETTE", "CATHLEEN", "ANGELIA", "GABRIELLE", "AUTUMN", - "KATHARINE", "SUMMER", "JODIE", "STACI", "LEA", "CHRISTI", "JIMMIE", "JUSTINE", "ELMA", "LUELLA", - "MARGRET", "DOMINIQUE", "SOCORRO", "RENE", "MARTINA", "MARGO", "MAVIS", "CALLIE", "BOBBI", "MARITZA", - "LUCILE", "LEANNE", "JEANNINE", "DEANA", "AILEEN", "LORIE", "LADONNA", "WILLA", "MANUELA", "GALE", - "SELMA", "DOLLY", "SYBIL", "ABBY", "LARA", "DALE", "IVY", "DEE", "WINNIE", "MARCY", - "LUISA", "JERI", "MAGDALENA", "OFELIA", "MEAGAN", "AUDRA", "MATILDA", "LEILA", "CORNELIA", "BIANCA", - "SIMONE", "BETTYE", "RANDI", "VIRGIE", "LATISHA", "BARBRA", "GEORGINA", "ELIZA", "LEANN", "BRIDGETTE", - "RHODA", "HALEY", "ADELA", "NOLA", "BERNADINE", "FLOSSIE", "ILA", "GRETA", "RUTHIE", "NELDA", - "MINERVA", "LILLY", "TERRIE", "LETHA", "HILARY", "ESTELA", "VALARIE", "BRIANNA", "ROSALYN", "EARLINE", - "CATALINA", "AVA", "MIA", "CLARISSA", "LIDIA", "CORRINE", "ALEXANDRIA", "CONCEPCION", "TIA", "SHARRON", - "RAE", "DONA", "ERICKA", "JAMI", "ELNORA", "CHANDRA", "LENORE", "NEVA", "MARYLOU", "MELISA", - "TABATHA", "SERENA", "AVIS", "ALLIE", "SOFIA", "JEANIE", "ODESSA", "NANNIE", "HARRIETT", "LORAINE", - "PENELOPE", "MILAGROS", "EMILIA", "BENITA", "ALLYSON", "ASHLEE", "TANIA", "TOMMIE", "ESMERALDA", "KARINA", - "EVE", "PEARLIE", "ZELMA", "MALINDA", "NOREEN", "TAMEKA", "SAUNDRA", "HILLARY", "AMIE", "ALTHEA", - "ROSALINDA", "JORDAN", "LILIA", "ALANA", "GAY", "CLARE", "ALEJANDRA", "ELINOR", "MICHAEL", "LORRIE", - "JERRI", "DARCY", "EARNESTINE", "CARMELLA", "TAYLOR", "NOEMI", "MARCIE", "LIZA", "ANNABELLE", "LOUISA", - "EARLENE", "MALLORY", "CARLENE", "NITA", "SELENA", "TANISHA", "KATY", "JULIANNE", "JOHN", "LAKISHA", - "EDWINA", "MARICELA", "MARGERY", "KENYA", "DOLLIE", "ROXIE", "ROSLYN", "KATHRINE", "NANETTE", "CHARMAINE", - "LAVONNE", "ILENE", "KRIS", "TAMMI", "SUZETTE", "CORINE", "KAYE", "JERRY", "MERLE", "CHRYSTAL", - "LINA", "DEANNE", "LILIAN", "JULIANA", "ALINE", "LUANN", "KASEY", "MARYANNE", "EVANGELINE", "COLETTE", - "MELVA", "LAWANDA", "YESENIA", "NADIA", "MADGE", "KATHIE", "EDDIE", "OPHELIA", "VALERIA", "NONA", - "MITZI", "MARI", "GEORGETTE", "CLAUDINE", "FRAN", "ALISSA", "ROSEANN", "LAKEISHA", "SUSANNA", "REVA", - "DEIDRE", "CHASITY", "SHEREE", "CARLY", "JAMES", "ELVIA", "ALYCE", "DEIRDRE", "GENA", "BRIANA", - "ARACELI", "KATELYN", "ROSANNE", "WENDI", "TESSA", "BERTA", "MARVA", "IMELDA", "MARIETTA", "MARCI", - "LEONOR", "ARLINE", "SASHA", "MADELYN", "JANNA", "JULIETTE", "DEENA", "AURELIA", "JOSEFA", "AUGUSTA", - "LILIANA", "YOUNG", "CHRISTIAN", "LESSIE", "AMALIA", "SAVANNAH", "ANASTASIA", "VILMA", "NATALIA", "ROSELLA", - "LYNNETTE", "CORINA", "ALFREDA", "LEANNA", "CAREY", "AMPARO", "COLEEN", "TAMRA", "AISHA", "WILDA", - "KARYN", "CHERRY", "QUEEN", "MAURA", "MAI", "EVANGELINA", "ROSANNA", "HALLIE", "ERNA", "ENID", - "MARIANA", "LACY", "JULIET", "JACKLYN", "FREIDA", "MADELEINE", "MARA", "HESTER", "CATHRYN", "LELIA", - "CASANDRA", "BRIDGETT", "ANGELITA", "JANNIE", "DIONNE", "ANNMARIE", "KATINA", "BERYL", "PHOEBE", "MILLICENT", - "KATHERYN", "DIANN", "CARISSA", "MARYELLEN", "LIZ", "LAURI", "HELGA", "GILDA", "ADRIAN", "RHEA", - "MARQUITA", "HOLLIE", "TISHA", "TAMERA", "ANGELIQUE", "FRANCESCA", "BRITNEY", "KAITLIN", "LOLITA", "FLORINE", - "ROWENA", "REYNA", "TWILA", "FANNY", "JANELL", "INES", "CONCETTA", "BERTIE", "ALBA", "BRIGITTE", - "ALYSON", "VONDA", "PANSY", "ELBA", "NOELLE", "LETITIA", "KITTY", "DEANN", "BRANDIE", "LOUELLA", - "LETA", "FELECIA", "SHARLENE", "LESA", "BEVERLEY", "ROBERT", "ISABELLA", "HERMINIA", "TERRA", "CELINA", - "TORI", "OCTAVIA", "JADE", "DENICE", "GERMAINE", "SIERRA", "MICHELL", "CORTNEY", "NELLY", "DORETHA", - "SYDNEY", "DEIDRA", "MONIKA", "LASHONDA", "JUDI", "CHELSEY", "ANTIONETTE", "MARGOT", "BOBBY", "ADELAIDE", - "NAN", "LEEANN", "ELISHA", "DESSIE", "LIBBY", "KATHI", "GAYLA", "LATANYA", "MINA", "MELLISA", - "KIMBERLEE", "JASMIN", "RENAE", "ZELDA", "ELDA", "MA", "JUSTINA", "GUSSIE", "EMILIE", "CAMILLA", - "ABBIE", "ROCIO", "KAITLYN", "JESSE", "EDYTHE", "ASHLEIGH", "SELINA", "LAKESHA", "GERI", "ALLENE", - "PAMALA", "MICHAELA", "DAYNA", "CARYN", "ROSALIA", "SUN", "JACQULINE", "REBECA", "MARYBETH", "KRYSTLE", - "IOLA", "DOTTIE", "BENNIE", "BELLE", "AUBREY", "GRISELDA", "ERNESTINA", "ELIDA", "ADRIANNE", "DEMETRIA", - "DELMA", "CHONG", "JAQUELINE", "DESTINY", "ARLEEN", "VIRGINA", "RETHA", "FATIMA", "TILLIE", "ELEANORE", - "CARI", "TREVA", "BIRDIE", "WILHELMINA", "ROSALEE", "MAURINE", "LATRICE", "YONG", "JENA", "TARYN", - "ELIA", "DEBBY", "MAUDIE", "JEANNA", "DELILAH", "CATRINA", "SHONDA", "HORTENCIA", "THEODORA", "TERESITA", - "ROBBIN", "DANETTE", "MARYJANE", "FREDDIE", "DELPHINE", "BRIANNE", "NILDA", "DANNA", "CINDI", "BESS", - "IONA", "HANNA", "ARIEL", "WINONA", "VIDA", "ROSITA", "MARIANNA", "WILLIAM", "RACHEAL", "GUILLERMINA", - "ELOISA", "CELESTINE", "CAREN", "MALISSA", "LONA", "CHANTEL", "SHELLIE", "MARISELA", "LEORA", "AGATHA", - "SOLEDAD", "MIGDALIA", "IVETTE", "CHRISTEN", "ATHENA", "JANEL", "CHLOE", "VEDA", "PATTIE", "TESSIE", - "TERA", "MARILYNN", "LUCRETIA", "KARRIE", "DINAH", "DANIELA", "ALECIA", "ADELINA", "VERNICE", "SHIELA", - "PORTIA", "MERRY", "LASHAWN", "DEVON", "DARA", "TAWANA", "OMA", "VERDA", "CHRISTIN", "ALENE", - "ZELLA", "SANDI", "RAFAELA", "MAYA", "KIRA", "CANDIDA", "ALVINA", "SUZAN", "SHAYLA", "LYN", - "LETTIE", "ALVA", "SAMATHA", "ORALIA", "MATILDE", "MADONNA", "LARISSA", "VESTA", "RENITA", "INDIA", - "DELOIS", "SHANDA", "PHILLIS", "LORRI", "ERLINDA", "CRUZ", "CATHRINE", "BARB", "ZOE", "ISABELL", - "IONE", "GISELA", "CHARLIE", "VALENCIA", "ROXANNA", "MAYME", "KISHA", "ELLIE", "MELLISSA", "DORRIS", - "DALIA", "BELLA", "ANNETTA", "ZOILA", "RETA", "REINA", "LAURETTA", "KYLIE", "CHRISTAL", "PILAR", - "CHARLA", "ELISSA", "TIFFANI", "TANA", "PAULINA", "LEOTA", "BREANNA", "JAYME", "CARMEL", "VERNELL", - "TOMASA", "MANDI", "DOMINGA", "SANTA", "MELODIE", "LURA", "ALEXA", "TAMELA", "RYAN", "MIRNA", - "KERRIE", "VENUS", "NOEL", "FELICITA", "CRISTY", "CARMELITA", "BERNIECE", "ANNEMARIE", "TIARA", "ROSEANNE", - "MISSY", "CORI", "ROXANA", "PRICILLA", "KRISTAL", "JUNG", "ELYSE", "HAYDEE", "ALETHA", "BETTINA", - "MARGE", "GILLIAN", "FILOMENA", "CHARLES", "ZENAIDA", "HARRIETTE", "CARIDAD", "VADA", "UNA", "ARETHA", - "PEARLINE", "MARJORY", "MARCELA", "FLOR", "EVETTE", "ELOUISE", "ALINA", "TRINIDAD", "DAVID", "DAMARIS", - "CATHARINE", "CARROLL", "BELVA", "NAKIA", "MARLENA", "LUANNE", "LORINE", "KARON", "DORENE", "DANITA", - "BRENNA", "TATIANA", "SAMMIE", "LOUANN", "LOREN", "JULIANNA", "ANDRIA", "PHILOMENA", "LUCILA", "LEONORA", - "DOVIE", "ROMONA", "MIMI", "JACQUELIN", "GAYE", "TONJA", "MISTI", "JOE", "GENE", "CHASTITY", - "STACIA", "ROXANN", "MICAELA", "NIKITA", "MEI", "VELDA", "MARLYS", "JOHNNA", "AURA", "LAVERN", - "IVONNE", "HAYLEY", "NICKI", "MAJORIE", "HERLINDA", "GEORGE", "ALPHA", "YADIRA", "PERLA", "GREGORIA", - "DANIEL", "ANTONETTE", "SHELLI", "MOZELLE", "MARIAH", "JOELLE", "CORDELIA", "JOSETTE", "CHIQUITA", "TRISTA", - "LOUIS", "LAQUITA", "GEORGIANA", "CANDI", "SHANON", "LONNIE", "HILDEGARD", "CECIL", "VALENTINA", "STEPHANY", - "MAGDA", "KAROL", "GERRY", "GABRIELLA", "TIANA", "ROMA", "RICHELLE", "RAY", "PRINCESS", "OLETA", - "JACQUE", "IDELLA", "ALAINA", "SUZANNA", "JOVITA", "BLAIR", "TOSHA", "RAVEN", "NEREIDA", "MARLYN", - "KYLA", "JOSEPH", "DELFINA", "TENA", "STEPHENIE", "SABINA", "NATHALIE", "MARCELLE", "GERTIE", "DARLEEN", - "THEA", "SHARONDA", "SHANTEL", "BELEN", "VENESSA", "ROSALINA", "ONA", "GENOVEVA", "COREY", "CLEMENTINE", - "ROSALBA", "RENATE", "RENATA", "MI", "IVORY", "GEORGIANNA", "FLOY", "DORCAS", "ARIANA", "TYRA", - "THEDA", "MARIAM", "JULI", "JESICA", "DONNIE", "VIKKI", "VERLA", "ROSELYN", "MELVINA", "JANNETTE", - "GINNY", "DEBRAH", "CORRIE", "ASIA", "VIOLETA", "MYRTIS", "LATRICIA", "COLLETTE", "CHARLEEN", "ANISSA", - "VIVIANA", "TWYLA", "PRECIOUS", "NEDRA", "LATONIA", "LAN", "HELLEN", "FABIOLA", "ANNAMARIE", "ADELL", - "SHARYN", "CHANTAL", "NIKI", "MAUD", "LIZETTE", "LINDY", "KIA", "KESHA", "JEANA", "DANELLE", - "CHARLINE", "CHANEL", "CARROL", "VALORIE", "LIA", "DORTHA", "CRISTAL", "SUNNY", "LEONE", "LEILANI", - "GERRI", "DEBI", "ANDRA", "KESHIA", "IMA", "EULALIA", "EASTER", "DULCE", "NATIVIDAD", "LINNIE", - "KAMI", "GEORGIE", "CATINA", "BROOK", "ALDA", "WINNIFRED", "SHARLA", "RUTHANN", "MEAGHAN", "MAGDALENE", - "LISSETTE", "ADELAIDA", "VENITA", "TRENA", "SHIRLENE", "SHAMEKA", "ELIZEBETH", "DIAN", "SHANTA", "MICKEY", - "LATOSHA", "CARLOTTA", "WINDY", "SOON", "ROSINA", "MARIANN", "LEISA", "JONNIE", "DAWNA", "CATHIE", - "BILLY", "ASTRID", "SIDNEY", "LAUREEN", "JANEEN", "HOLLI", "FAWN", "VICKEY", "TERESSA", "SHANTE", - "RUBYE", "MARCELINA", "CHANDA", "CARY", "TERESE", "SCARLETT", "MARTY", "MARNIE", "LULU", "LISETTE", - "JENIFFER", "ELENOR", "DORINDA", "DONITA", "CARMAN", "BERNITA", "ALTAGRACIA", "ALETA", "ADRIANNA", "ZORAIDA", - "RONNIE", "NICOLA", "LYNDSEY", "KENDALL", "JANINA", "CHRISSY", "AMI", "STARLA", "PHYLIS", "PHUONG", - "KYRA", "CHARISSE", "BLANCH", "SANJUANITA", "RONA", "NANCI", "MARILEE", "MARANDA", "CORY", "BRIGETTE", - "SANJUANA", "MARITA", "KASSANDRA", "JOYCELYN", "IRA", "FELIPA", "CHELSIE", "BONNY", "MIREYA", "LORENZA", - "KYONG", "ILEANA", "CANDELARIA", "TONY", "TOBY", "SHERIE", "OK", "MARK", "LUCIE", "LEATRICE", - "LAKESHIA", "GERDA", "EDIE", "BAMBI", "MARYLIN", "LAVON", "HORTENSE", "GARNET", "EVIE", "TRESSA", - "SHAYNA", "LAVINA", "KYUNG", "JEANETTA", "SHERRILL", "SHARA", "PHYLISS", "MITTIE", "ANABEL", "ALESIA", - "THUY", "TAWANDA", "RICHARD", "JOANIE", "TIFFANIE", "LASHANDA", "KARISSA", "ENRIQUETA", "DARIA", "DANIELLA", - "CORINNA", "ALANNA", "ABBEY", "ROXANE", "ROSEANNA", "MAGNOLIA", "LIDA", "KYLE", "JOELLEN", "ERA", - "CORAL", "CARLEEN", "TRESA", "PEGGIE", "NOVELLA", "NILA", "MAYBELLE", "JENELLE", "CARINA", "NOVA", - "MELINA", "MARQUERITE", "MARGARETTE", "JOSEPHINA", "EVONNE", "DEVIN", "CINTHIA", "ALBINA", "TOYA", "TAWNYA", - "SHERITA", "SANTOS", "MYRIAM", "LIZABETH", "LISE", "KEELY", "JENNI", "GISELLE", "CHERYLE", "ARDITH", - "ARDIS", "ALESHA", "ADRIANE", "SHAINA", "LINNEA", "KAROLYN", "HONG", "FLORIDA", "FELISHA", "DORI", - "DARCI", "ARTIE", "ARMIDA", "ZOLA", "XIOMARA", "VERGIE", "SHAMIKA", "NENA", "NANNETTE", "MAXIE", - "LOVIE", "JEANE", "JAIMIE", "INGE", "FARRAH", "ELAINA", "CAITLYN", "STARR", "FELICITAS", "CHERLY", - "CARYL", "YOLONDA", "YASMIN", "TEENA", "PRUDENCE", "PENNIE", "NYDIA", "MACKENZIE", "ORPHA", "MARVEL", - "LIZBETH", "LAURETTE", "JERRIE", "HERMELINDA", "CAROLEE", "TIERRA", "MIRIAN", "META", "MELONY", "KORI", - "JENNETTE", "JAMILA", "ENA", "ANH", "YOSHIKO", "SUSANNAH", "SALINA", "RHIANNON", "JOLEEN", "CRISTINE", - "ASHTON", "ARACELY", "TOMEKA", "SHALONDA", "MARTI", "LACIE", "KALA", "JADA", "ILSE", "HAILEY", - "BRITTANI", "ZONA", "SYBLE", "SHERRYL", "RANDY", "NIDIA", "MARLO", "KANDICE", "KANDI", "DEB", - "DEAN", "AMERICA", "ALYCIA", "TOMMY", "RONNA", "NORENE", "MERCY", "JOSE", "INGEBORG", "GIOVANNA", - "GEMMA", "CHRISTEL", "AUDRY", "ZORA", "VITA", "VAN", "TRISH", "STEPHAINE", "SHIRLEE", "SHANIKA", - "MELONIE", "MAZIE", "JAZMIN", "INGA", "HOA", "HETTIE", "GERALYN", "FONDA", "ESTRELLA", "ADELLA", - "SU", "SARITA", "RINA", "MILISSA", "MARIBETH", "GOLDA", "EVON", "ETHELYN", "ENEDINA", "CHERISE", - "CHANA", "VELVA", "TAWANNA", "SADE", "MIRTA", "LI", "KARIE", "JACINTA", "ELNA", "DAVINA", - "CIERRA", "ASHLIE", "ALBERTHA", "TANESHA", "STEPHANI", "NELLE", "MINDI", "LU", "LORINDA", "LARUE", - "FLORENE", "DEMETRA", "DEDRA", "CIARA", "CHANTELLE", "ASHLY", "SUZY", "ROSALVA", "NOELIA", "LYDA", - "LEATHA", "KRYSTYNA", "KRISTAN", "KARRI", "DARLINE", "DARCIE", "CINDA", "CHEYENNE", "CHERRIE", "AWILDA", - "ALMEDA", "ROLANDA", "LANETTE", "JERILYN", "GISELE", "EVALYN", "CYNDI", "CLETA", "CARIN", "ZINA", - "ZENA", "VELIA", "TANIKA", "PAUL", "CHARISSA", "THOMAS", "TALIA", "MARGARETE", "LAVONDA", "KAYLEE", - "KATHLENE", "JONNA", "IRENA", "ILONA", "IDALIA", "CANDIS", "CANDANCE", "BRANDEE", "ANITRA", "ALIDA", - "SIGRID", "NICOLETTE", "MARYJO", "LINETTE", "HEDWIG", "CHRISTIANA", "CASSIDY", "ALEXIA", "TRESSIE", "MODESTA", - "LUPITA", "LITA", "GLADIS", "EVELIA", "DAVIDA", "CHERRI", "CECILY", "ASHELY", "ANNABEL", "AGUSTINA", - "WANITA", "SHIRLY", "ROSAURA", "HULDA", "EUN", "BAILEY", "YETTA", "VERONA", "THOMASINA", "SIBYL", - "SHANNAN", "MECHELLE", "LUE", "LEANDRA", "LANI", "KYLEE", "KANDY", "JOLYNN", "FERNE", "EBONI", - "CORENE", "ALYSIA", "ZULA", "NADA", "MOIRA", "LYNDSAY", "LORRETTA", "JUAN", "JAMMIE", "HORTENSIA", - "GAYNELL", "CAMERON", "ADRIA", "VINA", "VICENTA", "TANGELA", "STEPHINE", "NORINE", "NELLA", "LIANA", - "LESLEE", "KIMBERELY", "ILIANA", "GLORY", "FELICA", "EMOGENE", "ELFRIEDE", "EDEN", "EARTHA", "CARMA", - "BEA", "OCIE", "MARRY", "LENNIE", "KIARA", "JACALYN", "CARLOTA", "ARIELLE", "YU", "STAR", - "OTILIA", "KIRSTIN", "KACEY", "JOHNETTA", "JOEY", "JOETTA", "JERALDINE", "JAUNITA", "ELANA", "DORTHEA", - "CAMI", "AMADA", "ADELIA", "VERNITA", "TAMAR", "SIOBHAN", "RENEA", "RASHIDA", "OUIDA", "ODELL", - "NILSA", "MERYL", "KRISTYN", "JULIETA", "DANICA", "BREANNE", "AUREA", "ANGLEA", "SHERRON", "ODETTE", - "MALIA", "LORELEI", "LIN", "LEESA", "KENNA", "KATHLYN", "FIONA", "CHARLETTE", "SUZIE", "SHANTELL", - "SABRA", "RACQUEL", "MYONG", "MIRA", "MARTINE", "LUCIENNE", "LAVADA", "JULIANN", "JOHNIE", "ELVERA", - "DELPHIA", "CLAIR", "CHRISTIANE", "CHAROLETTE", "CARRI", "AUGUSTINE", "ASHA", "ANGELLA", "PAOLA", "NINFA", - "LEDA", "LAI", "EDA", "SUNSHINE", "STEFANI", "SHANELL", "PALMA", "MACHELLE", "LISSA", "KECIA", - "KATHRYNE", "KARLENE", "JULISSA", "JETTIE", "JENNIFFER", "HUI", "CORRINA", "CHRISTOPHER", "CAROLANN", "ALENA", - "TESS", "ROSARIA", "MYRTICE", "MARYLEE", "LIANE", "KENYATTA", "JUDIE", "JANEY", "IN", "ELMIRA", - "ELDORA", "DENNA", "CRISTI", "CATHI", "ZAIDA", "VONNIE", "VIVA", "VERNIE", "ROSALINE", "MARIELA", - "LUCIANA", "LESLI", "KARAN", "FELICE", "DENEEN", "ADINA", "WYNONA", "TARSHA", "SHERON", "SHASTA", - "SHANITA", "SHANI", "SHANDRA", "RANDA", "PINKIE", "PARIS", "NELIDA", "MARILOU", "LYLA", "LAURENE", - "LACI", "JOI", "JANENE", "DOROTHA", "DANIELE", "DANI", "CAROLYNN", "CARLYN", "BERENICE", "AYESHA", - "ANNELIESE", "ALETHEA", "THERSA", "TAMIKO", "RUFINA", "OLIVA", "MOZELL", "MARYLYN", "MADISON", "KRISTIAN", - "KATHYRN", "KASANDRA", "KANDACE", "JANAE", "GABRIEL", "DOMENICA", "DEBBRA", "DANNIELLE", "CHUN", "BUFFY", - "BARBIE", "ARCELIA", "AJA", "ZENOBIA", "SHAREN", "SHAREE", "PATRICK", "PAGE", "MY", "LAVINIA", - "KUM", "KACIE", "JACKELINE", "HUONG", "FELISA", "EMELIA", "ELEANORA", "CYTHIA", "CRISTIN", "CLYDE", - "CLARIBEL", "CARON", "ANASTACIA", "ZULMA", "ZANDRA", "YOKO", "TENISHA", "SUSANN", "SHERILYN", "SHAY", - "SHAWANDA", "SABINE", "ROMANA", "MATHILDA", "LINSEY", "KEIKO", "JOANA", "ISELA", "GRETTA", "GEORGETTA", - "EUGENIE", "DUSTY", "DESIRAE", "DELORA", "CORAZON", "ANTONINA", "ANIKA", "WILLENE", "TRACEE", "TAMATHA", - "REGAN", "NICHELLE", "MICKIE", "MAEGAN", "LUANA", "LANITA", "KELSIE", "EDELMIRA", "BREE", "AFTON", - "TEODORA", "TAMIE", "SHENA", "MEG", "LINH", "KELI", "KACI", "DANYELLE", "BRITT", "ARLETTE", - "ALBERTINE", "ADELLE", "TIFFINY", "STORMY", "SIMONA", "NUMBERS", "NICOLASA", "NICHOL", "NIA", "NAKISHA", - "MEE", "MAIRA", "LOREEN", "KIZZY", "JOHNNY", "JAY", "FALLON", "CHRISTENE", "BOBBYE", "ANTHONY", - "YING", "VINCENZA", "TANJA", "RUBIE", "RONI", "QUEENIE", "MARGARETT", "KIMBERLI", "IRMGARD", "IDELL", - "HILMA", "EVELINA", "ESTA", "EMILEE", "DENNISE", "DANIA", "CARL", "CARIE", "ANTONIO", "WAI", - "SANG", "RISA", "RIKKI", "PARTICIA", "MUI", "MASAKO", "MARIO", "LUVENIA", "LOREE", "LONI", - "LIEN", "KEVIN", "GIGI", "FLORENCIA", "DORIAN", "DENITA", "DALLAS", "CHI", "BILLYE", "ALEXANDER", - "TOMIKA", "SHARITA", "RANA", "NIKOLE", "NEOMA", "MARGARITE", "MADALYN", "LUCINA", "LAILA", "KALI", - "JENETTE", "GABRIELE", "EVELYNE", "ELENORA", "CLEMENTINA", "ALEJANDRINA", "ZULEMA", "VIOLETTE", "VANNESSA", "THRESA", - "RETTA", "PIA", "PATIENCE", "NOELLA", "NICKIE", "JONELL", "DELTA", "CHUNG", "CHAYA", "CAMELIA", - "BETHEL", "ANYA", "ANDREW", "THANH", "SUZANN", "SPRING", "SHU", "MILA", "LILLA", "LAVERNA", - "KEESHA", "KATTIE", "GIA", "GEORGENE", "EVELINE", "ESTELL", "ELIZBETH", "VIVIENNE", "VALLIE", "TRUDIE", - "STEPHANE", "MICHEL", "MAGALY", "MADIE", "KENYETTA", "KARREN", "JANETTA", "HERMINE", "HARMONY", "DRUCILLA", - "DEBBI", "CELESTINA", "CANDIE", "BRITNI", "BECKIE", "AMINA", "ZITA", "YUN", "YOLANDE", "VIVIEN", - "VERNETTA", "TRUDI", "SOMMER", "PEARLE", "PATRINA", "OSSIE", "NICOLLE", "LOYCE", "LETTY", "LARISA", - "KATHARINA", "JOSELYN", "JONELLE", "JENELL", "IESHA", "HEIDE", "FLORINDA", "FLORENTINA", "FLO", "ELODIA", - "DORINE", "BRUNILDA", "BRIGID", "ASHLI", "ARDELLA", "TWANA", "THU", "TARAH", "SUNG", "SHEA", - "SHAVON", "SHANE", "SERINA", "RAYNA", "RAMONITA", "NGA", "MARGURITE", "LUCRECIA", "KOURTNEY", "KATI", - "JESUS", "JESENIA", "DIAMOND", "CRISTA", "AYANA", "ALICA", "ALIA", "VINNIE", "SUELLEN", "ROMELIA", - "RACHELL", "PIPER", "OLYMPIA", "MICHIKO", "KATHALEEN", "JOLIE", "JESSI", "JANESSA", "HANA", "HA", - "ELEASE", "CARLETTA", "BRITANY", "SHONA", "SALOME", "ROSAMOND", "REGENA", "RAINA", "NGOC", "NELIA", - "LOUVENIA", "LESIA", "LATRINA", "LATICIA", "LARHONDA", "JINA", "JACKI", "HOLLIS", "HOLLEY", "EMMY", - "DEEANN", "CORETTA", "ARNETTA", "VELVET", "THALIA", "SHANICE", "NETA", "MIKKI", "MICKI", "LONNA", - "LEANA", "LASHUNDA", "KILEY", "JOYE", "JACQULYN", "IGNACIA", "HYUN", "HIROKO", "HENRY", "HENRIETTE", - "ELAYNE", "DELINDA", "DARNELL", "DAHLIA", "COREEN", "CONSUELA", "CONCHITA", "CELINE", "BABETTE", "AYANNA", - "ANETTE", "ALBERTINA", "SKYE", "SHAWNEE", "SHANEKA", "QUIANA", "PAMELIA", "MIN", "MERRI", "MERLENE", - "MARGIT", "KIESHA", "KIERA", "KAYLENE", "JODEE", "JENISE", "ERLENE", "EMMIE", "ELSE", "DARYL", - "DALILA", "DAISEY", "CODY", "CASIE", "BELIA", "BABARA", "VERSIE", "VANESA", "SHELBA", "SHAWNDA", - "SAM", "NORMAN", "NIKIA", "NAOMA", "MARNA", "MARGERET", "MADALINE", "LAWANA", "KINDRA", "JUTTA", - "JAZMINE", "JANETT", "HANNELORE", "GLENDORA", "GERTRUD", "GARNETT", "FREEDA", "FREDERICA", "FLORANCE", "FLAVIA", - "DENNIS", "CARLINE", "BEVERLEE", "ANJANETTE", "VALDA", "TRINITY", "TAMALA", "STEVIE", "SHONNA", "SHA", - "SARINA", "ONEIDA", "MICAH", "MERILYN", "MARLEEN", "LURLINE", "LENNA", "KATHERIN", "JIN", "JENI", - "HAE", "GRACIA", "GLADY", "FARAH", "ERIC", "ENOLA", "EMA", "DOMINQUE", "DEVONA", "DELANA", - "CECILA", "CAPRICE", "ALYSHA", "ALI", "ALETHIA", "VENA", "THERESIA", "TAWNY", "SONG", "SHAKIRA", - "SAMARA", "SACHIKO", "RACHELE", "PAMELLA", "NICKY", "MARNI", "MARIEL", "MAREN", "MALISA", "LIGIA", - "LERA", "LATORIA", "LARAE", "KIMBER", "KATHERN", "KAREY", "JENNEFER", "JANETH", "HALINA", "FREDIA", - "DELISA", "DEBROAH", "CIERA", "CHIN", "ANGELIKA", "ANDREE", "ALTHA", "YEN", "VIVAN", "TERRESA", - "TANNA", "SUK", "SUDIE", "SOO", "SIGNE", "SALENA", "RONNI", "REBBECCA", "MYRTIE", "MCKENZIE", - "MALIKA", "MAIDA", "LOAN", "LEONARDA", "KAYLEIGH", "FRANCE", "ETHYL", "ELLYN", "DAYLE", "CAMMIE", - "BRITTNI", "BIRGIT", "AVELINA", "ASUNCION", "ARIANNA", "AKIKO", "VENICE", "TYESHA", "TONIE", "TIESHA", - "TAKISHA", "STEFFANIE", "SINDY", "SANTANA", "MEGHANN", "MANDA", "MACIE", "LADY", "KELLYE", "KELLEE", - "JOSLYN", "JASON", "INGER", "INDIRA", "GLINDA", "GLENNIS", "FERNANDA", "FAUSTINA", "ENEIDA", "ELICIA", - "DOT", "DIGNA", "DELL", "ARLETTA", "ANDRE", "WILLIA", "TAMMARA", "TABETHA", "SHERRELL", "SARI", - "REFUGIO", "REBBECA", "PAULETTA", "NIEVES", "NATOSHA", "NAKITA", "MAMMIE", "KENISHA", "KAZUKO", "KASSIE", - "GARY", "EARLEAN", "DAPHINE", "CORLISS", "CLOTILDE", "CAROLYNE", "BERNETTA", "AUGUSTINA", "AUDREA", "ANNIS", - "ANNABELL", "YAN", "TENNILLE", "TAMICA", "SELENE", "SEAN", "ROSANA", "REGENIA", "QIANA", "MARKITA", - "MACY", "LEEANNE", "LAURINE", "KYM", "JESSENIA", "JANITA", "GEORGINE", "GENIE", "EMIKO", "ELVIE", - "DEANDRA", "DAGMAR", "CORIE", "COLLEN", "CHERISH", "ROMAINE", "PORSHA", "PEARLENE", "MICHELINE", "MERNA", - "MARGORIE", "MARGARETTA", "LORE", "KENNETH", "JENINE", "HERMINA", "FREDERICKA", "ELKE", "DRUSILLA", "DORATHY", - "DIONE", "DESIRE", "CELENA", "BRIGIDA", "ANGELES", "ALLEGRA", "THEO", "TAMEKIA", "SYNTHIA", "STEPHEN", - "SOOK", "SLYVIA", "ROSANN", "REATHA", "RAYE", "MARQUETTA", "MARGART", "LING", "LAYLA", "KYMBERLY", - "KIANA", "KAYLEEN", "KATLYN", "KARMEN", "JOELLA", "IRINA", "EMELDA", "ELENI", "DETRA", "CLEMMIE", - "CHERYLL", "CHANTELL", "CATHEY", "ARNITA", "ARLA", "ANGLE", "ANGELIC", "ALYSE", "ZOFIA", "THOMASINE", - "TENNIE", "SON", "SHERLY", "SHERLEY", "SHARYL", "REMEDIOS", "PETRINA", "NICKOLE", "MYUNG", "MYRLE", - "MOZELLA", "LOUANNE", "LISHA", "LATIA", "LANE", "KRYSTA", "JULIENNE", "JOEL", "JEANENE", "JACQUALINE", - "ISAURA", "GWENDA", "EARLEEN", "DONALD", "CLEOPATRA", "CARLIE", "AUDIE", "ANTONIETTA", "ALISE", "ALEX", - "VERDELL", "VAL", "TYLER", "TOMOKO", "THAO", "TALISHA", "STEVEN", "SO", "SHEMIKA", "SHAUN", - "SCARLET", "SAVANNA", "SANTINA", "ROSIA", "RAEANN", "ODILIA", "NANA", "MINNA", "MAGAN", "LYNELLE", - "LE", "KARMA", "JOEANN", "IVANA", "INELL", "ILANA", "HYE", "HONEY", "HEE", "GUDRUN", - "FRANK", "DREAMA", "CRISSY", "CHANTE", "CARMELINA", "ARVILLA", "ARTHUR", "ANNAMAE", "ALVERA", "ALEIDA", - "AARON", "YEE", "YANIRA", "VANDA", "TIANNA", "TAM", "STEFANIA", "SHIRA", "PERRY", "NICOL", - "NANCIE", "MONSERRATE", "MINH", "MELYNDA", "MELANY", "MATTHEW", "LOVELLA", "LAURE", "KIRBY", "KACY", - "JACQUELYNN", "HYON", "GERTHA", "FRANCISCO", "ELIANA", "CHRISTENA", "CHRISTEEN", "CHARISE", "CATERINA", "CARLEY", - "CANDYCE", "ARLENA", "AMMIE", "YANG", "WILLETTE", "VANITA", "TUYET", "TINY", "SYREETA", "SILVA", - "SCOTT", "RONALD", "PENNEY", "NYLA", "MICHAL", "MAURICE", "MARYAM", "MARYA", "MAGEN", "LUDIE", - "LOMA", "LIVIA", "LANELL", "KIMBERLIE", "JULEE", "DONETTA", "DIEDRA", "DENISHA", "DEANE", "DAWNE", - "CLARINE", "CHERRYL", "BRONWYN", "BRANDON", "ALLA", "VALERY", "TONDA", "SUEANN", "SORAYA", "SHOSHANA", - "SHELA", "SHARLEEN", "SHANELLE", "NERISSA", "MICHEAL", "MERIDITH", "MELLIE", "MAYE", "MAPLE", "MAGARET", - "LUIS", "LILI", "LEONILA", "LEONIE", "LEEANNA", "LAVONIA", "LAVERA", "KRISTEL", "KATHEY", "KATHE", - "JUSTIN", "JULIAN", "JIMMY", "JANN", "ILDA", "HILDRED", "HILDEGARDE", "GENIA", "FUMIKO", "EVELIN", - "ERMELINDA", "ELLY", "DUNG", "DOLORIS", "DIONNA", "DANAE", "BERNEICE", "ANNICE", "ALIX", "VERENA", - "VERDIE", "TRISTAN", "SHAWNNA", "SHAWANA", "SHAUNNA", "ROZELLA", "RANDEE", "RANAE", "MILAGRO", "LYNELL", - "LUISE", "LOUIE", "LOIDA", "LISBETH", "KARLEEN", "JUNITA", "JONA", "ISIS", "HYACINTH", "HEDY", - "GWENN", "ETHELENE", "ERLINE", "EDWARD", "DONYA", "DOMONIQUE", "DELICIA", "DANNETTE", "CICELY", "BRANDA", - "BLYTHE", "BETHANN", "ASHLYN", "ANNALEE", "ALLINE", "YUKO", "VELLA", "TRANG", "TOWANDA", "TESHA", - "SHERLYN", "NARCISA", "MIGUELINA", "MERI", "MAYBELL", "MARLANA", "MARGUERITA", "MADLYN", "LUNA", "LORY", - "LORIANN", "LIBERTY", "LEONORE", "LEIGHANN", "LAURICE", "LATESHA", "LARONDA", "KATRICE", "KASIE", "KARL", - "KALEY", "JADWIGA", "GLENNIE", "GEARLDINE", "FRANCINA", "EPIFANIA", "DYAN", "DORIE", "DIEDRE", "DENESE", - "DEMETRICE", "DELENA", "DARBY", "CRISTIE", "CLEORA", "CATARINA", "CARISA", "BERNIE", "BARBERA", "ALMETA", - "TRULA", "TEREASA", "SOLANGE", "SHEILAH", "SHAVONNE", "SANORA", "ROCHELL", "MATHILDE", "MARGARETA", "MAIA", - "LYNSEY", "LAWANNA", "LAUNA", "KENA", "KEENA", "KATIA", "JAMEY", "GLYNDA", "GAYLENE", "ELVINA", - "ELANOR", "DANUTA", "DANIKA", "CRISTEN", "CORDIE", "COLETTA", "CLARITA", "CARMON", "BRYNN", "AZUCENA", - "AUNDREA", "ANGELE", "YI", "WALTER", "VERLIE", "VERLENE", "TAMESHA", "SILVANA", "SEBRINA", "SAMIRA", - "REDA", "RAYLENE", "PENNI", "PANDORA", "NORAH", "NOMA", "MIREILLE", "MELISSIA", "MARYALICE", "LARAINE", - "KIMBERY", "KARYL", "KARINE", "KAM", "JOLANDA", "JOHANA", "JESUSA", "JALEESA", "JAE", "JACQUELYNE", - "IRISH", "ILUMINADA", "HILARIA", "HANH", "GENNIE", "FRANCIE", "FLORETTA", "EXIE", "EDDA", "DREMA", - "DELPHA", "BEV", "BARBAR", "ASSUNTA", "ARDELL", "ANNALISA", "ALISIA", "YUKIKO", "YOLANDO", "WONDA", - "WEI", "WALTRAUD", "VETA", "TEQUILA", "TEMEKA", "TAMEIKA", "SHIRLEEN", "SHENITA", "PIEDAD", "OZELLA", - "MIRTHA", "MARILU", "KIMIKO", "JULIANE", "JENICE", "JEN", "JANAY", "JACQUILINE", "HILDE", "FE", - "FAE", "EVAN", "EUGENE", "ELOIS", "ECHO", "DEVORAH", "CHAU", "BRINDA", "BETSEY", "ARMINDA", - "ARACELIS", "APRYL", "ANNETT", "ALISHIA", "VEOLA", "USHA", "TOSHIKO", "THEOLA", "TASHIA", "TALITHA", - "SHERY", "RUDY", "RENETTA", "REIKO", "RASHEEDA", "OMEGA", "OBDULIA", "MIKA", "MELAINE", "MEGGAN", - "MARTIN", "MARLEN", "MARGET", "MARCELINE", "MANA", "MAGDALEN", "LIBRADA", "LEZLIE", "LEXIE", "LATASHIA", - "LASANDRA", "KELLE", "ISIDRA", "ISA", "INOCENCIA", "GWYN", "FRANCOISE", "ERMINIA", "ERINN", "DIMPLE", - "DEVORA", "CRISELDA", "ARMANDA", "ARIE", "ARIANE", "ANGELO", "ANGELENA", "ALLEN", "ALIZA", "ADRIENE", - "ADALINE", "XOCHITL", "TWANNA", "TRAN", "TOMIKO", "TAMISHA", "TAISHA", "SUSY", "SIU", "RUTHA", - "ROXY", "RHONA", "RAYMOND", "OTHA", "NORIKO", "NATASHIA", "MERRIE", "MELVIN", "MARINDA", "MARIKO", - "MARGERT", "LORIS", "LIZZETTE", "LEISHA", "KAILA", "KA", "JOANNIE", "JERRICA", "JENE", "JANNET", - "JANEE", "JACINDA", "HERTA", "ELENORE", "DORETTA", "DELAINE", "DANIELL", "CLAUDIE", "CHINA", "BRITTA", - "APOLONIA", "AMBERLY", "ALEASE", "YURI", "YUK", "WEN", "WANETA", "UTE", "TOMI", "SHARRI", - "SANDIE", "ROSELLE", "REYNALDA", "RAGUEL", "PHYLICIA", "PATRIA", "OLIMPIA", "ODELIA", "MITZIE", "MITCHELL", - "MISS", "MINDA", "MIGNON", "MICA", "MENDY", "MARIVEL", "MAILE", "LYNETTA", "LAVETTE", "LAURYN", - "LATRISHA", "LAKIESHA", "KIERSTEN", "KARY", "JOSPHINE", "JOLYN", "JETTA", "JANISE", "JACQUIE", "IVELISSE", - "GLYNIS", "GIANNA", "GAYNELLE", "EMERALD", "DEMETRIUS", "DANYELL", "DANILLE", "DACIA", "CORALEE", "CHER", - "CEOLA", "BRETT", "BELL", "ARIANNE", "ALESHIA", "YUNG", "WILLIEMAE", "TROY", "TRINH", "THORA", - "TAI", "SVETLANA", "SHERIKA", "SHEMEKA", "SHAUNDA", "ROSELINE", "RICKI", "MELDA", "MALLIE", "LAVONNA", - "LATINA", "LARRY", "LAQUANDA", "LALA", "LACHELLE", "KLARA", "KANDIS", "JOHNA", "JEANMARIE", "JAYE", - "HANG", "GRAYCE", "GERTUDE", "EMERITA", "EBONIE", "CLORINDA", "CHING", "CHERY", "CAROLA", "BREANN", - "BLOSSOM", "BERNARDINE", "BECKI", "ARLETHA", "ARGELIA", "ARA", "ALITA", "YULANDA", "YON", "YESSENIA", - "TOBI", "TASIA", "SYLVIE", "SHIRL", "SHIRELY", "SHERIDAN", "SHELLA", "SHANTELLE", "SACHA", "ROYCE", - "REBECKA", "REAGAN", "PROVIDENCIA", "PAULENE", "MISHA", "MIKI", "MARLINE", "MARICA", "LORITA", "LATOYIA", - "LASONYA", "KERSTIN", "KENDA", "KEITHA", "KATHRIN", "JAYMIE", "JACK", "GRICELDA", "GINETTE", "ERYN", - "ELINA", "ELFRIEDA", "DANYEL", "CHEREE", "CHANELLE", "BARRIE", "AVERY", "AURORE", "ANNAMARIA", "ALLEEN", - "AILENE", "AIDE", "YASMINE", "VASHTI", "VALENTINE", "TREASA", "TORY", "TIFFANEY", "SHERYLL", "SHARIE", - "SHANAE", "SAU", "RAISA", "PA", "NEDA", "MITSUKO", "MIRELLA", "MILDA", "MARYANNA", "MARAGRET", - "MABELLE", "LUETTA", "LORINA", "LETISHA", "LATARSHA", "LANELLE", "LAJUANA", "KRISSY", "KARLY", "KARENA", - "JON", "JESSIKA", "JERICA", "JEANELLE", "JANUARY", "JALISA", "JACELYN", "IZOLA", "IVEY", "GREGORY", - "EUNA", "ETHA", "DREW", "DOMITILA", "DOMINICA", "DAINA", "CREOLA", "CARLI", "CAMIE", "BUNNY", - "BRITTNY", "ASHANTI", "ANISHA", "ALEEN", "ADAH", "YASUKO", "WINTER", "VIKI", "VALRIE", "TONA", - "TINISHA", "THI", "TERISA", "TATUM", "TANEKA", "SIMONNE", "SHALANDA", "SERITA", "RESSIE", "REFUGIA", - "PAZ", "OLENE", "NA", "MERRILL", "MARGHERITA", "MANDIE", "MAN", "MAIRE", "LYNDIA", "LUCI", - "LORRIANE", "LORETA", "LEONIA", "LAVONA", "LASHAWNDA", "LAKIA", "KYOKO", "KRYSTINA", "KRYSTEN", "KENIA", - "KELSI", "JUDE", "JEANICE", "ISOBEL", "GEORGIANN", "GENNY", "FELICIDAD", "EILENE", "DEON", "DELOISE", - "DEEDEE", "DANNIE", "CONCEPTION", "CLORA", "CHERILYN", "CHANG", "CALANDRA", "BERRY", "ARMANDINA", "ANISA", - "ULA", "TIMOTHY", "TIERA", "THERESSA", "STEPHANIA", "SIMA", "SHYLA", "SHONTA", "SHERA", "SHAQUITA", - "SHALA", "SAMMY", "ROSSANA", "NOHEMI", "NERY", "MORIAH", "MELITA", "MELIDA", "MELANI", "MARYLYNN", - "MARISHA", "MARIETTE", "MALORIE", "MADELENE", "LUDIVINA", "LORIA", "LORETTE", "LORALEE", "LIANNE", "LEON", - "LAVENIA", "LAURINDA", "LASHON", "KIT", "KIMI", "KEILA", "KATELYNN", "KAI", "JONE", "JOANE", - "JI", "JAYNA", "JANELLA", "JA", "HUE", "HERTHA", "FRANCENE", "ELINORE", "DESPINA", "DELSIE", - "DEEDRA", "CLEMENCIA", "CARRY", "CAROLIN", "CARLOS", "BULAH", "BRITTANIE", "BOK", "BLONDELL", "BIBI", - "BEAULAH", "BEATA", "ANNITA", "AGRIPINA", "VIRGEN", "VALENE", "UN", "TWANDA", "TOMMYE", "TOI", - "TARRA", "TARI", "TAMMERA", "SHAKIA", "SADYE", "RUTHANNE", "ROCHEL", "RIVKA", "PURA", "NENITA", - "NATISHA", "MING", "MERRILEE", "MELODEE", "MARVIS", "LUCILLA", "LEENA", "LAVETA", "LARITA", "LANIE", - "KEREN", "ILEEN", "GEORGEANN", "GENNA", "GENESIS", "FRIDA", "EWA", "EUFEMIA", "EMELY", "ELA", - "EDYTH", "DEONNA", "DEADRA", "DARLENA", "CHANELL", "CHAN", "CATHERN", "CASSONDRA", "CASSAUNDRA", "BERNARDA", - "BERNA", "ARLINDA", "ANAMARIA", "ALBERT", "WESLEY", "VERTIE", "VALERI", "TORRI", "TATYANA", "STASIA", - "SHERISE", "SHERILL", "SEASON", "SCOTTIE", "SANDA", "RUTHE", "ROSY", "ROBERTO", "ROBBI", "RANEE", - "QUYEN", "PEARLY", "PALMIRA", "ONITA", "NISHA", "NIESHA", "NIDA", "NEVADA", "NAM", "MERLYN", - "MAYOLA", "MARYLOUISE", "MARYLAND", "MARX", "MARTH", "MARGENE", "MADELAINE", "LONDA", "LEONTINE", "LEOMA", - "LEIA", "LAWRENCE", "LAURALEE", "LANORA", "LAKITA", "KIYOKO", "KETURAH", "KATELIN", "KAREEN", "JONIE", - "JOHNETTE", "JENEE", "JEANETT", "IZETTA", "HIEDI", "HEIKE", "HASSIE", "HAROLD", "GIUSEPPINA", "GEORGANN", - "FIDELA", "FERNANDE", "ELWANDA", "ELLAMAE", "ELIZ", "DUSTI", "DOTTY", "CYNDY", "CORALIE", "CELESTA", - "ARGENTINA", "ALVERTA", "XENIA", "WAVA", "VANETTA", "TORRIE", "TASHINA", "TANDY", "TAMBRA", "TAMA", - "STEPANIE", "SHILA", "SHAUNTA", "SHARAN", "SHANIQUA", "SHAE", "SETSUKO", "SERAFINA", "SANDEE", "ROSAMARIA", - "PRISCILA", "OLINDA", "NADENE", "MUOI", "MICHELINA", "MERCEDEZ", "MARYROSE", "MARIN", "MARCENE", "MAO", - "MAGALI", "MAFALDA", "LOGAN", "LINN", "LANNIE", "KAYCE", "KAROLINE", "KAMILAH", "KAMALA", "JUSTA", - "JOLINE", "JENNINE", "JACQUETTA", "IRAIDA", "GERALD", "GEORGEANNA", "FRANCHESCA", "FAIRY", "EMELINE", "ELANE", - "EHTEL", "EARLIE", "DULCIE", "DALENE", "CRIS", "CLASSIE", "CHERE", "CHARIS", "CAROYLN", "CARMINA", - "CARITA", "BRIAN", "BETHANIE", "AYAKO", "ARICA", "AN", "ALYSA", "ALESSANDRA", "AKILAH", "ADRIEN", - "ZETTA", "YOULANDA", "YELENA", "YAHAIRA", "XUAN", "WENDOLYN", "VICTOR", "TIJUANA", "TERRELL", "TERINA", - "TERESIA", "SUZI", "SUNDAY", "SHERELL", "SHAVONDA", "SHAUNTE", "SHARDA", "SHAKITA", "SENA", "RYANN", - "RUBI", "RIVA", "REGINIA", "REA", "RACHAL", "PARTHENIA", "PAMULA", "MONNIE", "MONET", "MICHAELE", - "MELIA", "MARINE", "MALKA", "MAISHA", "LISANDRA", "LEO", "LEKISHA", "LEAN", "LAURENCE", "LAKENDRA", - "KRYSTIN", "KORTNEY", "KIZZIE", "KITTIE", "KERA", "KENDAL", "KEMBERLY", "KANISHA", "JULENE", "JULE", - "JOSHUA", "JOHANNE", "JEFFREY", "JAMEE", "HAN", "HALLEY", "GIDGET", "GALINA", "FREDRICKA", "FLETA", - "FATIMAH", "EUSEBIA", "ELZA", "ELEONORE", "DORTHEY", "DORIA", "DONELLA", "DINORAH", "DELORSE", "CLARETHA", - "CHRISTINIA", "CHARLYN", "BONG", "BELKIS", "AZZIE", "ANDERA", "AIKO", "ADENA", "YER", "YAJAIRA", - "WAN", "VANIA", "ULRIKE", "TOSHIA", "TIFANY", "STEFANY", "SHIZUE", "SHENIKA", "SHAWANNA", "SHAROLYN", - "SHARILYN", "SHAQUANA", "SHANTAY", "SEE", "ROZANNE", "ROSELEE", "RICKIE", "REMONA", "REANNA", "RAELENE", - "QUINN", "PHUNG", "PETRONILA", "NATACHA", "NANCEY", "MYRL", "MIYOKO", "MIESHA", "MERIDETH", "MARVELLA", - "MARQUITTA", "MARHTA", "MARCHELLE", "LIZETH", "LIBBIE", "LAHOMA", "LADAWN", "KINA", "KATHELEEN", "KATHARYN", - "KARISA", "KALEIGH", "JUNIE", "JULIEANN", "JOHNSIE", "JANEAN", "JAIMEE", "JACKQUELINE", "HISAKO", "HERMA", - "HELAINE", "GWYNETH", "GLENN", "GITA", "EUSTOLIA", "EMELINA", "ELIN", "EDRIS", "DONNETTE", "DONNETTA", - "DIERDRE", "DENAE", "DARCEL", "CLAUDE", "CLARISA", "CINDERELLA", "CHIA", "CHARLESETTA", "CHARITA", "CELSA", - "CASSY", "CASSI", "CARLEE", "BRUNA", "BRITTANEY", "BRANDE", "BILLI", "BAO", "ANTONETTA", "ANGLA", - "ANGELYN", "ANALISA", "ALANE", "WENONA", "WENDIE", "VERONIQUE", "VANNESA", "TOBIE", "TEMPIE", "SUMIKO", - "SULEMA", "SPARKLE", "SOMER", "SHEBA", "SHAYNE", "SHARICE", "SHANEL", "SHALON", "SAGE", "ROY", - "ROSIO", "ROSELIA", "RENAY", "REMA", "REENA", "PORSCHE", "PING", "PEG", "OZIE", "ORETHA", - "ORALEE", "ODA", "NU", "NGAN", "NAKESHA", "MILLY", "MARYBELLE", "MARLIN", "MARIS", "MARGRETT", - "MARAGARET", "MANIE", "LURLENE", "LILLIA", "LIESELOTTE", "LAVELLE", "LASHAUNDA", "LAKEESHA", "KEITH", "KAYCEE", - "KALYN", "JOYA", "JOETTE", "JENAE", "JANIECE", "ILLA", "GRISEL", "GLAYDS", "GENEVIE", "GALA", - "FREDDA", "FRED", "ELMER", "ELEONOR", "DEBERA", "DEANDREA", "DAN", "CORRINNE", "CORDIA", "CONTESSA", - "COLENE", "CLEOTILDE", "CHARLOTT", "CHANTAY", "CECILLE", "BEATRIS", "AZALEE", "ARLEAN", "ARDATH", "ANJELICA", - "ANJA", "ALFREDIA", "ALEISHA", "ADAM", "ZADA", "YUONNE", "XIAO", "WILLODEAN", "WHITLEY", "VENNIE", - "VANNA", "TYISHA", "TOVA", "TORIE", "TONISHA", "TILDA", "TIEN", "TEMPLE", "SIRENA", "SHERRIL", - "SHANTI", "SHAN", "SENAIDA", "SAMELLA", "ROBBYN", "RENDA", "REITA", "PHEBE", "PAULITA", "NOBUKO", - "NGUYET", "NEOMI", "MOON", "MIKAELA", "MELANIA", "MAXIMINA", "MARG", "MAISIE", "LYNNA", "LILLI", - "LAYNE", "LASHAUN", "LAKENYA", "LAEL", "KIRSTIE", "KATHLINE", "KASHA", "KARLYN", "KARIMA", "JOVAN", - "JOSEFINE", "JENNELL", "JACQUI", "JACKELYN", "HYO", "HIEN", "GRAZYNA", "FLORRIE", "FLORIA", "ELEONORA", - "DWANA", "DORLA", "DONG", "DELMY", "DEJA", "DEDE", "DANN", "CRYSTA", "CLELIA", "CLARIS", - "CLARENCE", "CHIEKO", "CHERLYN", "CHERELLE", "CHARMAIN", "CHARA", "CAMMY", "BEE", "ARNETTE", "ARDELLE", - "ANNIKA", "AMIEE", "AMEE", "ALLENA", "YVONE", "YUKI", "YOSHIE", "YEVETTE", "YAEL", "WILLETTA", - "VONCILE", "VENETTA", "TULA", "TONETTE", "TIMIKA", "TEMIKA", "TELMA", "TEISHA", "TAREN", "TA", - "STACEE", "SHIN", "SHAWNTA", "SATURNINA", "RICARDA", "POK", "PASTY", "ONIE", "NUBIA", "MORA", - "MIKE", "MARIELLE", "MARIELLA", "MARIANELA", "MARDELL", "MANY", "LUANNA", "LOISE", "LISABETH", "LINDSY", - "LILLIANA", "LILLIAM", "LELAH", "LEIGHA", "LEANORA", "LANG", "KRISTEEN", "KHALILAH", "KEELEY", "KANDRA", - "JUNKO", "JOAQUINA", "JERLENE", "JANI", "JAMIKA", "JAME", "HSIU", "HERMILA", "GOLDEN", "GENEVIVE", - "EVIA", "EUGENA", "EMMALINE", "ELFREDA", "ELENE", "DONETTE", "DELCIE", "DEEANNA", "DARCEY", "CUC", - "CLARINDA", "CIRA", "CHAE", "CELINDA", "CATHERYN", "CATHERIN", "CASIMIRA", "CARMELIA", "CAMELLIA", "BREANA", - "BOBETTE", "BERNARDINA", "BEBE", "BASILIA", "ARLYNE", "AMAL", "ALAYNA", "ZONIA", "ZENIA", "YURIKO", - "YAEKO", "WYNELL", "WILLOW", "WILLENA", "VERNIA", "TU", "TRAVIS", "TORA", "TERRILYN", "TERICA", - "TENESHA", "TAWNA", "TAJUANA", "TAINA", "STEPHNIE", "SONA", "SOL", "SINA", "SHONDRA", "SHIZUKO", - "SHERLENE", "SHERICE", "SHARIKA", "ROSSIE", "ROSENA", "RORY", "RIMA", "RIA", "RHEBA", "RENNA", - "PETER", "NATALYA", "NANCEE", "MELODI", "MEDA", "MAXIMA", "MATHA", "MARKETTA", "MARICRUZ", "MARCELENE", - "MALVINA", "LUBA", "LOUETTA", "LEIDA", "LECIA", "LAURAN", "LASHAWNA", "LAINE", "KHADIJAH", "KATERINE", - "KASI", "KALLIE", "JULIETTA", "JESUSITA", "JESTINE", "JESSIA", "JEREMY", "JEFFIE", "JANYCE", "ISADORA", - "GEORGIANNE", "FIDELIA", "EVITA", "EURA", "EULAH", "ESTEFANA", "ELSY", "ELIZABET", "ELADIA", "DODIE", - "DION", "DIA", "DENISSE", "DELORAS", "DELILA", "DAYSI", "DAKOTA", "CURTIS", "CRYSTLE", "CONCHA", - "COLBY", "CLARETTA", "CHU", "CHRISTIA", "CHARLSIE", "CHARLENA", "CARYLON", "BETTYANN", "ASLEY", "ASHLEA", - "AMIRA", "AI", "AGUEDA", "AGNUS", "YUETTE", "VINITA", "VICTORINA", "TYNISHA", "TREENA", "TOCCARA", - "TISH", "THOMASENA", "TEGAN", "SOILA", "SHILOH", "SHENNA", "SHARMAINE", "SHANTAE", "SHANDI", "SEPTEMBER", - "SARAN", "SARAI", "SANA", "SAMUEL", "SALLEY", "ROSETTE", "ROLANDE", "REGINE", "OTELIA", "OSCAR", - "OLEVIA", "NICHOLLE", "NECOLE", "NAIDA", "MYRTA", "MYESHA", "MITSUE", "MINTA", "MERTIE", "MARGY", - "MAHALIA", "MADALENE", "LOVE", "LOURA", "LOREAN", "LEWIS", "LESHA", "LEONIDA", "LENITA", "LAVONE", - "LASHELL", "LASHANDRA", "LAMONICA", "KIMBRA", "KATHERINA", "KARRY", "KANESHA", "JULIO", "JONG", "JENEVA", - "JAQUELYN", "HWA", "GILMA", "GHISLAINE", "GERTRUDIS", "FRANSISCA", "FERMINA", "ETTIE", "ETSUKO", "ELLIS", - "ELLAN", "ELIDIA", "EDRA", "DORETHEA", "DOREATHA", "DENYSE", "DENNY", "DEETTA", "DAINE", "CYRSTAL", - "CORRIN", "CAYLA", "CARLITA", "CAMILA", "BURMA", "BULA", "BUENA", "BLAKE", "BARABARA", "AVRIL", - "AUSTIN", "ALAINE", "ZANA", "WILHEMINA", "WANETTA", "VIRGIL", "VI", "VERONIKA", "VERNON", "VERLINE", - "VASILIKI", "TONITA", "TISA", "TEOFILA", "TAYNA", "TAUNYA", "TANDRA", "TAKAKO", "SUNNI", "SUANNE", - "SIXTA", "SHARELL", "SEEMA", "RUSSELL", "ROSENDA", "ROBENA", "RAYMONDE", "PEI", "PAMILA", "OZELL", - "NEIDA", "NEELY", "MISTIE", "MICHA", "MERISSA", "MAURITA", "MARYLN", "MARYETTA", "MARSHALL", "MARCELL", - "MALENA", "MAKEDA", "MADDIE", "LOVETTA", "LOURIE", "LORRINE", "LORILEE", "LESTER", "LAURENA", "LASHAY", - "LARRAINE", "LAREE", "LACRESHA", "KRISTLE", "KRISHNA", "KEVA", "KEIRA", "KAROLE", "JOIE", "JINNY", - "JEANNETTA", "JAMA", "HEIDY", "GILBERTE", "GEMA", "FAVIOLA", "EVELYNN", "ENDA", "ELLI", "ELLENA", - "DIVINA", "DAGNY", "COLLENE", "CODI", "CINDIE", "CHASSIDY", "CHASIDY", "CATRICE", "CATHERINA", "CASSEY", - "CAROLL", "CARLENA", "CANDRA", "CALISTA", "BRYANNA", "BRITTENY", "BEULA", "BARI", "AUDRIE", "AUDRIA", - "ARDELIA", "ANNELLE", "ANGILA", "ALONA", "ALLYN", "DOUGLAS", "ROGER", "JONATHAN", "RALPH", "NICHOLAS", - "BENJAMIN", "BRUCE", "HARRY", "WAYNE", "STEVE", "HOWARD", "ERNEST", "PHILLIP", "TODD", "CRAIG", - "ALAN", "PHILIP", "EARL", "DANNY", "BRYAN", "STANLEY", "LEONARD", "NATHAN", "MANUEL", "RODNEY", - "MARVIN", "VINCENT", "JEFFERY", "JEFF", "CHAD", "JACOB", "ALFRED", "BRADLEY", "HERBERT", "FREDERICK", - "EDWIN", "DON", "RICKY", "RANDALL", "BARRY", "BERNARD", "LEROY", "MARCUS", "THEODORE", "CLIFFORD", - "MIGUEL", "JIM", "TOM", "CALVIN", "BILL", "LLOYD", "DEREK", "WARREN", "DARRELL", "JEROME", - "FLOYD", "ALVIN", "TIM", "GORDON", "GREG", "JORGE", "DUSTIN", "PEDRO", "DERRICK", "ZACHARY", - "HERMAN", "GLEN", "HECTOR", "RICARDO", "RICK", "BRENT", "RAMON", "GILBERT", "MARC", "REGINALD", - "RUBEN", "NATHANIEL", "RAFAEL", "EDGAR", "MILTON", "RAUL", "BEN", "CHESTER", "DUANE", "FRANKLIN", - "BRAD", "RON", "ROLAND", "ARNOLD", "HARVEY", "JARED", "ERIK", "DARRYL", "NEIL", "JAVIER", - "FERNANDO", "CLINTON", "TED", "MATHEW", "TYRONE", "DARREN", "LANCE", "KURT", "ALLAN", "NELSON", - "GUY", "CLAYTON", "HUGH", "MAX", "DWAYNE", "DWIGHT", "ARMANDO", "FELIX", "EVERETT", "IAN", - "WALLACE", "KEN", "BOB", "ALFREDO", "ALBERTO", "DAVE", "IVAN", "BYRON", "ISAAC", "MORRIS", - "CLIFTON", "WILLARD", "ROSS", "ANDY", "SALVADOR", "KIRK", "SERGIO", "SETH", "KENT", "TERRANCE", - "EDUARDO", "TERRENCE", "ENRIQUE", "WADE", "STUART", "FREDRICK", "ARTURO", "ALEJANDRO", "NICK", "LUTHER", - "WENDELL", "JEREMIAH", "JULIUS", "OTIS", "TREVOR", "OLIVER", "LUKE", "HOMER", "GERARD", "DOUG", - "KENNY", "HUBERT", "LYLE", "MATT", "ALFONSO", "ORLANDO", "REX", "CARLTON", "ERNESTO", "NEAL", - "PABLO", "LORENZO", "OMAR", "WILBUR", "GRANT", "HORACE", "RODERICK", "ABRAHAM", "WILLIS", "RICKEY", - "ANDRES", "CESAR", "JOHNATHAN", "MALCOLM", "RUDOLPH", "DAMON", "KELVIN", "PRESTON", "ALTON", "ARCHIE", - "MARCO", "WM", "PETE", "RANDOLPH", "GARRY", "GEOFFREY", "JONATHON", "FELIPE", "GERARDO", "ED", - "DOMINIC", "DELBERT", "COLIN", "GUILLERMO", "EARNEST", "LUCAS", "BENNY", "SPENCER", "RODOLFO", "MYRON", - "EDMUND", "GARRETT", "SALVATORE", "CEDRIC", "LOWELL", "GREGG", "SHERMAN", "WILSON", "SYLVESTER", "ROOSEVELT", - "ISRAEL", "JERMAINE", "FORREST", "WILBERT", "LELAND", "SIMON", "CLARK", "IRVING", "BRYANT", "OWEN", - "RUFUS", "WOODROW", "KRISTOPHER", "MACK", "LEVI", "MARCOS", "GUSTAVO", "JAKE", "LIONEL", "GILBERTO", - "CLINT", "NICOLAS", "ISMAEL", "ORVILLE", "ERVIN", "DEWEY", "AL", "WILFRED", "JOSH", "HUGO", - "IGNACIO", "CALEB", "TOMAS", "SHELDON", "ERICK", "STEWART", "DOYLE", "DARREL", "ROGELIO", "TERENCE", - "SANTIAGO", "ALONZO", "ELIAS", "BERT", "ELBERT", "RAMIRO", "CONRAD", "NOAH", "GRADY", "PHIL", - "CORNELIUS", "LAMAR", "ROLANDO", "CLAY", "PERCY", "DEXTER", "BRADFORD", "DARIN", "AMOS", "MOSES", - "IRVIN", "SAUL", "ROMAN", "RANDAL", "TIMMY", "DARRIN", "WINSTON", "BRENDAN", "ABEL", "DOMINICK", - "BOYD", "EMILIO", "ELIJAH", "DOMINGO", "EMMETT", "MARLON", "EMANUEL", "JERALD", "EDMOND", "EMIL", - "DEWAYNE", "WILL", "OTTO", "TEDDY", "REYNALDO", "BRET", "JESS", "TRENT", "HUMBERTO", "EMMANUEL", - "STEPHAN", "VICENTE", "LAMONT", "GARLAND", "MILES", "EFRAIN", "HEATH", "RODGER", "HARLEY", "ETHAN", - "ELDON", "ROCKY", "PIERRE", "JUNIOR", "FREDDY", "ELI", "BRYCE", "ANTOINE", "STERLING", "CHASE", - "GROVER", "ELTON", "CLEVELAND", "DYLAN", "CHUCK", "DAMIAN", "REUBEN", "STAN", "AUGUST", "LEONARDO", - "JASPER", "RUSSEL", "ERWIN", "BENITO", "HANS", "MONTE", "BLAINE", "ERNIE", "CURT", "QUENTIN", - "AGUSTIN", "MURRAY", "JAMAL", "ADOLFO", "HARRISON", "TYSON", "BURTON", "BRADY", "ELLIOTT", "WILFREDO", - "BART", "JARROD", "VANCE", "DENIS", "DAMIEN", "JOAQUIN", "HARLAN", "DESMOND", "ELLIOT", "DARWIN", - "GREGORIO", "BUDDY", "XAVIER", "KERMIT", "ROSCOE", "ESTEBAN", "ANTON", "SOLOMON", "SCOTTY", "NORBERT", - "ELVIN", "WILLIAMS", "NOLAN", "ROD", "QUINTON", "HAL", "BRAIN", "ROB", "ELWOOD", "KENDRICK", - "DARIUS", "MOISES", "FIDEL", "THADDEUS", "CLIFF", "MARCEL", "JACKSON", "RAPHAEL", "BRYON", "ARMAND", - "ALVARO", "JEFFRY", "DANE", "JOESPH", "THURMAN", "NED", "RUSTY", "MONTY", "FABIAN", "REGGIE", - "MASON", "GRAHAM", "ISAIAH", "VAUGHN", "GUS", "LOYD", "DIEGO", "ADOLPH", "NORRIS", "MILLARD", - "ROCCO", "GONZALO", "DERICK", "RODRIGO", "WILEY", "RIGOBERTO", "ALPHONSO", "TY", "NOE", "VERN", - "REED", "JEFFERSON", "ELVIS", "BERNARDO", "MAURICIO", "HIRAM", "DONOVAN", "BASIL", "RILEY", "NICKOLAS", - "MAYNARD", "SCOT", "VINCE", "QUINCY", "EDDY", "SEBASTIAN", "FEDERICO", "ULYSSES", "HERIBERTO", "DONNELL", - "COLE", "DAVIS", "GAVIN", "EMERY", "WARD", "ROMEO", "JAYSON", "DANTE", "CLEMENT", "COY", - "MAXWELL", "JARVIS", "BRUNO", "ISSAC", "DUDLEY", "BROCK", "SANFORD", "CARMELO", "BARNEY", "NESTOR", - "STEFAN", "DONNY", "ART", "LINWOOD", "BEAU", "WELDON", "GALEN", "ISIDRO", "TRUMAN", "DELMAR", - "JOHNATHON", "SILAS", "FREDERIC", "DICK", "IRWIN", "MERLIN", "CHARLEY", "MARCELINO", "HARRIS", "CARLO", - "TRENTON", "KURTIS", "HUNTER", "AURELIO", "WINFRED", "VITO", "COLLIN", "DENVER", "CARTER", "LEONEL", - "EMORY", "PASQUALE", "MOHAMMAD", "MARIANO", "DANIAL", "LANDON", "DIRK", "BRANDEN", "ADAN", "BUFORD", - "GERMAN", "WILMER", "EMERSON", "ZACHERY", "FLETCHER", "JACQUES", "ERROL", "DALTON", "MONROE", "JOSUE", - "EDWARDO", "BOOKER", "WILFORD", "SONNY", "SHELTON", "CARSON", "THERON", "RAYMUNDO", "DAREN", "HOUSTON", - "ROBBY", "LINCOLN", "GENARO", "BENNETT", "OCTAVIO", "CORNELL", "HUNG", "ARRON", "ANTONY", "HERSCHEL", - "GIOVANNI", "GARTH", "CYRUS", "CYRIL", "RONNY", "LON", "FREEMAN", "DUNCAN", "KENNITH", "CARMINE", - "ERICH", "CHADWICK", "WILBURN", "RUSS", "REID", "MYLES", "ANDERSON", "MORTON", "JONAS", "FOREST", - "MITCHEL", "MERVIN", "ZANE", "RICH", "JAMEL", "LAZARO", "ALPHONSE", "RANDELL", "MAJOR", "JARRETT", - "BROOKS", "ABDUL", "LUCIANO", "SEYMOUR", "EUGENIO", "MOHAMMED", "VALENTIN", "CHANCE", "ARNULFO", "LUCIEN", - "FERDINAND", "THAD", "EZRA", "ALDO", "RUBIN", "ROYAL", "MITCH", "EARLE", "ABE", "WYATT", - "MARQUIS", "LANNY", "KAREEM", "JAMAR", "BORIS", "ISIAH", "EMILE", "ELMO", "ARON", "LEOPOLDO", - "EVERETTE", "JOSEF", "ELOY", "RODRICK", "REINALDO", "LUCIO", "JERROD", "WESTON", "HERSHEL", "BARTON", - "PARKER", "LEMUEL", "BURT", "JULES", "GIL", "ELISEO", "AHMAD", "NIGEL", "EFREN", "ANTWAN", - "ALDEN", "MARGARITO", "COLEMAN", "DINO", "OSVALDO", "LES", "DEANDRE", "NORMAND", "KIETH", "TREY", - "NORBERTO", "NAPOLEON", "JEROLD", "FRITZ", "ROSENDO", "MILFORD", "CHRISTOPER", "ALFONZO", "LYMAN", "JOSIAH", - "BRANT", "WILTON", "RICO", "JAMAAL", "DEWITT", "BRENTON", "OLIN", "FOSTER", "FAUSTINO", "CLAUDIO", - "JUDSON", "GINO", "EDGARDO", "ALEC", "TANNER", "JARRED", "DONN", "TAD", "PRINCE", "PORFIRIO", - "ODIS", "LENARD", "CHAUNCEY", "TOD", "MEL", "MARCELO", "KORY", "AUGUSTUS", "KEVEN", "HILARIO", - "BUD", "SAL", "ORVAL", "MAURO", "ZACHARIAH", "OLEN", "ANIBAL", "MILO", "JED", "DILLON", - "AMADO", "NEWTON", "LENNY", "RICHIE", "HORACIO", "BRICE", "MOHAMED", "DELMER", "DARIO", "REYES", - "MAC", "JONAH", "JERROLD", "ROBT", "HANK", "RUPERT", "ROLLAND", "KENTON", "DAMION", "ANTONE", - "WALDO", "FREDRIC", "BRADLY", "KIP", "BURL", "WALKER", "TYREE", "JEFFEREY", "AHMED", "WILLY", - "STANFORD", "OREN", "NOBLE", "MOSHE", "MIKEL", "ENOCH", "BRENDON", "QUINTIN", "JAMISON", "FLORENCIO", - "DARRICK", "TOBIAS", "HASSAN", "GIUSEPPE", "DEMARCUS", "CLETUS", "TYRELL", "LYNDON", "KEENAN", "WERNER", - "GERALDO", "COLUMBUS", "CHET", "BERTRAM", "MARKUS", "HUEY", "HILTON", "DWAIN", "DONTE", "TYRON", - "OMER", "ISAIAS", "HIPOLITO", "FERMIN", "ADALBERTO", "BO", "BARRETT", "TEODORO", "MCKINLEY", "MAXIMO", - "GARFIELD", "RALEIGH", "LAWERENCE", "ABRAM", "RASHAD", "KING", "EMMITT", "DARON", "SAMUAL", "MIQUEL", - "EUSEBIO", "DOMENIC", "DARRON", "BUSTER", "WILBER", "RENATO", "JC", "HOYT", "HAYWOOD", "EZEKIEL", - "CHAS", "FLORENTINO", "ELROY", "CLEMENTE", "ARDEN", "NEVILLE", "EDISON", "DESHAWN", "NATHANIAL", "JORDON", - "DANILO", "CLAUD", "SHERWOOD", "RAYMON", "RAYFORD", "CRISTOBAL", "AMBROSE", "TITUS", "HYMAN", "FELTON", - "EZEQUIEL", "ERASMO", "STANTON", "LONNY", "LEN", "IKE", "MILAN", "LINO", "JAROD", "HERB", - "ANDREAS", "WALTON", "RHETT", "PALMER", "DOUGLASS", "CORDELL", "OSWALDO", "ELLSWORTH", "VIRGILIO", "TONEY", - "NATHANAEL", "DEL", "BENEDICT", "MOSE", "JOHNSON", "ISREAL", "GARRET", "FAUSTO", "ASA", "ARLEN", - "ZACK", "WARNER", "MODESTO", "FRANCESCO", "MANUAL", "GAYLORD", "GASTON", "FILIBERTO", "DEANGELO", "MICHALE", - "GRANVILLE", "WES", "MALIK", "ZACKARY", "TUAN", "ELDRIDGE", "CRISTOPHER", "CORTEZ", "ANTIONE", "MALCOM", - "LONG", "KOREY", "JOSPEH", "COLTON", "WAYLON", "VON", "HOSEA", "SHAD", "SANTO", "RUDOLF", - "ROLF", "REY", "RENALDO", "MARCELLUS", "LUCIUS", "KRISTOFER", "BOYCE", "BENTON", "HAYDEN", "HARLAND", - "ARNOLDO", "RUEBEN", "LEANDRO", "KRAIG", "JERRELL", "JEROMY", "HOBERT", "CEDRICK", "ARLIE", "WINFORD", - "WALLY", "LUIGI", "KENETH", "JACINTO", "GRAIG", "FRANKLYN", "EDMUNDO", "SID", "PORTER", "LEIF", - "JERAMY", "BUCK", "WILLIAN", "VINCENZO", "SHON", "LYNWOOD", "JERE", "HAI", "ELDEN", "DORSEY", - "DARELL", "BRODERICK", "ALONSO" - ] - total_sum = 0 - temp_sum = 0 - name.sort() - for i in range(len(name)): - for j in name[i]: - temp_sum += ord(j) - ord('A') + 1 - total_sum += (i + 1) * temp_sum - temp_sum = 0 - print(total_sum) +# -*- coding: latin-1 -*- +""" +Name scores +Problem 22 +Using names.txt (right click and 'Save Link/Target As...'), a 46K text file +containing over five-thousand first names, begin by sorting it into +alphabetical order. Then working out the alphabetical value for each name, +multiply this value by its alphabetical position in the list to obtain a name +score. -if __name__ == '__main__': - main() +For example, when the list is sorted into alphabetical order, COLIN, which is +worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would +obtain a score of 938 × 53 = 49714. + +What is the total of all the name scores in the file? +""" + + +def solution(): + """Returns the total of all the name scores in the file. + + >>> solution() + 871198282 + """ + total_sum = 0 + temp_sum = 0 + with open("p022_names.txt") as file: + name = str(file.readlines()[0]) + name = name.replace('"', "").split(",") + + name.sort() + for i in range(len(name)): + for j in name[i]: + temp_sum += ord(j) - ord("A") + 1 + total_sum += (i + 1) * temp_sum + temp_sum = 0 + return total_sum + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_234/sol1.py b/project_euler/problem_234/sol1.py index c7a6bd97d66b..8298a7f8cce3 100644 --- a/project_euler/problem_234/sol1.py +++ b/project_euler/problem_234/sol1.py @@ -1,4 +1,22 @@ -# https://projecteuler.net/problem=234 +""" +https://projecteuler.net/problem=234 + +For an integer n ≥ 4, we define the lower prime square root of n, denoted by +lps(n), as the largest prime ≤ √n and the upper prime square root of n, ups(n), +as the smallest prime ≥ √n. + +So, for example, lps(4) = 2 = ups(4), lps(1000) = 31, ups(1000) = 37. Let us +call an integer n ≥ 4 semidivisible, if one of lps(n) and ups(n) divides n, +but not both. + +The sum of the semidivisible numbers not exceeding 15 is 30, the numbers are 8, +10 and 12. 15 is not semidivisible because it is a multiple of both lps(15) = 3 +and ups(15) = 5. As a further example, the sum of the 92 semidivisible numbers +up to 1000 is 34825. + +What is the sum of all semidivisible numbers not exceeding 999966663333 ? +""" + def fib(a, b, n): if n==1: @@ -17,16 +35,22 @@ def fib(a, b, n): return c -q=int(input()) -for x in range(q): - l=[i for i in input().split()] - c1=0 - c2=1 - while(1): - - if len(fib(l[0],l[1],c2))>> solution() + '2783915460' + """ + result = list(map("".join, permutations("0123456789"))) + return result[999999] + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_25/sol1.py b/project_euler/problem_25/sol1.py index f8cea3093dcf..be3b4d9b2d7d 100644 --- a/project_euler/problem_25/sol1.py +++ b/project_euler/problem_25/sol1.py @@ -1,31 +1,75 @@ -from __future__ import print_function +# -*- coding: utf-8 -*- +""" +The Fibonacci sequence is defined by the recurrence relation: + + Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. + +Hence the first 12 terms will be: + + F1 = 1 + F2 = 1 + F3 = 2 + F4 = 3 + F5 = 5 + F6 = 8 + F7 = 13 + F8 = 21 + F9 = 34 + F10 = 55 + F11 = 89 + F12 = 144 + +The 12th term, F12, is the first term to contain three digits. + +What is the index of the first term in the Fibonacci sequence to contain 1000 +digits? +""" try: - xrange #Python 2 + xrange # Python 2 except NameError: - xrange = range #Python 3 + xrange = range # Python 3 + def fibonacci(n): - if n == 1 or type(n) is not int: - return 0 - elif n == 2: - return 1 - else: - sequence = [0, 1] - for i in xrange(2, n+1): - sequence.append(sequence[i-1] + sequence[i-2]) + if n == 1 or type(n) is not int: + return 0 + elif n == 2: + return 1 + else: + sequence = [0, 1] + for i in xrange(2, n + 1): + sequence.append(sequence[i - 1] + sequence[i - 2]) + + return sequence[n] - return sequence[n] def fibonacci_digits_index(n): - digits = 0 - index = 2 + digits = 0 + index = 2 + + while digits < n: + index += 1 + digits = len(str(fibonacci(index))) + + return index + + +def solution(n): + """Returns the index of the first term in the Fibonacci sequence to contain + n digits. - while digits < n: - index += 1 - digits = len(str(fibonacci(index))) + >>> solution(1000) + 4782 + >>> solution(100) + 476 + >>> solution(50) + 237 + >>> solution(3) + 12 + """ + return fibonacci_digits_index(n) - return index -if __name__ == '__main__': - print(fibonacci_digits_index(1000)) \ No newline at end of file +if __name__ == "__main__": + print(solution(int(str(input()).strip()))) diff --git a/project_euler/problem_25/sol2.py b/project_euler/problem_25/sol2.py index 35147a9bfb14..d754e2ddd722 100644 --- a/project_euler/problem_25/sol2.py +++ b/project_euler/problem_25/sol2.py @@ -1,10 +1,57 @@ -def fibonacci_genrator(): - a, b = 0,1 - while True: - a,b = b,a+b - yield b -answer = 1 -gen = fibonacci_genrator() -while len(str(next(gen))) < 1000: - answer += 1 -assert answer+1 == 4782 +# -*- coding: utf-8 -*- +""" +The Fibonacci sequence is defined by the recurrence relation: + + Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. + +Hence the first 12 terms will be: + + F1 = 1 + F2 = 1 + F3 = 2 + F4 = 3 + F5 = 5 + F6 = 8 + F7 = 13 + F8 = 21 + F9 = 34 + F10 = 55 + F11 = 89 + F12 = 144 + +The 12th term, F12, is the first term to contain three digits. + +What is the index of the first term in the Fibonacci sequence to contain 1000 +digits? +""" + + +def fibonacci_generator(): + a, b = 0, 1 + while True: + a, b = b, a + b + yield b + + +def solution(n): + """Returns the index of the first term in the Fibonacci sequence to contain + n digits. + + >>> solution(1000) + 4782 + >>> solution(100) + 476 + >>> solution(50) + 237 + >>> solution(3) + 12 + """ + answer = 1 + gen = fibonacci_generator() + while len(str(next(gen))) < n: + answer += 1 + return answer + 1 + + +if __name__ == "__main__": + print(solution(int(str(input()).strip()))) diff --git a/project_euler/problem_28/sol1.py b/project_euler/problem_28/sol1.py index 4942115ce537..63386ce3058c 100644 --- a/project_euler/problem_28/sol1.py +++ b/project_euler/problem_28/sol1.py @@ -1,29 +1,60 @@ -from __future__ import print_function +""" +Starting with the number 1 and moving to the right in a clockwise direction a 5 +by 5 spiral is formed as follows: + + 21 22 23 24 25 + 20 7 8 9 10 + 19 6 1 2 11 + 18 5 4 3 12 + 17 16 15 14 13 + +It can be verified that the sum of the numbers on the diagonals is 101. + +What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed +in the same way? +""" + from math import ceil try: - xrange #Python 2 + xrange # Python 2 except NameError: - xrange = range #Python 3 + xrange = range # Python 3 + def diagonal_sum(n): - total = 1 - - for i in xrange(1, int(ceil(n/2.0))): - odd = 2*i+1 - even = 2*i - total = total + 4*odd**2 - 6*even - - return total - -if __name__ == '__main__': - import sys - - if len(sys.argv) == 1: - print(diagonal_sum(1001)) - else: - try: - n = int(sys.argv[1]) - diagonal_sum(n) - except ValueError: - print('Invalid entry - please enter a number') \ No newline at end of file + """Returns the sum of the numbers on the diagonals in a n by n spiral + formed in the same way. + + >>> diagonal_sum(1001) + 669171001 + >>> diagonal_sum(500) + 82959497 + >>> diagonal_sum(100) + 651897 + >>> diagonal_sum(50) + 79697 + >>> diagonal_sum(10) + 537 + """ + total = 1 + + for i in xrange(1, int(ceil(n / 2.0))): + odd = 2 * i + 1 + even = 2 * i + total = total + 4 * odd ** 2 - 6 * even + + return total + + +if __name__ == "__main__": + import sys + + if len(sys.argv) == 1: + print(diagonal_sum(1001)) + else: + try: + n = int(sys.argv[1]) + print(diagonal_sum(n)) + except ValueError: + print("Invalid entry - please enter a number") diff --git a/project_euler/problem_29/solution.py b/project_euler/problem_29/solution.py index 64d35c84d9ca..e67dafe4639d 100644 --- a/project_euler/problem_29/solution.py +++ b/project_euler/problem_29/solution.py @@ -1,33 +1,51 @@ -def main(): +""" +Consider all integer combinations of ab for 2 <= a <= 5 and 2 <= b <= 5: + +2^2=4, 2^3=8, 2^4=16, 2^5=32 +3^2=9, 3^3=27, 3^4=81, 3^5=243 +4^2=16, 4^3=64, 4^4=256, 4^5=1024 +5^2=25, 5^3=125, 5^4=625, 5^5=3125 + +If they are then placed in numerical order, with any repeats removed, we get +the following sequence of 15 distinct terms: + +4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 + +How many distinct terms are in the sequence generated by ab +for 2 <= a <= 100 and 2 <= b <= 100? +""" +from __future__ import print_function + + +def solution(n): + """Returns the number of distinct terms in the sequence generated by a^b + for 2 <= a <= 100 and 2 <= b <= 100. + + >>> solution(100) + 9183 + >>> solution(50) + 2184 + >>> solution(20) + 324 + >>> solution(5) + 15 + >>> solution(2) + 1 + >>> solution(1) + 0 """ - Consider all integer combinations of ab for 2 <= a <= 5 and 2 <= b <= 5: - - 22=4, 23=8, 24=16, 25=32 - 32=9, 33=27, 34=81, 35=243 - 42=16, 43=64, 44=256, 45=1024 - 52=25, 53=125, 54=625, 55=3125 - If they are then placed in numerical order, with any repeats removed, - we get the following sequence of 15 distinct terms: - - 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 - - How many distinct terms are in the sequence generated by ab - for 2 <= a <= 100 and 2 <= b <= 100? - """ - collectPowers = set() currentPow = 0 - N = 101 # maximum limit + N = n + 1 # maximum limit for a in range(2, N): for b in range(2, N): - currentPow = a**b # calculates the current power - collectPowers.add(currentPow) # adds the result to the set - - print("Number of terms ", len(collectPowers)) + currentPow = a ** b # calculates the current power + collectPowers.add(currentPow) # adds the result to the set + return len(collectPowers) -if __name__ == '__main__': - main() +if __name__ == "__main__": + print("Number of terms ", solution(int(str(input()).strip()))) diff --git a/project_euler/problem_31/sol1.py b/project_euler/problem_31/sol1.py index 33653722f890..e2a209e5df5a 100644 --- a/project_euler/problem_31/sol1.py +++ b/project_euler/problem_31/sol1.py @@ -1,10 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import print_function -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 -''' +""" Coin sums Problem 31 In England the currency is made up of pound, £, and pence, p, and there are @@ -15,7 +10,13 @@ 1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p How many different ways can £2 be made using any number of coins? -''' +""" +from __future__ import print_function + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 def one_pence(): @@ -50,4 +51,21 @@ def two_pound(x): return 0 if x < 0 else two_pound(x - 200) + one_pound(x) -print(two_pound(200)) +def solution(n): + """Returns the number of different ways can £n be made using any number of + coins? + + >>> solution(500) + 6295434 + >>> solution(200) + 73682 + >>> solution(50) + 451 + >>> solution(10) + 11 + """ + return two_pound(n) + + +if __name__ == "__main__": + print(solution(int(str(input()).strip()))) diff --git a/project_euler/problem_36/sol1.py b/project_euler/problem_36/sol1.py index d78e7e59f210..38b60420992b 100644 --- a/project_euler/problem_36/sol1.py +++ b/project_euler/problem_36/sol1.py @@ -1,30 +1,57 @@ -from __future__ import print_function -''' +""" Double-base palindromes Problem 36 The decimal number, 585 = 10010010012 (binary), is palindromic in both bases. -Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. +Find the sum of all numbers, less than one million, which are palindromic in +base 10 and base 2. -(Please note that the palindromic number, in either base, may not include leading zeros.) -''' +(Please note that the palindromic number, in either base, may not include +leading zeros.) +""" try: - xrange #Python 2 + xrange # Python 2 except NameError: - xrange = range #Python 3 + xrange = range # Python 3 + def is_palindrome(n): - n = str(n) + n = str(n) + + if n == n[::-1]: + return True + else: + return False + + +def solution(n): + """Return the sum of all numbers, less than n , which are palindromic in + base 10 and base 2. - if n == n[::-1]: - return True - else: - return False + >>> solution(1000000) + 872187 + >>> solution(500000) + 286602 + >>> solution(100000) + 286602 + >>> solution(1000) + 1772 + >>> solution(100) + 157 + >>> solution(10) + 25 + >>> solution(2) + 1 + >>> solution(1) + 0 + """ + total = 0 -total = 0 + for i in xrange(1, n): + if is_palindrome(i) and is_palindrome(bin(i).split("b")[1]): + total += i + return total -for i in xrange(1, 1000000): - if is_palindrome(i) and is_palindrome(bin(i).split('b')[1]): - total += i -print(total) \ No newline at end of file +if __name__ == "__main__": + print(solution(int(str(input().strip())))) diff --git a/project_euler/problem_40/sol1.py b/project_euler/problem_40/sol1.py index ab4017512a1a..accd7125354c 100644 --- a/project_euler/problem_40/sol1.py +++ b/project_euler/problem_40/sol1.py @@ -1,26 +1,47 @@ -#-.- coding: latin-1 -.- -from __future__ import print_function -''' +# -.- coding: latin-1 -.- +""" Champernowne's constant Problem 40 -An irrational decimal fraction is created by concatenating the positive integers: +An irrational decimal fraction is created by concatenating the positive +integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. -If dn represents the nth digit of the fractional part, find the value of the following expression. +If dn represents the nth digit of the fractional part, find the value of the +following expression. d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000 -''' +""" +from __future__ import print_function + + +def solution(): + """Returns + + >>> solution() + 210 + """ + constant = [] + i = 1 + + while len(constant) < 1e6: + constant.append(str(i)) + i += 1 -constant = [] -i = 1 + constant = "".join(constant) -while len(constant) < 1e6: - constant.append(str(i)) - i += 1 + return ( + int(constant[0]) + * int(constant[9]) + * int(constant[99]) + * int(constant[999]) + * int(constant[9999]) + * int(constant[99999]) + * int(constant[999999]) + ) -constant = ''.join(constant) -print(int(constant[0])*int(constant[9])*int(constant[99])*int(constant[999])*int(constant[9999])*int(constant[99999])*int(constant[999999])) \ No newline at end of file +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_48/sol1.py b/project_euler/problem_48/sol1.py index 5c4bdb0f6384..95af951c0e8a 100644 --- a/project_euler/problem_48/sol1.py +++ b/project_euler/problem_48/sol1.py @@ -1,21 +1,29 @@ -from __future__ import print_function -''' +""" Self Powers Problem 48 The series, 11 + 22 + 33 + ... + 1010 = 10405071317. Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000. -''' +""" try: - xrange + xrange except NameError: - xrange = range + xrange = range -total = 0 -for i in xrange(1, 1001): - total += i**i +def solution(): + """Returns the last 10 digits of the series, 11 + 22 + 33 + ... + 10001000. -print(str(total)[-10:]) \ No newline at end of file + >>> solution() + '9110846700' + """ + total = 0 + for i in xrange(1, 1001): + total += i ** i + return str(total)[-10:] + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_52/sol1.py b/project_euler/problem_52/sol1.py index 376b4cfa1d63..df5c46ae05d1 100644 --- a/project_euler/problem_52/sol1.py +++ b/project_euler/problem_52/sol1.py @@ -1,23 +1,37 @@ -from __future__ import print_function -''' +""" Permuted multiples Problem 52 -It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order. +It can be seen that the number, 125874, and its double, 251748, contain exactly +the same digits, but in a different order. -Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. -''' -i = 1 +Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, +contain the same digits. +""" -while True: - if sorted(list(str(i))) == \ - sorted(list(str(2*i))) == \ - sorted(list(str(3*i))) == \ - sorted(list(str(4*i))) == \ - sorted(list(str(5*i))) == \ - sorted(list(str(6*i))): - break - i += 1 +def solution(): + """Returns the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and + 6x, contain the same digits. -print(i) \ No newline at end of file + >>> solution() + 142857 + """ + i = 1 + + while True: + if ( + sorted(list(str(i))) + == sorted(list(str(2 * i))) + == sorted(list(str(3 * i))) + == sorted(list(str(4 * i))) + == sorted(list(str(5 * i))) + == sorted(list(str(6 * i))) + ): + return i + + i += 1 + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_53/sol1.py b/project_euler/problem_53/sol1.py index ed6d5329eb4e..c72e0b993a34 100644 --- a/project_euler/problem_53/sol1.py +++ b/project_euler/problem_53/sol1.py @@ -1,13 +1,11 @@ -#-.- coding: latin-1 -.- -from __future__ import print_function -from math import factorial -''' +# -.- coding: latin-1 -.- +""" Combinatoric selections Problem 53 There are exactly ten ways of selecting three from five, 12345: -123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 + 123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 In combinatorics, we use the notation, 5C3 = 10. @@ -16,21 +14,37 @@ nCr = n!/(r!(n−r)!),where r ≤ n, n! = n×(n−1)×...×3×2×1, and 0! = 1. It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066. -How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater than one-million? -''' +How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater +than one-million? +""" +from __future__ import print_function +from math import factorial + try: - xrange #Python 2 + xrange # Python 2 except NameError: - xrange = range #Python 3 + xrange = range # Python 3 + def combinations(n, r): - return factorial(n)/(factorial(r)*factorial(n-r)) + return factorial(n) / (factorial(r) * factorial(n - r)) + + +def solution(): + """Returns the number of values of nCr, for 1 ≤ n ≤ 100, are greater than + one-million + + >>> solution() + 4075 + """ + total = 0 -total = 0 + for i in xrange(1, 101): + for j in xrange(1, i + 1): + if combinations(i, j) > 1e6: + total += 1 + return total -for i in xrange(1, 101): - for j in xrange(1, i+1): - if combinations(i, j) > 1e6: - total += 1 -print(total) \ No newline at end of file +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_76/sol1.py b/project_euler/problem_76/sol1.py index 2832f6d7afb6..c9e3c452fbc4 100644 --- a/project_euler/problem_76/sol1.py +++ b/project_euler/problem_76/sol1.py @@ -1,5 +1,4 @@ -from __future__ import print_function -''' +""" Counting Summations Problem 76 @@ -12,24 +11,50 @@ 2 + 1 + 1 + 1 1 + 1 + 1 + 1 + 1 -How many different ways can one hundred be written as a sum of at least two positive integers? -''' +How many different ways can one hundred be written as a sum of at least two +positive integers? +""" +from __future__ import print_function + try: - xrange #Python 2 + xrange # Python 2 except NameError: - xrange = range #Python 3 + xrange = range # Python 3 + def partition(m): - memo = [[0 for _ in xrange(m)] for _ in xrange(m+1)] - for i in xrange(m+1): - memo[i][0] = 1 + """Returns the number of different ways one hundred can be written as a sum + of at least two positive integers. + + >>> partition(100) + 190569291 + >>> partition(50) + 204225 + >>> partition(30) + 5603 + >>> partition(10) + 41 + >>> partition(5) + 6 + >>> partition(3) + 2 + >>> partition(2) + 1 + >>> partition(1) + 0 + """ + memo = [[0 for _ in xrange(m)] for _ in xrange(m + 1)] + for i in xrange(m + 1): + memo[i][0] = 1 + + for n in xrange(m + 1): + for k in xrange(1, m): + memo[n][k] += memo[n][k - 1] + if n > k: + memo[n][k] += memo[n - k - 1][k] - for n in xrange(m+1): - for k in xrange(1, m): - memo[n][k] += memo[n][k-1] - if n > k: - memo[n][k] += memo[n-k-1][k] + return memo[m][m - 1] - 1 - return (memo[m][m-1] - 1) -print(partition(100)) \ No newline at end of file +if __name__ == "__main__": + print(partition(int(str(input()).strip()))) From b69db30e10931be401be04f07516d2d63ec27702 Mon Sep 17 00:00:00 2001 From: Bruno Date: Mon, 15 Jul 2019 23:50:33 -0300 Subject: [PATCH 08/21] Changed the way files are loaded to support pytest call. --- project_euler/problem_11/sol1.py | 4 +++- project_euler/problem_11/sol2.py | 4 +++- project_euler/problem_13/sol1.py | 4 +++- project_euler/problem_22/sol1.py | 6 +++++- project_euler/problem_22/sol2.py | 4 +++- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/project_euler/problem_11/sol1.py b/project_euler/problem_11/sol1.py index 6c1ae855dae2..1021b9f050b2 100644 --- a/project_euler/problem_11/sol1.py +++ b/project_euler/problem_11/sol1.py @@ -25,6 +25,7 @@ """ from __future__ import print_function +import os try: xrange # Python 2 @@ -85,7 +86,8 @@ def solution(): 70600674 """ grid = [] - with open("grid.txt") as file: + path = os.path.split(os.path.realpath(__file__)) + with open(path[0] + "/grid.txt") as file: for line in file: grid.append(line.strip("\n").split(" ")) diff --git a/project_euler/problem_11/sol2.py b/project_euler/problem_11/sol2.py index 7ed4a97d26fe..c78f1a846c44 100644 --- a/project_euler/problem_11/sol2.py +++ b/project_euler/problem_11/sol2.py @@ -25,6 +25,7 @@ """ from __future__ import print_function +import os try: xrange # Python 2 @@ -38,7 +39,8 @@ def solution(): >>> solution() 70600674 """ - with open("grid.txt", "r") as f: + path = os.path.split(os.path.realpath(__file__)) + with open(path[0] + "/grid.txt") as f: l = [] for i in xrange(20): l.append([int(x) for x in f.readline().split()]) diff --git a/project_euler/problem_13/sol1.py b/project_euler/problem_13/sol1.py index b7118a6150a7..3af0957d84f3 100644 --- a/project_euler/problem_13/sol1.py +++ b/project_euler/problem_13/sol1.py @@ -4,6 +4,7 @@ numbers. """ from __future__ import print_function +import os try: raw_input # Python 2 @@ -16,7 +17,8 @@ def solution(array): >>> sum = 0 >>> array = [] - >>> with open("num.txt",'r') as f: + >>> path = os.path.split(os.path.realpath(__file__)) + >>> with open(path[0] + "/num.txt","r") as f: ... for line in f: ... array.append(int(line)) ... diff --git a/project_euler/problem_22/sol1.py b/project_euler/problem_22/sol1.py index c27dd12c9681..faf1d42c2e44 100644 --- a/project_euler/problem_22/sol1.py +++ b/project_euler/problem_22/sol1.py @@ -15,6 +15,9 @@ What is the total of all the name scores in the file? """ +import os + + try: xrange # Python 2 except NameError: @@ -27,7 +30,8 @@ def solution(): >>> solution() 871198282 """ - with open("p022_names.txt") as file: + path = os.path.split(os.path.realpath(__file__)) + with open(path[0] + "/p022_names.txt") as file: names = str(file.readlines()[0]) names = names.replace('"', "").split(",") diff --git a/project_euler/problem_22/sol2.py b/project_euler/problem_22/sol2.py index bf55ee85fb0a..014d8c7550eb 100644 --- a/project_euler/problem_22/sol2.py +++ b/project_euler/problem_22/sol2.py @@ -15,6 +15,7 @@ What is the total of all the name scores in the file? """ +import os def solution(): @@ -25,7 +26,8 @@ def solution(): """ total_sum = 0 temp_sum = 0 - with open("p022_names.txt") as file: + path = os.path.split(os.path.realpath(__file__)) + with open(path[0] + "/p022_names.txt") as file: name = str(file.readlines()[0]) name = name.replace('"', "").split(",") From efa4bad73549179e0bf2865b1c9adabd33dec24e Mon Sep 17 00:00:00 2001 From: Bruno Date: Mon, 15 Jul 2019 23:56:55 -0300 Subject: [PATCH 09/21] Added __init__.py to problems to make them modules and allow pytest execution. --- project_euler/problem_01/__init__.py | 0 project_euler/problem_02/__init__.py | 0 project_euler/problem_03/__init__.py | 0 project_euler/problem_04/__init__.py | 0 project_euler/problem_05/__init__.py | 0 project_euler/problem_06/__init__.py | 0 project_euler/problem_07/__init__.py | 0 project_euler/problem_08/__init__.py | 0 project_euler/problem_09/__init__.py | 0 project_euler/problem_10/__init__.py | 0 project_euler/problem_11/__init__.py | 0 project_euler/problem_12/__init__.py | 0 project_euler/problem_13/__init__.py | 0 project_euler/problem_14/__init__.py | 0 project_euler/problem_15/__init__.py | 0 project_euler/problem_16/__init__.py | 0 project_euler/problem_17/__init__.py | 0 project_euler/problem_19/__init__.py | 0 project_euler/problem_20/__init__.py | 0 project_euler/problem_21/__init__.py | 0 project_euler/problem_22/__init__.py | 0 project_euler/problem_234/__init__.py | 0 project_euler/problem_24/__init__.py | 0 project_euler/problem_25/__init__.py | 0 project_euler/problem_28/__init__.py | 0 project_euler/problem_29/__init__.py | 0 project_euler/problem_31/__init__.py | 0 project_euler/problem_36/__init__.py | 0 project_euler/problem_40/__init__.py | 0 project_euler/problem_48/__init__.py | 0 project_euler/problem_52/__init__.py | 0 project_euler/problem_53/__init__.py | 0 project_euler/problem_76/__init__.py | 0 33 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 project_euler/problem_01/__init__.py create mode 100644 project_euler/problem_02/__init__.py create mode 100644 project_euler/problem_03/__init__.py create mode 100644 project_euler/problem_04/__init__.py create mode 100644 project_euler/problem_05/__init__.py create mode 100644 project_euler/problem_06/__init__.py create mode 100644 project_euler/problem_07/__init__.py create mode 100644 project_euler/problem_08/__init__.py create mode 100644 project_euler/problem_09/__init__.py create mode 100644 project_euler/problem_10/__init__.py create mode 100644 project_euler/problem_11/__init__.py create mode 100644 project_euler/problem_12/__init__.py create mode 100644 project_euler/problem_13/__init__.py create mode 100644 project_euler/problem_14/__init__.py create mode 100644 project_euler/problem_15/__init__.py create mode 100644 project_euler/problem_16/__init__.py create mode 100644 project_euler/problem_17/__init__.py create mode 100644 project_euler/problem_19/__init__.py create mode 100644 project_euler/problem_20/__init__.py create mode 100644 project_euler/problem_21/__init__.py create mode 100644 project_euler/problem_22/__init__.py create mode 100644 project_euler/problem_234/__init__.py create mode 100644 project_euler/problem_24/__init__.py create mode 100644 project_euler/problem_25/__init__.py create mode 100644 project_euler/problem_28/__init__.py create mode 100644 project_euler/problem_29/__init__.py create mode 100644 project_euler/problem_31/__init__.py create mode 100644 project_euler/problem_36/__init__.py create mode 100644 project_euler/problem_40/__init__.py create mode 100644 project_euler/problem_48/__init__.py create mode 100644 project_euler/problem_52/__init__.py create mode 100644 project_euler/problem_53/__init__.py create mode 100644 project_euler/problem_76/__init__.py diff --git a/project_euler/problem_01/__init__.py b/project_euler/problem_01/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_02/__init__.py b/project_euler/problem_02/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_03/__init__.py b/project_euler/problem_03/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_04/__init__.py b/project_euler/problem_04/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_05/__init__.py b/project_euler/problem_05/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_06/__init__.py b/project_euler/problem_06/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_07/__init__.py b/project_euler/problem_07/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_08/__init__.py b/project_euler/problem_08/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_09/__init__.py b/project_euler/problem_09/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_10/__init__.py b/project_euler/problem_10/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_11/__init__.py b/project_euler/problem_11/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_12/__init__.py b/project_euler/problem_12/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_13/__init__.py b/project_euler/problem_13/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_14/__init__.py b/project_euler/problem_14/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_15/__init__.py b/project_euler/problem_15/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_16/__init__.py b/project_euler/problem_16/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_17/__init__.py b/project_euler/problem_17/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_19/__init__.py b/project_euler/problem_19/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_20/__init__.py b/project_euler/problem_20/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_21/__init__.py b/project_euler/problem_21/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_22/__init__.py b/project_euler/problem_22/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_234/__init__.py b/project_euler/problem_234/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_24/__init__.py b/project_euler/problem_24/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_25/__init__.py b/project_euler/problem_25/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_28/__init__.py b/project_euler/problem_28/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_29/__init__.py b/project_euler/problem_29/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_31/__init__.py b/project_euler/problem_31/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_36/__init__.py b/project_euler/problem_36/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_40/__init__.py b/project_euler/problem_40/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_48/__init__.py b/project_euler/problem_48/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_52/__init__.py b/project_euler/problem_52/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_53/__init__.py b/project_euler/problem_53/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_76/__init__.py b/project_euler/problem_76/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 From abfee1048dd5773f38930a7faea748c182497d05 Mon Sep 17 00:00:00 2001 From: Bruno Date: Tue, 16 Jul 2019 00:09:43 -0300 Subject: [PATCH 10/21] Added project_euler folder to test units execution --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 9afc0c93a037..3b55045ac33f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,7 @@ script: matrix networking_flow other + project_euler searches sorts strings From aa8746654cbc9774021470b4416fb67bbe7af238 Mon Sep 17 00:00:00 2001 From: Bruno Date: Tue, 16 Jul 2019 14:46:58 -0300 Subject: [PATCH 11/21] Changed 'os.path.split(os.path.realpath(__file__))' to 'os.path.dirname()' --- project_euler/problem_11/sol1.py | 3 +-- project_euler/problem_11/sol2.py | 3 +-- project_euler/problem_13/sol1.py | 3 +-- project_euler/problem_22/sol1.py | 3 +-- project_euler/problem_22/sol2.py | 3 +-- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/project_euler/problem_11/sol1.py b/project_euler/problem_11/sol1.py index 1021b9f050b2..3bdddc89d917 100644 --- a/project_euler/problem_11/sol1.py +++ b/project_euler/problem_11/sol1.py @@ -86,8 +86,7 @@ def solution(): 70600674 """ grid = [] - path = os.path.split(os.path.realpath(__file__)) - with open(path[0] + "/grid.txt") as file: + with open(os.path.dirname(__file__) + "/grid.txt") as file: for line in file: grid.append(line.strip("\n").split(" ")) diff --git a/project_euler/problem_11/sol2.py b/project_euler/problem_11/sol2.py index c78f1a846c44..0a5785b42b2c 100644 --- a/project_euler/problem_11/sol2.py +++ b/project_euler/problem_11/sol2.py @@ -39,8 +39,7 @@ def solution(): >>> solution() 70600674 """ - path = os.path.split(os.path.realpath(__file__)) - with open(path[0] + "/grid.txt") as f: + with open(os.path.dirname(__file__) + "/grid.txt") as f: l = [] for i in xrange(20): l.append([int(x) for x in f.readline().split()]) diff --git a/project_euler/problem_13/sol1.py b/project_euler/problem_13/sol1.py index 3af0957d84f3..983347675b3f 100644 --- a/project_euler/problem_13/sol1.py +++ b/project_euler/problem_13/sol1.py @@ -17,8 +17,7 @@ def solution(array): >>> sum = 0 >>> array = [] - >>> path = os.path.split(os.path.realpath(__file__)) - >>> with open(path[0] + "/num.txt","r") as f: + >>> with open(os.path.dirname(__file__) + "/num.txt","r") as f: ... for line in f: ... array.append(int(line)) ... diff --git a/project_euler/problem_22/sol1.py b/project_euler/problem_22/sol1.py index faf1d42c2e44..aa779f222eaa 100644 --- a/project_euler/problem_22/sol1.py +++ b/project_euler/problem_22/sol1.py @@ -30,8 +30,7 @@ def solution(): >>> solution() 871198282 """ - path = os.path.split(os.path.realpath(__file__)) - with open(path[0] + "/p022_names.txt") as file: + with open(os.path.dirname(__file__) + "/p022_names.txt") as file: names = str(file.readlines()[0]) names = names.replace('"', "").split(",") diff --git a/project_euler/problem_22/sol2.py b/project_euler/problem_22/sol2.py index 014d8c7550eb..69acd2fb8ef3 100644 --- a/project_euler/problem_22/sol2.py +++ b/project_euler/problem_22/sol2.py @@ -26,8 +26,7 @@ def solution(): """ total_sum = 0 temp_sum = 0 - path = os.path.split(os.path.realpath(__file__)) - with open(path[0] + "/p022_names.txt") as file: + with open(os.path.dirname(__file__) + "/p022_names.txt") as file: name = str(file.readlines()[0]) name = name.replace('"', "").split(",") From c08c4b515ff7573fb179472f7bb4383e8c054acc Mon Sep 17 00:00:00 2001 From: Bruno Date: Tue, 16 Jul 2019 23:27:05 -0300 Subject: [PATCH 12/21] Added Burrows-Wheeler transform algorithm. --- compression/burrows_wheeler.py | 165 +++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 compression/burrows_wheeler.py diff --git a/compression/burrows_wheeler.py b/compression/burrows_wheeler.py new file mode 100644 index 000000000000..0f3156b1babc --- /dev/null +++ b/compression/burrows_wheeler.py @@ -0,0 +1,165 @@ +""" +https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform + +The Burrows–Wheeler transform (BWT, also called block-sorting compression) +rearranges a character string into runs of similar characters. This is useful +for compression, since it tends to be easy to compress a string that has runs +of repeated characters by techniques such as move-to-front transform and +run-length encoding. More importantly, the transformation is reversible, +without needing to store any additional data except the position of the first +original character. The BWT is thus a "free" method of improving the efficiency +of text compression algorithms, costing only some extra computation. +""" + + +def all_rotations(string): + """ + :param str string: The string that will be rotated len(string) times. + :return: A list with len(string) rotations of the parameter string. + :rtype: list[str] + :raises TypeError: If the string parameter type is not str. + Examples: + + >>> all_rotations("^BANANA|") + ['^BANANA|', 'BANANA|^', 'ANANA|^B', 'NANA|^BA', 'ANA|^BAN', 'NA|^BANA',\ + 'A|^BANAN', '|^BANANA'] + >>> all_rotations("a_asa_da_casa") + ['a_asa_da_casa', '_asa_da_casaa', 'asa_da_casaa_', 'sa_da_casaa_a',\ + 'a_da_casaa_as', '_da_casaa_asa', 'da_casaa_asa_', 'a_casaa_asa_d',\ + '_casaa_asa_da', 'casaa_asa_da_', 'asaa_asa_da_c', 'saa_asa_da_ca',\ + 'aa_asa_da_cas'] + >>> all_rotations("panamabanana") + ['panamabanana', 'anamabananap', 'namabananapa', 'amabananapan',\ + 'mabananapana', 'abananapanam', 'bananapanama', 'ananapanamab',\ + 'nanapanamaba', 'anapanamaban', 'napanamabana', 'apanamabanan'] + >>> all_rotations(5) + Traceback (most recent call last): + ... + TypeError: The parameter string type must be str. + """ + if not (type(string) is str): + raise TypeError("The parameter string type must be str.") + + return [string[i:] + string[:i] for i in range(len(string))] + + +def bwt_transform(string): + """ + :param str string: The string that will be used at bwt algorithm + :return: A dictionary with the bwt result, the string composed of the last + char of each row of the ordered rotations list and the index of the + original string at ordered rotations list + :rtype: dict + :raises TypeError: If the string parameter type is not str + :raises ValueError: If the string parameter is empty + Examples: + + >>> bwt_transform("^BANANA") + {'bwt_string': 'BNN^AAA', 'idx_original_string': 6} + >>> bwt_transform("a_asa_da_casa") + {'bwt_string': 'aaaadss_c__aa', 'idx_original_string': 3} + >>> bwt_transform("panamabanana") + {'bwt_string': 'mnpbnnaaaaaa', 'idx_original_string': 11} + >>> bwt_transform(4) + Traceback (most recent call last): + ... + TypeError: The parameter string type must be str. + >>> bwt_transform('') + Traceback (most recent call last): + ... + ValueError: The parameter string must not be empty. + """ + if not (type(string) is str): + raise TypeError("The parameter string type must be str.") + if not string: + raise ValueError("The parameter string must not be empty.") + + rotations = all_rotations(string) + rotations.sort() # sort the list of rotations in alphabetically order + # make a string composed of the last char of each rotation + return { + "bwt_string": "".join([word[-1] for word in rotations]), + "idx_original_string": rotations.index(string), + } + + +def reverse_bwt(bwt_string, idx_original_string): + """ + :param str bwt_string: The string returned from bwt algorithm execution + :param int idx_original_string: The index of the string that was used to + generate bwt_string at ordered rotations list + :return: The string used to generate bwt_string when bwt was executed + :rtype str + :raises TypeError: If the bwt_string parameter type is not str + :raises ValueError: If the bwt_string parameter is empty + :raises TypeError: If the idx_original_string type is not int or if not + possible to cast it to int + :raises ValueError: If the idx_original_string value is lower than 0 + + >>> reverse_bwt("BNN^AAA", 6) + '^BANANA' + >>> reverse_bwt("aaaadss_c__aa", 3) + 'a_asa_da_casa' + >>> reverse_bwt("mnpbnnaaaaaa", 11) + 'panamabanana' + >>> reverse_bwt(4, 11) + Traceback (most recent call last): + ... + TypeError: The parameter bwt_string type must be str. + >>> reverse_bwt("", 11) + Traceback (most recent call last): + ... + ValueError: The parameter bwt_string must not be empty. + >>> reverse_bwt("mnpbnnaaaaaa", "asd") + Traceback (most recent call last): + ... + TypeError: The parameter idx_original_string type must be int or passive of cast to int. + >>> reverse_bwt("mnpbnnaaaaaa", -1) + Traceback (most recent call last): + ... + ValueError: The parameter idx_original_string must not be lower than 0. + >>> reverse_bwt("mnpbnnaaaaaa", 11.0) + 'panamabanana' + >>> reverse_bwt("mnpbnnaaaaaa", 11.4) + 'panamabanana' + """ + if not (type(bwt_string) is str): + raise TypeError("The parameter bwt_string type must be str.") + if not bwt_string: + raise ValueError("The parameter bwt_string must not be empty.") + try: + idx_original_string = int(idx_original_string) + except ValueError: + raise TypeError( + "The parameter idx_original_string type must be int or passive of cast to int." + ) + if idx_original_string < 0: + raise ValueError( + "The parameter idx_original_string must not be lower than 0." + ) + + ordered_rotations = [""] * len(bwt_string) + for x in range(len(bwt_string)): + for i in range(len(bwt_string)): + ordered_rotations[i] = bwt_string[i] + ordered_rotations[i] + ordered_rotations.sort() + return ordered_rotations[idx_original_string] + + +if __name__ == "__main__": + string = input("Provide a string that I will generate its BWT transform: ") + result = bwt_transform(string) + print( + "Burrows Wheeler tranform for string '{}' results in '{}'".format( + string, result["bwt_string"] + ) + ) + original_string = reverse_bwt( + result["bwt_string"], result["idx_original_string"] + ) + print( + ( + "Reversing Burrows Wheeler tranform for entry '{}' we get original" + " string '{}'" + ).format(result["bwt_string"], original_string) + ) From cf3df564c4eea57b53fcb0e20cbb9992559599cb Mon Sep 17 00:00:00 2001 From: Bruno Date: Wed, 17 Jul 2019 15:15:34 -0300 Subject: [PATCH 13/21] Added changes suggested by cclauss --- compression/burrows_wheeler.py | 129 ++++++++++++++++++--------------- 1 file changed, 70 insertions(+), 59 deletions(-) diff --git a/compression/burrows_wheeler.py b/compression/burrows_wheeler.py index 0f3156b1babc..fabeab39adf8 100644 --- a/compression/burrows_wheeler.py +++ b/compression/burrows_wheeler.py @@ -10,48 +10,46 @@ original character. The BWT is thus a "free" method of improving the efficiency of text compression algorithms, costing only some extra computation. """ +from typing import List, Dict -def all_rotations(string): +def all_rotations(s: str) -> List[str]: """ - :param str string: The string that will be rotated len(string) times. - :return: A list with len(string) rotations of the parameter string. - :rtype: list[str] - :raises TypeError: If the string parameter type is not str. + :param s: The string that will be rotated len(s) times. + :return: A list with the rotations. + :raises TypeError: If s is not an instance of str. Examples: - >>> all_rotations("^BANANA|") - ['^BANANA|', 'BANANA|^', 'ANANA|^B', 'NANA|^BA', 'ANA|^BAN', 'NA|^BANA',\ - 'A|^BANAN', '|^BANANA'] - >>> all_rotations("a_asa_da_casa") - ['a_asa_da_casa', '_asa_da_casaa', 'asa_da_casaa_', 'sa_da_casaa_a',\ - 'a_da_casaa_as', '_da_casaa_asa', 'da_casaa_asa_', 'a_casaa_asa_d',\ - '_casaa_asa_da', 'casaa_asa_da_', 'asaa_asa_da_c', 'saa_asa_da_ca',\ - 'aa_asa_da_cas'] - >>> all_rotations("panamabanana") - ['panamabanana', 'anamabananap', 'namabananapa', 'amabananapan',\ - 'mabananapana', 'abananapanam', 'bananapanama', 'ananapanamab',\ - 'nanapanamaba', 'anapanamaban', 'napanamabana', 'apanamabanan'] + >>> all_rotations("^BANANA|") # doctest: +NORMALIZE_WHITESPACE + ['^BANANA|', 'BANANA|^', 'ANANA|^B', 'NANA|^BA', 'ANA|^BAN', 'NA|^BANA', + 'A|^BANAN', '|^BANANA'] + >>> all_rotations("a_asa_da_casa") # doctest: +NORMALIZE_WHITESPACE + ['a_asa_da_casa', '_asa_da_casaa', 'asa_da_casaa_', 'sa_da_casaa_a', + 'a_da_casaa_as', '_da_casaa_asa', 'da_casaa_asa_', 'a_casaa_asa_d', + '_casaa_asa_da', 'casaa_asa_da_', 'asaa_asa_da_c', 'saa_asa_da_ca', + 'aa_asa_da_cas'] + >>> all_rotations("panamabanana") # doctest: +NORMALIZE_WHITESPACE + ['panamabanana', 'anamabananap', 'namabananapa', 'amabananapan', + 'mabananapana', 'abananapanam', 'bananapanama', 'ananapanamab', + 'nanapanamaba', 'anapanamaban', 'napanamabana', 'apanamabanan'] >>> all_rotations(5) Traceback (most recent call last): ... - TypeError: The parameter string type must be str. + TypeError: The parameter s type must be str. """ - if not (type(string) is str): - raise TypeError("The parameter string type must be str.") + if not isinstance(s, str): + raise TypeError("The parameter s type must be str.") - return [string[i:] + string[:i] for i in range(len(string))] + return [s[i:] + s[:i] for i in range(len(s))] -def bwt_transform(string): +def bwt_transform(s: str) -> Dict: """ - :param str string: The string that will be used at bwt algorithm - :return: A dictionary with the bwt result, the string composed of the last - char of each row of the ordered rotations list and the index of the - original string at ordered rotations list - :rtype: dict - :raises TypeError: If the string parameter type is not str - :raises ValueError: If the string parameter is empty + :param s: The string that will be used at bwt algorithm + :return: the string composed of the last char of each row of the ordered + rotations and the index of the original string at ordered rotations list + :raises TypeError: If the s parameter type is not str + :raises ValueError: If the s parameter is empty Examples: >>> bwt_transform("^BANANA") @@ -63,38 +61,38 @@ def bwt_transform(string): >>> bwt_transform(4) Traceback (most recent call last): ... - TypeError: The parameter string type must be str. + TypeError: The parameter s type must be str. >>> bwt_transform('') Traceback (most recent call last): ... - ValueError: The parameter string must not be empty. + ValueError: The parameter s must not be empty. """ - if not (type(string) is str): - raise TypeError("The parameter string type must be str.") - if not string: - raise ValueError("The parameter string must not be empty.") + if not isinstance(s, str): + raise TypeError("The parameter s type must be str.") + if not s: + raise ValueError("The parameter s must not be empty.") - rotations = all_rotations(string) + rotations = all_rotations(s) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation return { "bwt_string": "".join([word[-1] for word in rotations]), - "idx_original_string": rotations.index(string), + "idx_original_string": rotations.index(s), } -def reverse_bwt(bwt_string, idx_original_string): +def reverse_bwt(bwt_string: str, idx_original_string: int) -> str: """ - :param str bwt_string: The string returned from bwt algorithm execution - :param int idx_original_string: The index of the string that was used to + :param bwt_string: The string returned from bwt algorithm execution + :param idx_original_string: A 0-based index of the string that was used to generate bwt_string at ordered rotations list :return: The string used to generate bwt_string when bwt was executed - :rtype str :raises TypeError: If the bwt_string parameter type is not str :raises ValueError: If the bwt_string parameter is empty :raises TypeError: If the idx_original_string type is not int or if not - possible to cast it to int - :raises ValueError: If the idx_original_string value is lower than 0 + possible to cast it to int + :raises ValueError: If the idx_original_string value is lower than 0 or + greater than len(bwt_string) - 1 >>> reverse_bwt("BNN^AAA", 6) '^BANANA' @@ -110,20 +108,26 @@ def reverse_bwt(bwt_string, idx_original_string): Traceback (most recent call last): ... ValueError: The parameter bwt_string must not be empty. - >>> reverse_bwt("mnpbnnaaaaaa", "asd") + >>> reverse_bwt("mnpbnnaaaaaa", "asd") # doctest: +NORMALIZE_WHITESPACE Traceback (most recent call last): ... - TypeError: The parameter idx_original_string type must be int or passive of cast to int. + TypeError: The parameter idx_original_string type must be int or passive + of cast to int. >>> reverse_bwt("mnpbnnaaaaaa", -1) Traceback (most recent call last): ... ValueError: The parameter idx_original_string must not be lower than 0. + >>> reverse_bwt("mnpbnnaaaaaa", 12) # doctest: +NORMALIZE_WHITESPACE + Traceback (most recent call last): + ... + ValueError: The parameter idx_original_string must be lower than + len(bwt_string). >>> reverse_bwt("mnpbnnaaaaaa", 11.0) 'panamabanana' >>> reverse_bwt("mnpbnnaaaaaa", 11.4) 'panamabanana' """ - if not (type(bwt_string) is str): + if not isinstance(bwt_string, str): raise TypeError("The parameter bwt_string type must be str.") if not bwt_string: raise ValueError("The parameter bwt_string must not be empty.") @@ -131,12 +135,22 @@ def reverse_bwt(bwt_string, idx_original_string): idx_original_string = int(idx_original_string) except ValueError: raise TypeError( - "The parameter idx_original_string type must be int or passive of cast to int." + ( + "The parameter idx_original_string type must be int or passive" + " of cast to int." + ) ) if idx_original_string < 0: raise ValueError( "The parameter idx_original_string must not be lower than 0." ) + if idx_original_string >= len(bwt_string): + raise ValueError( + ( + "The parameter idx_original_string must be lower than" + " len(bwt_string)." + ) + ) ordered_rotations = [""] * len(bwt_string) for x in range(len(bwt_string)): @@ -147,19 +161,16 @@ def reverse_bwt(bwt_string, idx_original_string): if __name__ == "__main__": - string = input("Provide a string that I will generate its BWT transform: ") - result = bwt_transform(string) - print( - "Burrows Wheeler tranform for string '{}' results in '{}'".format( - string, result["bwt_string"] - ) - ) + entry_msg = "Provide a string that I will generate its BWT transform: " + s = input(entry_msg).strip() + result = bwt_transform(s) + bwt_output_msg = "Burrows Wheeler tranform for string '{}' results in '{}'" + print(bwt_output_msg.format(s, result["bwt_string"])) original_string = reverse_bwt( result["bwt_string"], result["idx_original_string"] ) - print( - ( - "Reversing Burrows Wheeler tranform for entry '{}' we get original" - " string '{}'" - ).format(result["bwt_string"], original_string) + fmt = ( + "Reversing Burrows Wheeler tranform for entry '{}' we get original" + " string '{}'" ) + print(fmt.format(result["bwt_string"], original_string)) From c7c9eef7750e3c0d86bf4c65926925f8c95174a5 Mon Sep 17 00:00:00 2001 From: Bruno Date: Wed, 17 Jul 2019 21:14:46 -0300 Subject: [PATCH 14/21] Fixes for issue 'Fix the LGTM issues #1024'. --- project_euler/problem_02/sol4.py | 2 +- project_euler/problem_03/sol1.py | 7 +++++-- project_euler/problem_03/sol2.py | 1 - project_euler/problem_05/sol1.py | 1 - project_euler/problem_07/sol2.py | 1 - project_euler/problem_09/sol1.py | 1 - project_euler/problem_19/sol1.py | 2 +- project_euler/problem_234/sol1.py | 1 - 8 files changed, 7 insertions(+), 9 deletions(-) diff --git a/project_euler/problem_02/sol4.py b/project_euler/problem_02/sol4.py index ba13b12a15e9..2c4df93bbaee 100644 --- a/project_euler/problem_02/sol4.py +++ b/project_euler/problem_02/sol4.py @@ -11,7 +11,7 @@ """ from __future__ import print_function import math -from decimal import * +from decimal import Decimal, getcontext try: raw_input # Python 2 diff --git a/project_euler/problem_03/sol1.py b/project_euler/problem_03/sol1.py index c2e601bd0040..d5a5f7efcabf 100644 --- a/project_euler/problem_03/sol1.py +++ b/project_euler/problem_03/sol1.py @@ -28,18 +28,22 @@ def isprime(no): def solution(n): """Returns the largest prime factor of a given number n. - + >>> solution(13195) 29 >>> solution(10) 5 >>> solution(17) 17 + >>> solution(0) + -1 """ maxNumber = 0 if isprime(n): return n else: + if n <= 0: + return -1 while n % 2 == 0: n = n / 2 if isprime(n): @@ -54,7 +58,6 @@ def solution(n): elif isprime(i): maxNumber = i return maxNumber - return int(sum) if __name__ == "__main__": diff --git a/project_euler/problem_03/sol2.py b/project_euler/problem_03/sol2.py index 497db3965cc3..7e8a2199e7c0 100644 --- a/project_euler/problem_03/sol2.py +++ b/project_euler/problem_03/sol2.py @@ -6,7 +6,6 @@ e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. """ from __future__ import print_function, division -import math try: raw_input # Python 2 diff --git a/project_euler/problem_05/sol1.py b/project_euler/problem_05/sol1.py index 609f02102a08..bd27245db39e 100644 --- a/project_euler/problem_05/sol1.py +++ b/project_euler/problem_05/sol1.py @@ -39,7 +39,6 @@ def solution(n): if i == 0: i = 1 return i - break if __name__ == "__main__": diff --git a/project_euler/problem_07/sol2.py b/project_euler/problem_07/sol2.py index 630e5196796d..3dc0b1343eb7 100644 --- a/project_euler/problem_07/sol2.py +++ b/project_euler/problem_07/sol2.py @@ -7,7 +7,6 @@ We can see that the 6th prime is 13. What is the Nth prime number? """ from __future__ import print_function -from math import sqrt try: raw_input # Python 2 diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index 0f368e48d2e3..20dedb84bc0e 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -28,7 +28,6 @@ def solution(): if (a ** 2) + (b ** 2) == (c ** 2): if (a + b + c) == 1000: return a * b * c - break if __name__ == "__main__": diff --git a/project_euler/problem_19/sol1.py b/project_euler/problem_19/sol1.py index 6e4e29ec19c6..ab59365843b2 100644 --- a/project_euler/problem_19/sol1.py +++ b/project_euler/problem_19/sol1.py @@ -61,4 +61,4 @@ def solution(): if __name__ == "__main__": - print(solution(171)) + print(solution()) diff --git a/project_euler/problem_234/sol1.py b/project_euler/problem_234/sol1.py index 8298a7f8cce3..c0d2949285e9 100644 --- a/project_euler/problem_234/sol1.py +++ b/project_euler/problem_234/sol1.py @@ -40,7 +40,6 @@ def solution(n): semidivisible = [] for x in range(n): l=[i for i in input().split()] - c1=0 c2=1 while(1): if len(fib(l[0],l[1],c2)) Date: Thu, 18 Jul 2019 13:32:12 -0300 Subject: [PATCH 15/21] Added doctest for different parameter types and negative values. --- project_euler/problem_02/sol4.py | 24 ++++++++++++++++++++++++ project_euler/problem_03/sol1.py | 26 +++++++++++++++++++++++--- project_euler/problem_03/sol2.py | 26 +++++++++++++++++++++++++- project_euler/problem_05/sol1.py | 26 +++++++++++++++++++++++++- project_euler/problem_07/sol2.py | 24 ++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 5 deletions(-) diff --git a/project_euler/problem_02/sol4.py b/project_euler/problem_02/sol4.py index 2c4df93bbaee..930ce934dffa 100644 --- a/project_euler/problem_02/sol4.py +++ b/project_euler/problem_02/sol4.py @@ -33,7 +33,31 @@ def solution(n): 0 >>> solution(34) 44 + >>> solution(3.4) + 3 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. """ + try: + n = int(n) + except (TypeError, ValueError) as e: + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater or equal to one.") getcontext().prec = 100 phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2) diff --git a/project_euler/problem_03/sol1.py b/project_euler/problem_03/sol1.py index d5a5f7efcabf..ab19d8b30457 100644 --- a/project_euler/problem_03/sol1.py +++ b/project_euler/problem_03/sol1.py @@ -35,15 +35,35 @@ def solution(n): 5 >>> solution(17) 17 + >>> solution(3.4) + 3 >>> solution(0) - -1 + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. """ + try: + n = int(n) + except (TypeError, ValueError) as e: + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater or equal to one.") maxNumber = 0 if isprime(n): return n else: - if n <= 0: - return -1 while n % 2 == 0: n = n / 2 if isprime(n): diff --git a/project_euler/problem_03/sol2.py b/project_euler/problem_03/sol2.py index 7e8a2199e7c0..f93a0b75f4e0 100644 --- a/project_euler/problem_03/sol2.py +++ b/project_euler/problem_03/sol2.py @@ -15,14 +15,38 @@ def solution(n): """Returns the largest prime factor of a given number n. - + >>> solution(13195) 29 >>> solution(10) 5 >>> solution(17) 17 + >>> solution(3.4) + 3 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. """ + try: + n = int(n) + except (TypeError, ValueError) as e: + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater or equal to one.") prime = 1 i = 2 while i * i <= n: diff --git a/project_euler/problem_05/sol1.py b/project_euler/problem_05/sol1.py index bd27245db39e..e2deb91fb6aa 100644 --- a/project_euler/problem_05/sol1.py +++ b/project_euler/problem_05/sol1.py @@ -17,7 +17,7 @@ def solution(n): """Returns the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to n. - + >>> solution(10) 2520 >>> solution(15) @@ -26,7 +26,31 @@ def solution(n): 232792560 >>> solution(22) 232792560 + >>> solution(3.4) + 6 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. """ + try: + n = int(n) + except (TypeError, ValueError) as e: + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater or equal to one.") i = 0 while 1: i += n * (n - 1) diff --git a/project_euler/problem_07/sol2.py b/project_euler/problem_07/sol2.py index 3dc0b1343eb7..67336f7c1c96 100644 --- a/project_euler/problem_07/sol2.py +++ b/project_euler/problem_07/sol2.py @@ -36,7 +36,31 @@ def solution(n): 229 >>> solution(100) 541 + >>> solution(3.4) + 5 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. """ + try: + n = int(n) + except (TypeError, ValueError) as e: + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater or equal to one.") primes = [] num = 2 while len(primes) < n: From 176e8016978b6fe0d7acddb17fbb56031e0e92ad Mon Sep 17 00:00:00 2001 From: Bruno Date: Thu, 18 Jul 2019 13:57:29 -0300 Subject: [PATCH 16/21] Fixed doctest issue added at last commit. --- project_euler/problem_02/sol4.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_02/sol4.py b/project_euler/problem_02/sol4.py index 930ce934dffa..5e8c04899f3d 100644 --- a/project_euler/problem_02/sol4.py +++ b/project_euler/problem_02/sol4.py @@ -34,7 +34,7 @@ def solution(n): >>> solution(34) 44 >>> solution(3.4) - 3 + 2 >>> solution(0) Traceback (most recent call last): ... From 6c8a2144889f4b080c0cddd6d12a61a99ff1b3a5 Mon Sep 17 00:00:00 2001 From: Bruno Date: Thu, 18 Jul 2019 18:24:07 -0300 Subject: [PATCH 17/21] Commented doctest that were causing slowness at Travis. --- project_euler/problem_09/sol1.py | 4 ++-- project_euler/problem_09/sol3.py | 4 ++-- project_euler/problem_10/sol1.py | 4 ++-- project_euler/problem_10/sol2.py | 4 ++-- project_euler/problem_12/sol1.py | 4 ++-- project_euler/problem_12/sol2.py | 4 ++-- project_euler/problem_14/sol1.py | 4 ++-- project_euler/problem_14/sol2.py | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index 20dedb84bc0e..7eacd29bc806 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -18,8 +18,8 @@ def solution(): 2. a**2 + b**2 = c**2 3. a + b + c = 1000 - >>> solution() - 31875000 + #>>> solution() + #31875000 """ for a in range(300): for b in range(400): diff --git a/project_euler/problem_09/sol3.py b/project_euler/problem_09/sol3.py index f749b8a61f11..829ba84c4a77 100644 --- a/project_euler/problem_09/sol3.py +++ b/project_euler/problem_09/sol3.py @@ -21,8 +21,8 @@ def solution(): 1. a**2 + b**2 = c**2 2. a + b + c = 1000 - >>> solution() - 31875000 + #>>> solution() + #31875000 """ return [ a * b * c diff --git a/project_euler/problem_10/sol1.py b/project_euler/problem_10/sol1.py index 038da96e6352..0af123719695 100644 --- a/project_euler/problem_10/sol1.py +++ b/project_euler/problem_10/sol1.py @@ -42,8 +42,8 @@ def sum_of_primes(n): def solution(n): """Returns the sum of all the primes below n. - >>> solution(2000000) - 142913828922 + #>>> solution(2000000) + #142913828922 >>> solution(1000) 76127 >>> solution(5000) diff --git a/project_euler/problem_10/sol2.py b/project_euler/problem_10/sol2.py index 9e51d61b8749..d63e0aac9861 100644 --- a/project_euler/problem_10/sol2.py +++ b/project_euler/problem_10/sol2.py @@ -31,8 +31,8 @@ def prime_generator(): def solution(n): """Returns the sum of all the primes below n. - >>> solution(2000000) - 142913828922 + #>>> solution(2000000) + #142913828922 >>> solution(1000) 76127 >>> solution(5000) diff --git a/project_euler/problem_12/sol1.py b/project_euler/problem_12/sol1.py index baf9babab686..f84a89c0f2e4 100644 --- a/project_euler/problem_12/sol1.py +++ b/project_euler/problem_12/sol1.py @@ -45,8 +45,8 @@ def solution(): """Returns the value of the first triangle number to have over five hundred divisors. - >>> solution() - 76576500 + #>>> solution() + #76576500 """ tNum = 1 i = 1 diff --git a/project_euler/problem_12/sol2.py b/project_euler/problem_12/sol2.py index 071d7516ac0f..4447e3d4a49e 100644 --- a/project_euler/problem_12/sol2.py +++ b/project_euler/problem_12/sol2.py @@ -39,8 +39,8 @@ def solution(): """Returns the value of the first triangle number to have over five hundred divisors. - >>> solution() - 76576500 + #>>> solution() + #76576500 """ return next( i for i in triangle_number_generator() if count_divisors(i) > 500 diff --git a/project_euler/problem_14/sol1.py b/project_euler/problem_14/sol1.py index 6b80cd7cb24b..58cd61049d5d 100644 --- a/project_euler/problem_14/sol1.py +++ b/project_euler/problem_14/sol1.py @@ -30,8 +30,8 @@ def solution(n): n → n/2 (n is even) n → 3n + 1 (n is odd) - >>> solution(1000000) - {'counter': 525, 'largest_number': 837799} + #>>> solution(1000000) + #{'counter': 525, 'largest_number': 837799} >>> solution(200) {'counter': 125, 'largest_number': 171} >>> solution(5000) diff --git a/project_euler/problem_14/sol2.py b/project_euler/problem_14/sol2.py index 59fa79515148..94d290ed374f 100644 --- a/project_euler/problem_14/sol2.py +++ b/project_euler/problem_14/sol2.py @@ -47,8 +47,8 @@ def collatz_sequence(n): def solution(n): """Returns the number under n that generates the longest Collatz sequence. - >>> solution(1000000) - {'counter': 525, 'largest_number': 837799} + #>>> solution(1000000) + #{'counter': 525, 'largest_number': 837799} >>> solution(200) {'counter': 125, 'largest_number': 171} >>> solution(5000) From f0c9f210a52d25e72f21c799aa782d6c5265590b Mon Sep 17 00:00:00 2001 From: Bruno Date: Thu, 18 Jul 2019 19:30:08 -0300 Subject: [PATCH 18/21] Added comment with the reason for some doctest commented. --- project_euler/problem_09/sol1.py | 5 +++-- project_euler/problem_10/sol1.py | 5 +++-- project_euler/problem_10/sol2.py | 5 +++-- project_euler/problem_12/sol1.py | 5 +++-- project_euler/problem_12/sol2.py | 5 +++-- project_euler/problem_14/sol1.py | 5 +++-- project_euler/problem_14/sol2.py | 5 +++-- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py index 7eacd29bc806..d9ebe8760861 100644 --- a/project_euler/problem_09/sol1.py +++ b/project_euler/problem_09/sol1.py @@ -18,8 +18,9 @@ def solution(): 2. a**2 + b**2 = c**2 3. a + b + c = 1000 - #>>> solution() - #31875000 + # The code below has been commented due to slow execution affecting Travis. + # >>> solution() + # 31875000 """ for a in range(300): for b in range(400): diff --git a/project_euler/problem_10/sol1.py b/project_euler/problem_10/sol1.py index 0af123719695..49384d7c78f0 100644 --- a/project_euler/problem_10/sol1.py +++ b/project_euler/problem_10/sol1.py @@ -42,8 +42,9 @@ def sum_of_primes(n): def solution(n): """Returns the sum of all the primes below n. - #>>> solution(2000000) - #142913828922 + # The code below has been commented due to slow execution affecting Travis. + # >>> solution(2000000) + # 142913828922 >>> solution(1000) 76127 >>> solution(5000) diff --git a/project_euler/problem_10/sol2.py b/project_euler/problem_10/sol2.py index d63e0aac9861..451a4ae5e8f3 100644 --- a/project_euler/problem_10/sol2.py +++ b/project_euler/problem_10/sol2.py @@ -31,8 +31,9 @@ def prime_generator(): def solution(n): """Returns the sum of all the primes below n. - #>>> solution(2000000) - #142913828922 + # The code below has been commented due to slow execution affecting Travis. + # >>> solution(2000000) + # 142913828922 >>> solution(1000) 76127 >>> solution(5000) diff --git a/project_euler/problem_12/sol1.py b/project_euler/problem_12/sol1.py index f84a89c0f2e4..54476110b503 100644 --- a/project_euler/problem_12/sol1.py +++ b/project_euler/problem_12/sol1.py @@ -45,8 +45,9 @@ def solution(): """Returns the value of the first triangle number to have over five hundred divisors. - #>>> solution() - #76576500 + # The code below has been commented due to slow execution affecting Travis. + # >>> solution() + # 76576500 """ tNum = 1 i = 1 diff --git a/project_euler/problem_12/sol2.py b/project_euler/problem_12/sol2.py index 4447e3d4a49e..0d1502830bee 100644 --- a/project_euler/problem_12/sol2.py +++ b/project_euler/problem_12/sol2.py @@ -39,8 +39,9 @@ def solution(): """Returns the value of the first triangle number to have over five hundred divisors. - #>>> solution() - #76576500 + # The code below has been commented due to slow execution affecting Travis. + # >>> solution() + # 76576500 """ return next( i for i in triangle_number_generator() if count_divisors(i) > 500 diff --git a/project_euler/problem_14/sol1.py b/project_euler/problem_14/sol1.py index 58cd61049d5d..8d3efbc59eb5 100644 --- a/project_euler/problem_14/sol1.py +++ b/project_euler/problem_14/sol1.py @@ -30,8 +30,9 @@ def solution(n): n → n/2 (n is even) n → 3n + 1 (n is odd) - #>>> solution(1000000) - #{'counter': 525, 'largest_number': 837799} + # The code below has been commented due to slow execution affecting Travis. + # >>> solution(1000000) + # {'counter': 525, 'largest_number': 837799} >>> solution(200) {'counter': 125, 'largest_number': 171} >>> solution(5000) diff --git a/project_euler/problem_14/sol2.py b/project_euler/problem_14/sol2.py index 94d290ed374f..0ec80e221f09 100644 --- a/project_euler/problem_14/sol2.py +++ b/project_euler/problem_14/sol2.py @@ -47,8 +47,9 @@ def collatz_sequence(n): def solution(n): """Returns the number under n that generates the longest Collatz sequence. - #>>> solution(1000000) - #{'counter': 525, 'largest_number': 837799} + # The code below has been commented due to slow execution affecting Travis. + # >>> solution(1000000) + # {'counter': 525, 'largest_number': 837799} >>> solution(200) {'counter': 125, 'largest_number': 171} >>> solution(5000) From d9df3a8cc352dfe80f980b662803a71e3b393e11 Mon Sep 17 00:00:00 2001 From: cclauss Date: Fri, 19 Jul 2019 00:32:51 +0200 Subject: [PATCH 19/21] pytest --ignore --- .travis.yml | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6ac3010c5396..a3ff22fb09b7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,31 +9,19 @@ before_script: - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics script: - mypy --ignore-missing-imports . - #- IGNORE="data_structures,file_transfer_protocol,graphs,machine_learning,maths,neural_network,project_euler" - #- pytest . --doctest-modules --ignore=${IGNORE} - - pytest --doctest-modules - arithmetic_analysis - backtracking - boolean_algebra - ciphers - compression - conversions - digital_image_processing - divide_and_conquer - dynamic_programming - graphs - hashes - linear_algebra_python - matrix - networking_flow - neural_network - other - project_euler - searches - sorts - strings - traversals - + - pytest . --doctest-modules + --ignore=data_structures/stacks/balanced_parentheses.py + --ignore=data_structures/stacks/infix_to_postfix_conversion.py + --ignore=file_transfer_protocol/ftp_send_receive.py + --ignore=file_transfer_protocol/ftp_client_server.py + --ignore=machine_learning/linear_regression.py + --ignore=machine_learning/perceptron.py + --ignore=machine_learning/random_forest_classification/random_forest_classification.py + --ignore=machine_learning/random_forest_regression/random_forest_regression.py + --ignore=maths/abs_min.py + --ignore=maths/binary_exponentiation.py + --ignore=maths/lucas_series.py + --ignore=maths/sieve_of_eratosthenes.py after_success: - python scripts/build_directory_md.py - cat DIRECTORY.md From 454e2ff8c6c01881cbbcbcbb95fe2a4e7028ede9 Mon Sep 17 00:00:00 2001 From: Bruno Date: Thu, 18 Jul 2019 20:23:50 -0300 Subject: [PATCH 20/21] Added tests execution again. --- .travis.yml | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index a3ff22fb09b7..6ac3010c5396 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,19 +9,31 @@ before_script: - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics script: - mypy --ignore-missing-imports . - - pytest . --doctest-modules - --ignore=data_structures/stacks/balanced_parentheses.py - --ignore=data_structures/stacks/infix_to_postfix_conversion.py - --ignore=file_transfer_protocol/ftp_send_receive.py - --ignore=file_transfer_protocol/ftp_client_server.py - --ignore=machine_learning/linear_regression.py - --ignore=machine_learning/perceptron.py - --ignore=machine_learning/random_forest_classification/random_forest_classification.py - --ignore=machine_learning/random_forest_regression/random_forest_regression.py - --ignore=maths/abs_min.py - --ignore=maths/binary_exponentiation.py - --ignore=maths/lucas_series.py - --ignore=maths/sieve_of_eratosthenes.py + #- IGNORE="data_structures,file_transfer_protocol,graphs,machine_learning,maths,neural_network,project_euler" + #- pytest . --doctest-modules --ignore=${IGNORE} + - pytest --doctest-modules + arithmetic_analysis + backtracking + boolean_algebra + ciphers + compression + conversions + digital_image_processing + divide_and_conquer + dynamic_programming + graphs + hashes + linear_algebra_python + matrix + networking_flow + neural_network + other + project_euler + searches + sorts + strings + traversals + after_success: - python scripts/build_directory_md.py - cat DIRECTORY.md From 273a71ed3ac936bee8c4acd1706f203b26796494 Mon Sep 17 00:00:00 2001 From: Bruno Date: Thu, 18 Jul 2019 20:29:31 -0300 Subject: [PATCH 21/21] Had forgotten to add comment to file project_euler/problem_09/sol3.py --- project_euler/problem_09/sol3.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_09/sol3.py b/project_euler/problem_09/sol3.py index 829ba84c4a77..412a17206948 100644 --- a/project_euler/problem_09/sol3.py +++ b/project_euler/problem_09/sol3.py @@ -20,9 +20,9 @@ def solution(): 1. a**2 + b**2 = c**2 2. a + b + c = 1000 - - #>>> solution() - #31875000 + # The code below has been commented due to slow execution affecting Travis. + # >>> solution() + # 31875000 """ return [ a * b * c