How can I found how many numbers are there in a Collatz Sequence that I found? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: How can I found how many numbers are there in a Collatz Sequence that I found? (/thread-31024.html) |
How can I found how many numbers are there in a Collatz Sequence that I found? - cananb - Nov-18-2020 I tried to find the longest starting value less than 100,000 in a Collatz Sequence. Actually, I have two questions: 1)Output is 77,031, is this correct? 2)How can I find how many elements are there in the sequence that I found? def sequence_number(n): term = 1 while n > 1: if n % 2 == 0: n = n/2 else: n = 3 * n+1 term += 1 return term def starting_number(): t = 0 x = 1 while x < 100000: if sequence_number(x) > t: t = sequence_number(x) value = x x += 1 return value print("It starts with the number:", starting_number()) RE: How can I found how many numbers are there in a Collatz Sequence that I found? - DPaul - Nov-19-2020 Hi i see some potential issues: - Indentation in the functions cannot be right -you seem to be returning (line17) the value, rather than t. Tell us what the max sequence is. Less than 77031 ? Paul RE: How can I found how many numbers are there in a Collatz Sequence that I found? - DeaD_EyE - Nov-19-2020 The result seems to be right. Just your indentation is wrong. For infinite sequences (collatz is one of them) Generators could be used. Here is a derivation of your code: from itertools import count def ask_int(question): while True: user_input = input(question + ": ") try: return int(user_input) except ValueError: print(f"'{user_input}' is not a valid integer") def collatz_generator(start): if start < 1: raise ValueError("Start must be a positive integer") n = start while n > 1: if n % 2 == 0: n = n // 2 else: n = 3 * n + 1 yield n def collatz_counter(start): if start < 1: raise ValueError("Start must be a positive integer") n = start for trys in count(1): if n % 2 == 0: n = n // 2 else: n = 3 * n + 1 if n <= 1: break return trys def longest_collatz(start=1, end=100_000): maximum_length = 0 maximum_value = 0 for value in range(start, end + 1): length = collatz_counter(value) if length > maximum_length: maximum_length = length maximum_value = value return maximum_value, maximum_length if __name__ == "__main__": print("Find the longest collatz sequence with given start and end") start = ask_int("Start Value") end = ask_int("End Value") value, length = longest_collatz(start, end) print(f"The start value {value} creates a collatz sequence with {length} values.") Quote:PS C:\Users\Admin\Desktop> py collatz.py I think the code is also a bit faster. The generator collatz_generator is not used in the code. I didn't remove it. Maybe you can experiment with it. gen = collatz_generator(19) print(list(gen)) In [1]: gen = collatz_generator(19) ...: print(list(gen)) [58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]The 19 is not included. My mistake. Quick fix just for completeness: def collatz_generator(start): if start < 1: raise ValueError("Start must be a positive integer") n = start yield n # <-- this will yield n, which is the start value while n > 1: if n % 2 == 0: n = n // 2 else: n = 3 * n + 1 yield n # <-- this here are the values after the start value RE: How can I found how many numbers are there in a Collatz Sequence that I found? - DPaul - Nov-19-2020 @DeaD_EyE: I thought we were looking for the most iterations, but we are looking for the value that causes them, indeed 77031! Paul RE: How can I found how many numbers are there in a Collatz Sequence that I found? - DeaD_EyE - Nov-19-2020 Quote:The start value 77031 creates a collatz sequence with 350 values. Yes, we're looking for the most iterations (sequence length). Given is start value from 1 to 100_000 for example. Calculate for each start value, the collatz sequence length. Take the longest sequence and tell us the start value. In collatz_counter I count the iterations (hopefully right). In longest_collatz I calculate the maximum_length (sequence length / iterations) and the corresponding start value to get this maximum length. By the way, negative values are also possible for a collatz sequence. In this case the addition of 1 changed to subtraction. Just for completeness: def collatz_generator(start): n = start if n < 0: offset = -1 else: offset = +1 yield start while abs(n) > 1: if n % 2 == 0: n = n // 2 else: n = 3 * n + offset yield n RE: How can I found how many numbers are there in a Collatz Sequence that I found? - cananb - Nov-23-2020 (Nov-19-2020, 09:20 AM)DPaul Wrote: @DeaD_EyE: I really appreciate your help!! Thank you so much |