From 8fe553d7e4825940c24af045fcc51e1eaa2ac8c2 Mon Sep 17 00:00:00 2001 From: Sanders Lin <45224617+SandersLin@users.noreply.github.com> Date: Thu, 6 Dec 2018 19:06:26 +0800 Subject: [PATCH 1/3] Create collatz_sequence.py --- collatz_sequence.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 collatz_sequence.py diff --git a/collatz_sequence.py b/collatz_sequence.py new file mode 100644 index 000000000000..7578e6b59ed8 --- /dev/null +++ b/collatz_sequence.py @@ -0,0 +1,25 @@ +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 + + +def main(): + n = 43 + sequence = collatz_sequence(n) + print(sequence) + print("collatz sequence from %d took %d steps."%(n,len(sequence))) + + + +if __name__ == '__main__': + main() From 838c7c98c551c9c45a17d1aac131869950c92c50 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 31 Jul 2019 23:04:34 +0800 Subject: [PATCH 2/3] Update and rename collatz_sequence.py to maths/collatz_sequence.py --- collatz_sequence.py | 25 ------------------------- maths/collatz_sequence.py | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 25 deletions(-) delete mode 100644 collatz_sequence.py create mode 100644 maths/collatz_sequence.py diff --git a/collatz_sequence.py b/collatz_sequence.py deleted file mode 100644 index 7578e6b59ed8..000000000000 --- a/collatz_sequence.py +++ /dev/null @@ -1,25 +0,0 @@ -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 - - -def main(): - n = 43 - sequence = collatz_sequence(n) - print(sequence) - print("collatz sequence from %d took %d steps."%(n,len(sequence))) - - - -if __name__ == '__main__': - main() diff --git a/maths/collatz_sequence.py b/maths/collatz_sequence.py new file mode 100644 index 000000000000..49b2345ceafd --- /dev/null +++ b/maths/collatz_sequence.py @@ -0,0 +1,25 @@ +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 + + +def main(): + n = 43 + sequence = collatz_sequence(n) + print(sequence) + print("collatz sequence from %d took %d steps."%(n,len(sequence))) + +if __name__ == '__main__': + main() From 38d459cb1eba49e2c8c8dd4bf5c68388d962e483 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 31 Jul 2019 23:09:03 +0800 Subject: [PATCH 3/3] doctest --- maths/collatz_sequence.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/maths/collatz_sequence.py b/maths/collatz_sequence.py index 49b2345ceafd..9f88453d518b 100644 --- a/maths/collatz_sequence.py +++ b/maths/collatz_sequence.py @@ -4,6 +4,9 @@ def collatz_sequence(n): 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. + Example: + >>> collatz_sequence(43) + [43, 130, 65, 196, 98, 49, 148, 74, 37, 112, 56, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] """ sequence = [n] while n != 1: