Note that there are some explanatory texts on larger screens.

plurals
  1. POA wrong answer obviously means a logic error, but what is at fault in what was thought?
    primarykey
    data
    text
    <p><a href="http://projecteuler.net/problem=14" rel="nofollow">Problem 14</a> on <a href="http://projecteuler.net/" rel="nofollow">Project Euler</a> describes a certain puzzle that many people have asked about here. My question is not how to solve the problem or how to fix other people's errors. After thinking about the puzzle, the following "solution" was written but appears to be wrong. Could someone explain my error?</p> <pre><code>def main(): # start has all candidate numbers; found has known sequence numbers start, found = set(range(1, 1000000)), set() # if are numbers in start, then there are still unfound candidates while start: # pick a random starting number to test in the sequence generator number = start.pop() # define the set of numbers that the generator created for study result = set(sequence(number, found)) # remove them from the candidates since another number came first start -= result # record that these numbers are part of an already found sequence found |= result # whatever number was used last should yield the longest sequence print(number) def sequence(n, found): # generate all numbers in the sequence defined by the problem while True: # since the first number begins the sequence, yield it back yield n # since 1 is the last sequence number, stop if we yielded it if n == 1: break # generate the next number in the sequence with binary magic n = 3 * n + 1 if n &amp; 1 else n &gt;&gt; 1 # if the new number was already found, this sequence is done if n in found: break if __name__ == '__main__': main() </code></pre> <p>The documentation was added later and is hopefully clear enough to explain why I thought it would work.</p>
    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.
 

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