From ec1ef3778352020b4ef9408bff672d00aaaec0ed Mon Sep 17 00:00:00 2001 From: DontEatThemCookies Date: Sun, 2 Jan 2022 15:52:27 +0800 Subject: [PATCH 1/2] Revision 1 of my Collatz-Conjecture program. --- Collatz-Conjecture.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Collatz-Conjecture.py b/Collatz-Conjecture.py index a96fa71bb8e..8a4d355d7e5 100644 --- a/Collatz-Conjecture.py +++ b/Collatz-Conjecture.py @@ -9,7 +9,7 @@ 3x + 1 problem, is a mathematical conjecture concerning a certain sequence. This sequence operates on any input number in such a way -hat the output will always reach 1. +that the output will always reach 1. The Collatz conjecture is most famous for harboring one of the unsolved problems in From 4a16584ea0bb14558216457657685d6be0508d91 Mon Sep 17 00:00:00 2001 From: DontEatThemCookies Date: Sun, 2 Jan 2022 16:01:21 +0800 Subject: [PATCH 2/2] Finalized Revision 1 of the Collatz-Conjecture program --- Collatz-Conjecture.py | 170 ++++++++++++++++++++++-------------------- 1 file changed, 88 insertions(+), 82 deletions(-) diff --git a/Collatz-Conjecture.py b/Collatz-Conjecture.py index 8a4d355d7e5..c5da7b775a1 100644 --- a/Collatz-Conjecture.py +++ b/Collatz-Conjecture.py @@ -1,83 +1,89 @@ -#!/usr/bin/env python3 - -# Recommended: Python 3.6+ - -""" -Collatz Conjecture - Python - -The Collatz conjecture, also known as the -3x + 1 problem, is a mathematical conjecture -concerning a certain sequence. This sequence -operates on any input number in such a way +#!/usr/bin/env python3 + +# Recommended: Python 3.6+ + +""" +Collatz Conjecture - Python + +The Collatz conjecture, also known as the +3x + 1 problem, is a mathematical conjecture +concerning a certain sequence. This sequence +operates on any input number in such a way that the output will always reach 1. - -The Collatz conjecture is most famous for -harboring one of the unsolved problems in -mathematics: does the Collatz sequence really -reach 1 for all positive integers? - -This program takes any input integer -and performs a Collatz sequence on them. -The expected behavior is that any number -inputted will always reach a 4-2-1 loop. - -Do note that Python is limited in terms of -number size, so any enormous numbers may be -interpreted as infinity, and therefore -incalculable, by Python. This limitation -was only observed in CPython, so other -implementations may or may not differ. - -11/24/2021 -David Costell (DontEatThemCookies on GitHub) -""" - -import math - -print("Collatz Conjecture") -number = input('Enter a number to calculate: ') -try: - number = float(number) -except: - print('Error: Could not convert to integer.') - print('Only integers/floats can be entered as input.') - input() - exit() - -# Checks to see if input is valid -if number == 0: - input('Error: Zero is not calculable. ') - exit() -if number < 0: - input('Error: Negative numbers are not calculable. ') - exit() -if number == math.inf: - input('Error: Infinity is not calculable.') - exit() - -print('Number is', number) -input('Press ENTER to begin.') -print('BEGIN COLLATZ SEQUENCE') - -def modulo(): - global number - modulo = number % 2 # Modulo the number by 2 - if modulo == 0: # If the result is 0, - number = number / 2 # divide it by 2 - else: # Otherwise, - number = number * 3 + 1 # multiply by 3 and add 1 - -def final(): - print('END COLLATZ SEQUENCE') - print('Sequence has reached a 4-2-1 loop.') - input() - exit() - -while True: - # Execute the sequence - modulo() - print(number) - if number == 1.0: - break - -final() + +The Collatz conjecture is most famous for +harboring one of the unsolved problems in +mathematics: does the Collatz sequence really +reach 1 for all positive integers? + +This program takes any input integer +and performs a Collatz sequence on them. +The expected behavior is that any number +inputted will always reach a 4-2-1 loop. + +Do note that Python is limited in terms of +number size, so any enormous numbers may be +interpreted as infinity, and therefore +incalculable, by Python. This limitation +was only observed in CPython, so other +implementations may or may not differ. + +1/2/2022 - Revision 1 of Collatz-Conjecture +David Costell (DontEatThemCookies on GitHub) +""" + +import math + +print('Collatz Conjecture (Revised)\n') + +def main(): + # Get the input + number = input('Enter a number to calculate: ') + try: + number = float(number) + except ValueError: + print('Error: Could not convert to integer.') + print('Only numbers (e.g. 42) can be entered as input.') + main() + + # Prevent any invalid inputs + if number <= 0: + print('Error: Numbers zero and below are not calculable.') + main() + if number == math.inf: + print('Error: Infinity is not calculable.') + main() + + # Confirmation before beginning + print('Number is:', number) + input('Press ENTER to begin.') + print('\nBEGIN COLLATZ SEQUENCE') + + def sequence(number: float) -> float: + """ + The core part of this program, + it performs the operations of + the Collatz sequence to the given + number (parameter number). + """ + modulo = number % 2 # The number modulo'd by 2 + if modulo == 0: # If the result is 0, + number = number / 2 # divide it by 2 + else: # Otherwise, + number = 3 * number + 1 # multiply by 3 and add 1 (3x + 1) + return number + + # Execute the sequence + while True: + number = sequence(number) + print(round(number)) + if number == 1.0: + break + + print('END COLLATZ SEQUENCE') + print('Sequence has reached a 4-2-1 loop.') + exit(input('\nPress ENTER to exit.')) + +# Entry point of the program +if __name__ == '__main__': + main()