From b893b86ff3e33584f3214f2fdcabb6bdc83b39ca Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Sun, 19 Jul 2020 16:24:11 +0200 Subject: [PATCH 01/16] added a faster solution to project euler problem 14 --- project_euler/problem_14/sol3.py | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 project_euler/problem_14/sol3.py diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py new file mode 100755 index 000000000000..5cdebada04b1 --- /dev/null +++ b/project_euler/problem_14/sol3.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python + +""" +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? +""" + + +def solution(m): + """ 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} + """ + # we are going to avoid repeat computations by creating a knowledge base + # where we store the length of all collatz chains we calculated so far + + knowledge = {1: 1} + + # a single step in a collatz chain + def collatz(n): + if n % 2 == 0: + return n // 2 + else: + return 3 * n + 1 + + # calculates a collatz chain of a certain number this calculation is halted + # whenever we find a key with a know collatz chain in our knowledge base + def calculateChain(n): + entries = [] + while n not in knowledge: + entries.append(n) + n = collatz(n) + chainSize = knowledge[n] + for i in entries[::-1]: + chainSize += 1 + knowledge[i] = chainSize + + maxChain = { + "counter": 1, + "largest_number": 1 + } + for i in range(1, m + 1): + calculateChain(i) + if maxChain["counter"] < knowledge[i]: + maxChain = { + "counter": knowledge[i], + "largest_number": i + } + + return maxChain + + +if __name__ == "__main__": + print("calculate the number with the longest collatz chain in the range between 1 and the following number:") + inputNumber = int(input().strip()) + number, chainLength = solution(inputNumber) + print(f"the maximum collatz chain of all numbers between 1 and {inputNumber} is {chainLength}, starting with the number {number}") + From 9e6228203023f69057d1162261cdfe97d58fe6c5 Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Sun, 19 Jul 2020 16:41:33 +0200 Subject: [PATCH 02/16] removed blank line --- project_euler/problem_14/sol3.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py index 5cdebada04b1..e629b6ea2186 100755 --- a/project_euler/problem_14/sol3.py +++ b/project_euler/problem_14/sol3.py @@ -72,8 +72,13 @@ def calculateChain(n): if __name__ == "__main__": - print("calculate the number with the longest collatz chain in the range between 1 and the following number:") + print(""" + calculate the number with the longest collatz chain + in the range between 1 and the following number: + """) inputNumber = int(input().strip()) number, chainLength = solution(inputNumber) - print(f"the maximum collatz chain of all numbers between 1 and {inputNumber} is {chainLength}, starting with the number {number}") - + print(f""" + the maximum collatz chain of all numbers between 1 and {inputNumber} is + {chainLength}, starting with the number {number} + """) From 7efc16935467a8f9a655f0e92d529380a39f1c9e Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Sun, 19 Jul 2020 16:50:57 +0200 Subject: [PATCH 03/16] switched result type from dict to tuple --- project_euler/problem_14/sol3.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py index e629b6ea2186..881a2d26e84f 100755 --- a/project_euler/problem_14/sol3.py +++ b/project_euler/problem_14/sol3.py @@ -24,13 +24,13 @@ def solution(m): """ Returns the number under n that generates the longest Collatz sequence. >>> solution(1000000) - {'counter': 525, 'largest_number': 837799} + (837799, 525) >>> solution(200) - {'counter': 125, 'largest_number': 171} + (171, 125) >>> solution(5000) - {'counter': 238, 'largest_number': 3711} + (3711, 238) >>> solution(15000) - {'counter': 276, 'largest_number': 13255} + (13255, 276) """ # we are going to avoid repeat computations by creating a knowledge base # where we store the length of all collatz chains we calculated so far @@ -56,17 +56,14 @@ def calculateChain(n): chainSize += 1 knowledge[i] = chainSize - maxChain = { - "counter": 1, - "largest_number": 1 - } + maxChain = (1, 1) for i in range(1, m + 1): calculateChain(i) - if maxChain["counter"] < knowledge[i]: - maxChain = { - "counter": knowledge[i], - "largest_number": i - } + # we can use knowledge[i] because calculateChain + # by definition already adds the key we specified to the + # knowledge base + if maxChain[1] < knowledge[i]: + maxChain = (i, knowledge[i]) return maxChain From 08fe4a947e830952ec24fddea89b1976413110f2 Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Sun, 19 Jul 2020 16:58:42 +0200 Subject: [PATCH 04/16] fixed the naming of python variables --- project_euler/problem_14/sol3.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py index 881a2d26e84f..ab86ce563da4 100755 --- a/project_euler/problem_14/sol3.py +++ b/project_euler/problem_14/sol3.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from typings import Tuple """ Collatz conjecture: start with any positive integer n. Next term obtained from @@ -20,7 +21,7 @@ """ -def solution(m): +def solution(m: int) -> Tuple(int, int): """ Returns the number under n that generates the longest Collatz sequence. >>> solution(1000000) @@ -46,26 +47,26 @@ def collatz(n): # calculates a collatz chain of a certain number this calculation is halted # whenever we find a key with a know collatz chain in our knowledge base - def calculateChain(n): + def calculate_chain(n): entries = [] while n not in knowledge: entries.append(n) n = collatz(n) - chainSize = knowledge[n] + chain_size = knowledge[n] for i in entries[::-1]: - chainSize += 1 - knowledge[i] = chainSize + chain_size += 1 + knowledge[i] = chain_size - maxChain = (1, 1) + max_chain = (1, 1) for i in range(1, m + 1): - calculateChain(i) - # we can use knowledge[i] because calculateChain + calculate_chain(i) + # we can use knowledge[i] because calculate_chain # by definition already adds the key we specified to the # knowledge base - if maxChain[1] < knowledge[i]: - maxChain = (i, knowledge[i]) + if max_chain[1] < knowledge[i]: + max_chain = (i, knowledge[i]) - return maxChain + return max_chain if __name__ == "__main__": @@ -73,9 +74,9 @@ def calculateChain(n): calculate the number with the longest collatz chain in the range between 1 and the following number: """) - inputNumber = int(input().strip()) - number, chainLength = solution(inputNumber) + input_number = int(input().strip()) + number, chain_length = solution(input_number) print(f""" - the maximum collatz chain of all numbers between 1 and {inputNumber} is - {chainLength}, starting with the number {number} + the maximum collatz chain of all numbers between 1 and {input_number} is + {chain_length}, starting with the number {number} """) From 0cbc3c2a21544f39b1b581e38f59de6e4631942c Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Sun, 19 Jul 2020 17:07:24 +0200 Subject: [PATCH 05/16] fixed typos It's been a while since I worked with typed python, forgive me ok? --- project_euler/problem_14/sol3.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py index ab86ce563da4..1d88bc87f594 100755 --- a/project_euler/problem_14/sol3.py +++ b/project_euler/problem_14/sol3.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from typings import Tuple +from typing import Tuple """ Collatz conjecture: start with any positive integer n. Next term obtained from @@ -21,7 +21,7 @@ """ -def solution(m: int) -> Tuple(int, int): +def solution(m: int) -> Tuple[int, int]: """ Returns the number under n that generates the longest Collatz sequence. >>> solution(1000000) From 79391d86600fc8ed90afe331bd641c28c9cdcd42 Mon Sep 17 00:00:00 2001 From: vinayak Date: Mon, 20 Jul 2020 22:39:31 +0530 Subject: [PATCH 06/16] Update sol3.py --- project_euler/problem_14/sol3.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py index 1d88bc87f594..eef6c5470f59 100755 --- a/project_euler/problem_14/sol3.py +++ b/project_euler/problem_14/sol3.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python from typing import Tuple """ From e65bb4ff86cfd7b211fb6f247c7a9462e67b40df Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Mon, 20 Jul 2020 22:19:45 +0200 Subject: [PATCH 07/16] un-nested helper functions --- project_euler/problem_14/sol3.py | 50 +++++++++++++++++--------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py index eef6c5470f59..23a42cf25de5 100755 --- a/project_euler/problem_14/sol3.py +++ b/project_euler/problem_14/sol3.py @@ -19,6 +19,32 @@ Which starting number, under one million, produces the longest chain? """ +# we are going to avoid repeat computations by creating a knowledge base +# where we store the length of all collatz chains we calculated so far + +knowledge = {1: 1} + + +# a single step in a collatz chain +def collatz(n): + if n % 2 == 0: + return n // 2 + else: + return 3 * n + 1 + + +# calculates a collatz chain of a certain number this calculation is halted +# whenever we find a key with a know collatz chain in our knowledge base +def calculate_chain(n): + entries = [] + while n not in knowledge: + entries.append(n) + n = collatz(n) + chain_size = knowledge[n] + for i in entries[::-1]: + chain_size += 1 + knowledge[i] = chain_size + def solution(m: int) -> Tuple[int, int]: """ Returns the number under n that generates the longest Collatz sequence. @@ -32,30 +58,6 @@ def solution(m: int) -> Tuple[int, int]: >>> solution(15000) (13255, 276) """ - # we are going to avoid repeat computations by creating a knowledge base - # where we store the length of all collatz chains we calculated so far - - knowledge = {1: 1} - - # a single step in a collatz chain - def collatz(n): - if n % 2 == 0: - return n // 2 - else: - return 3 * n + 1 - - # calculates a collatz chain of a certain number this calculation is halted - # whenever we find a key with a know collatz chain in our knowledge base - def calculate_chain(n): - entries = [] - while n not in knowledge: - entries.append(n) - n = collatz(n) - chain_size = knowledge[n] - for i in entries[::-1]: - chain_size += 1 - knowledge[i] = chain_size - max_chain = (1, 1) for i in range(1, m + 1): calculate_chain(i) From 20786f59f8db010e3a5ac024b7416ca0bec4b7e7 Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Mon, 20 Jul 2020 22:22:11 +0200 Subject: [PATCH 08/16] added typehints to all functions --- project_euler/problem_14/sol3.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py index 23a42cf25de5..536205d9e69f 100755 --- a/project_euler/problem_14/sol3.py +++ b/project_euler/problem_14/sol3.py @@ -26,7 +26,7 @@ # a single step in a collatz chain -def collatz(n): +def collatz(n: int) -> int: if n % 2 == 0: return n // 2 else: @@ -35,7 +35,7 @@ def collatz(n): # calculates a collatz chain of a certain number this calculation is halted # whenever we find a key with a know collatz chain in our knowledge base -def calculate_chain(n): +def calculate_chain(n: int) -> None: entries = [] while n not in knowledge: entries.append(n) From 2118f31946af92d0043817841ee0b325206b5bab Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Mon, 20 Jul 2020 22:36:53 +0200 Subject: [PATCH 09/16] added doctests I am not sure it the calculate_chain doctest works, since they are testing the side effects of the function, we will see what CI thinks of it --- project_euler/problem_14/sol3.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py index 536205d9e69f..69bfd1fb6fe1 100755 --- a/project_euler/problem_14/sol3.py +++ b/project_euler/problem_14/sol3.py @@ -27,15 +27,38 @@ # a single step in a collatz chain def collatz(n: int) -> int: + """ calculates a single step in a collatz chain + + >>> collatz(13) + 40 + >>> collatz(40) + 20 + >>> collatz(5) + 16 + """ if n % 2 == 0: return n // 2 else: return 3 * n + 1 -# calculates a collatz chain of a certain number this calculation is halted -# whenever we find a key with a know collatz chain in our knowledge base def calculate_chain(n: int) -> None: + """ + calculates a collatz chain of a certain number this calculation is halted + whenever we find a key with a know collatz chain in our knowledge base + + >>> knowledge = {1: 1} + >>> calculate_chain(13) + >>> knowledge + {1: 1, 2: 2, 4: 3, 8: 4, 16: 5, 5: 6, 10: 7, 20: 8, 40: 9, 13: 10} + >>> calculate_chain(8) + >>> knowledge + {1: 1, 2: 2, 4: 3, 8: 4, 16: 5, 5: 6, 10: 7, 20: 8, 40: 9, 13: 10} + >>> knowledge = {1: 1} + >>> calculate_chain(12) + >>> knowledge + {1: 1, 2: 2, 4: 3, 8: 4, 16: 5, 5: 6, 10: 7, 3: 8, 6: 9, 12: 10} + """ entries = [] while n not in knowledge: entries.append(n) From 12d20ff43aad25e04138f8177ed52bef896b6ddb Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Mon, 20 Jul 2020 22:38:49 +0200 Subject: [PATCH 10/16] added the shebang back noone gave a good reason on why to remove it, and since this file is executable anyway (and thus easier to run for me) --- project_euler/problem_14/sol3.py | 1 + 1 file changed, 1 insertion(+) diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py index 69bfd1fb6fe1..1fb2de73209e 100755 --- a/project_euler/problem_14/sol3.py +++ b/project_euler/problem_14/sol3.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python from typing import Tuple """ From b45a9d0b4b82c987f3c426ab9f3b0991474b3a49 Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Mon, 20 Jul 2020 22:51:09 +0200 Subject: [PATCH 11/16] removed doctests from calculate_chain doctests reset the environment after each function evaluation, so we can't use doctests to inspect side effects of void functions --- project_euler/problem_14/sol3.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/project_euler/problem_14/sol3.py b/project_euler/problem_14/sol3.py index 1fb2de73209e..c230fe5e3337 100755 --- a/project_euler/problem_14/sol3.py +++ b/project_euler/problem_14/sol3.py @@ -28,7 +28,7 @@ # a single step in a collatz chain def collatz(n: int) -> int: - """ calculates a single step in a collatz chain + """ Calculates a single step in a collatz chain. >>> collatz(13) 40 @@ -45,20 +45,10 @@ def collatz(n: int) -> int: def calculate_chain(n: int) -> None: """ - calculates a collatz chain of a certain number this calculation is halted - whenever we find a key with a know collatz chain in our knowledge base - - >>> knowledge = {1: 1} - >>> calculate_chain(13) - >>> knowledge - {1: 1, 2: 2, 4: 3, 8: 4, 16: 5, 5: 6, 10: 7, 20: 8, 40: 9, 13: 10} - >>> calculate_chain(8) - >>> knowledge - {1: 1, 2: 2, 4: 3, 8: 4, 16: 5, 5: 6, 10: 7, 20: 8, 40: 9, 13: 10} - >>> knowledge = {1: 1} - >>> calculate_chain(12) - >>> knowledge - {1: 1, 2: 2, 4: 3, 8: 4, 16: 5, 5: 6, 10: 7, 3: 8, 6: 9, 12: 10} + Calculates a collatz chain of a certain number. This calculation is halted + whenever we find a key with a known collatz chain in our knowledge base. + This function has no return value and doctests can't measure side effects, + so we can't do doctests on this function. """ entries = [] while n not in knowledge: From 2c98623737503f20c2b2d6b45382b7df7d79d8de Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 12 Nov 2020 12:15:05 +0000 Subject: [PATCH 12/16] updating DIRECTORY.md --- DIRECTORY.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index fd45eacaad9b..136d25c80fcd 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -206,8 +206,10 @@ * [Heaps Algorithm](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/heaps_algorithm.py) * [Heaps Algorithm Iterative](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/heaps_algorithm_iterative.py) * [Inversions](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/inversions.py) + * [Kth Order Statistic](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/kth_order_statistic.py) * [Max Subarray Sum](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/max_subarray_sum.py) * [Mergesort](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/mergesort.py) + * [Peak](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/peak.py) * [Power](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/power.py) * [Strassen Matrix Multiplication](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/strassen_matrix_multiplication.py) @@ -390,6 +392,7 @@ * [Chudnovsky Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/chudnovsky_algorithm.py) * [Collatz Sequence](https://github.com/TheAlgorithms/Python/blob/master/maths/collatz_sequence.py) * [Combinations](https://github.com/TheAlgorithms/Python/blob/master/maths/combinations.py) + * [Decimal Isolate](https://github.com/TheAlgorithms/Python/blob/master/maths/decimal_isolate.py) * [Entropy](https://github.com/TheAlgorithms/Python/blob/master/maths/entropy.py) * [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py) * [Explicit Euler](https://github.com/TheAlgorithms/Python/blob/master/maths/explicit_euler.py) @@ -493,6 +496,7 @@ * [Autocomplete Using Trie](https://github.com/TheAlgorithms/Python/blob/master/other/autocomplete_using_trie.py) * [Binary Exponentiation](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation.py) * [Binary Exponentiation 2](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation_2.py) + * [Davis–Putnam–Logemann–Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davis–putnam–logemann–loveland.py) * [Detecting English Programmatically](https://github.com/TheAlgorithms/Python/blob/master/other/detecting_english_programmatically.py) * [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py) * [Doomsday](https://github.com/TheAlgorithms/Python/blob/master/other/doomsday.py) @@ -582,6 +586,7 @@ * Problem 014 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_014/sol1.py) * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_014/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_014/sol3.py) * Problem 015 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_015/sol1.py) * Problem 016 @@ -660,6 +665,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_048/sol1.py) * Problem 049 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_049/sol1.py) + * Problem 050 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_050/sol1.py) * Problem 051 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_051/sol1.py) * Problem 052 @@ -681,6 +688,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_062/sol1.py) * Problem 063 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_063/sol1.py) + * Problem 064 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_064/sol1.py) * Problem 065 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_065/sol1.py) * Problem 067 @@ -694,6 +703,7 @@ * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_072/sol2.py) * Problem 074 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_074/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_074/sol2.py) * Problem 075 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_075/sol1.py) * Problem 076 @@ -718,6 +728,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_119/sol1.py) * Problem 120 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_120/sol1.py) + * Problem 123 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_123/sol1.py) * Problem 125 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_125/sol1.py) * Problem 173 @@ -726,12 +738,16 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_174/sol1.py) * Problem 191 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_191/sol1.py) + * Problem 203 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_203/sol1.py) * Problem 206 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_206/sol1.py) * Problem 207 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_207/sol1.py) * Problem 234 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_234/sol1.py) + * Problem 301 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_301/sol1.py) * Problem 551 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py) @@ -740,6 +756,7 @@ * [Half Adder](https://github.com/TheAlgorithms/Python/blob/master/quantum/half_adder.py) * [Not Gate](https://github.com/TheAlgorithms/Python/blob/master/quantum/not_gate.py) * [Quantum Entanglement](https://github.com/TheAlgorithms/Python/blob/master/quantum/quantum_entanglement.py) + * [Ripple Adder Classic](https://github.com/TheAlgorithms/Python/blob/master/quantum/ripple_adder_classic.py) * [Single Qubit Measure](https://github.com/TheAlgorithms/Python/blob/master/quantum/single_qubit_measure.py) ## Scheduling From d2924d7b9fdf2db6db29892e9fab50de03cf84b6 Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Thu, 12 Nov 2020 13:17:01 +0100 Subject: [PATCH 13/16] validate_solutions wants a default parameter for the function --- project_euler/problem_014/sol3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_014/sol3.py b/project_euler/problem_014/sol3.py index c230fe5e3337..10530d8dfb05 100755 --- a/project_euler/problem_014/sol3.py +++ b/project_euler/problem_014/sol3.py @@ -60,7 +60,7 @@ def calculate_chain(n: int) -> None: knowledge[i] = chain_size -def solution(m: int) -> Tuple[int, int]: +def solution(m: int = 1000000) -> Tuple[int, int]: """ Returns the number under n that generates the longest Collatz sequence. >>> solution(1000000) From 661728fa410af4d1e369ce1cf8b3b02732f29d8a Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Thu, 12 Nov 2020 16:01:44 +0100 Subject: [PATCH 14/16] Changed answer format Turns out that we don't do any doctests here anymore, we just write our oWN FRAMEWORK NOW DO WE?! --- project_euler/problem_014/sol3.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_014/sol3.py b/project_euler/problem_014/sol3.py index 10530d8dfb05..d359b964c6e4 100755 --- a/project_euler/problem_014/sol3.py +++ b/project_euler/problem_014/sol3.py @@ -60,7 +60,7 @@ def calculate_chain(n: int) -> None: knowledge[i] = chain_size -def solution(m: int = 1000000) -> Tuple[int, int]: +def solution(m: int = 1000000, extra_info: bool = False) -> Tuple[int, int]: """ Returns the number under n that generates the longest Collatz sequence. >>> solution(1000000) @@ -81,7 +81,10 @@ def solution(m: int = 1000000) -> Tuple[int, int]: if max_chain[1] < knowledge[i]: max_chain = (i, knowledge[i]) - return max_chain + if extra_info: + return max_chain + else: + return max_chain[0] if __name__ == "__main__": @@ -90,7 +93,7 @@ def solution(m: int = 1000000) -> Tuple[int, int]: in the range between 1 and the following number: """) input_number = int(input().strip()) - number, chain_length = solution(input_number) + number, chain_length = solution(input_number, True) print(f""" the maximum collatz chain of all numbers between 1 and {input_number} is {chain_length}, starting with the number {number} From 33ab2a13539dd19601a4fd5840c4ae61c4fdebf3 Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Thu, 12 Nov 2020 20:52:29 +0100 Subject: [PATCH 15/16] Fixed doctests --- project_euler/problem_014/sol3.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_014/sol3.py b/project_euler/problem_014/sol3.py index d359b964c6e4..59a7c1e1d2b9 100755 --- a/project_euler/problem_014/sol3.py +++ b/project_euler/problem_014/sol3.py @@ -63,13 +63,13 @@ def calculate_chain(n: int) -> None: def solution(m: int = 1000000, extra_info: bool = False) -> Tuple[int, int]: """ Returns the number under n that generates the longest Collatz sequence. - >>> solution(1000000) + >>> solution(1000000, True) (837799, 525) - >>> solution(200) + >>> solution(200, True) (171, 125) - >>> solution(5000) + >>> solution(5000, True) (3711, 238) - >>> solution(15000) + >>> solution(15000, True) (13255, 276) """ max_chain = (1, 1) From bc8c526f93e3114e3489c12d8cb56267196da0a1 Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Fri, 13 Nov 2020 09:15:22 +0100 Subject: [PATCH 16/16] Fixed some pre-commit problems --- project_euler/problem_014/sol3.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_014/sol3.py b/project_euler/problem_014/sol3.py index 59a7c1e1d2b9..2ae43cae30eb 100755 --- a/project_euler/problem_014/sol3.py +++ b/project_euler/problem_014/sol3.py @@ -28,7 +28,8 @@ # a single step in a collatz chain def collatz(n: int) -> int: - """ Calculates a single step in a collatz chain. + """ + Calculates a single step in a collatz chain. >>> collatz(13) 40 @@ -61,7 +62,8 @@ def calculate_chain(n: int) -> None: def solution(m: int = 1000000, extra_info: bool = False) -> Tuple[int, int]: - """ Returns the number under n that generates the longest Collatz sequence. + """ + Returns the number under n that generates the longest Collatz sequence. >>> solution(1000000, True) (837799, 525) @@ -88,13 +90,17 @@ def solution(m: int = 1000000, extra_info: bool = False) -> Tuple[int, int]: if __name__ == "__main__": - print(""" + print( + """ calculate the number with the longest collatz chain in the range between 1 and the following number: - """) + """ + ) input_number = int(input().strip()) number, chain_length = solution(input_number, True) - print(f""" + print( + f""" the maximum collatz chain of all numbers between 1 and {input_number} is {chain_length}, starting with the number {number} - """) + """ + )