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 7770241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:21:44+00:00 2026-06-01T16:21:44+00:00

I solved Euler problem 14 but the program I used is very slow. I

  • 0

I solved Euler problem 14 but the program I used is very slow. I had a look at what the others did and they all came up with elegant solutions. I tried to understand their code without much success.

Here is my code (the function to determine the length of the Collatz chain

def collatz(n):
a=1
while n!=1:
    if n%2==0:
        n=n/2
    else:
        n=3*n+1
    a+=1
return a

Then I used brute force. It is slow and I know it is weak. Could someone tell me why my code is weak and how I can improve my code in plain English.
Bear in mind that I am a beginner, my programming skills are basic.

  • 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-01T16:21:45+00:00Added an answer on June 1, 2026 at 4:21 pm

    Rather than computing every possible chain from the start to the end, you can keep a cache of chain starts and their resulting length. For example, for the chain

    13 40  20  10  5  16  8  4  2  1
    

    you could remember the following:

    1. The Collatz chain that starts with 13 has length 10
    2. The Collatz chain that starts with 40 has length 9
    3. The Collatz chain starting with 20 has length 8

    … and so on.

    We can then use this saved information to stop computing a chain as soon as we encounter a number which is already in our cache.

    Implementation

    Use dictionaries in Python to associate starting numbers with their chain length:

    chain_sizes = {}
    chain_sizes[13] = 10
    chain_sizes[40] = 9
    chain_sizes[40]   # => 9
    20 in chain_sizes # => False
    

    Now you just have to adapt your algorithm to make use of this dictionary (filling it with values as well as looking up intermediate numbers).

    By the way, this can be expressed very nicely using recursion. The chain sizes that can occur here will not overflow the stack 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did this problem [ Project Euler problem 5 ], but very bad manner
I was trying to solve Project Euler problem 10 using python, but my program
I solved a problem on Project Euler but it took about 4 minutes to
I solved Problem 10 of Project Euler with the following code, which works through
I just solved the first problem from Project Euler in JavaFX for the fun
I am trying to solve Project Euler Problem 88 , and I did it
I'm currently working on project euler problem 14 . I solved it using a
I tried to solve problem# 276 from Project Euler , but without success so
This is fairly 'math-y' but I'm posting here because it's a Project Euler problem,
Beginner Haskell learner here. I've solved Project Euler Problem 1, sum of multiples of

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.