Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:46:28+00:00 2026-06-15T18:46:28+00:00

Problem 14 on Project Euler describes a certain puzzle that many people have asked

  • 0

Problem 14 on Project Euler 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?

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 & 1 else n >> 1
        # if the new number was already found, this sequence is done
        if n in found:
            break

if __name__ == '__main__':
    main()

The documentation was added later and is hopefully clear enough to explain why I thought it would work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:46:29+00:00Added an answer on June 15, 2026 at 6:46 pm

    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.

    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.

    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)
    

    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.

    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 & 1 else n >> 1
    
    if __name__ == '__main__':
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble with this problem in Project Euler. Here's what the question
I have a code that basically solves one of the problem in Project Euler
In a Project Euler problem I need to deal with numbers that can have
Here is how Project Euler Problem #76 sounds like: How many different ways can
I am attempting to solve Project Euler problem 10 where the user is asked
This is Problem 3 from Project Euler site I'm not out after the solution,
Possible Duplicate: Project Euler, Problem 10 java solution not working So, I'm attempting to
I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit
I'm trying to complete the Project Euler problem found here. For some reason my
I'm trying to solve Project Euler problem 59, where I have to xor-decrypt a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.