Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After explaining the proposed solution to a colleague, the answer came to me: this solution does not take into consideration the length of sequences generated outside of the range of numbers being tested. Therefore, a new solution will need to be devised that considers the length of complete sequences.</p> <p>To test the algorithm, the following program was written. The method works for a sequence over an entire closed range. This is quite impossible to accomplish in the Collatz Problem, so the code fails.</p> <pre><code>import random import time class Sequencer: def __init__(self, limit, seed): random.seed(seed) self.__sequence = tuple(random.sample(range(limit), limit)) def __call__(self, start): yield from self.__sequence[self.__sequence.index(start):] @property def longest(self): return self.__sequence[0] def main(limit): while True: sequence = Sequencer(limit, str(time.time())) longest = find_longest(limit, sequence) print('Found longest ' + ('' if longest == sequence.longest else 'in') + 'correctly.') def find_longest(limit, sequence): start, found = set(range(limit)), set() while start: number = start.pop() result = set(get_sequence(sequence(number), found)) start -= result found |= result return number def get_sequence(sequence, found): for number in sequence: if number in found: break yield number if __name__ == '__main__': main(1000000) </code></pre> <p>The corrected version of the code follows a similar pattern in its design but keeps track of values outside of the original range. Execution time has been found to be similar to other Python solutions to the puzzle.</p> <pre><code>def main(): start, found = set(range(2, 1000000)), {1: 1} while start: scope = reversed(tuple(sequence(start.pop(), found))) value = dict(map(reversed, enumerate(scope, found[next(scope)] + 1))) start -= frozenset(value) found.update(value) lengths = dict(map(reversed, found.items())) print(lengths[max(lengths)]) def sequence(n, found): while True: yield n if n in found: break n = 3 * n + 1 if n &amp; 1 else n &gt;&gt; 1 if __name__ == '__main__': main() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload