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

  • Home
  • SEARCH
  • 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 8319507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:20:51+00:00 2026-06-08T22:20:51+00:00

I solved Project Euler #14 using the following code: import time start_time = time.time()

  • 0

I solved Project Euler #14 using the following code:

import time
start_time = time.time()
def collatz_problem(n):
    count = 0
    while n!=1:
        if n%2==0:
            n = n/2
            count = count+1
        elif n%2!=0:
            n = 3*n+1
            count = count +1
    return count+1


def longest_chain():
    max_len,num = 1,1
    for i in xrange(13,1000000):
        chain_length = collatz_problem(i)
        if chain_length > max_len:
            max_len = chain_length
            num = i

    return num

print longest_chain()
print time.time() - start_time, "seconds"

The above solution took around ~35 seconds to run. Now, I tried another solution from here.

Solution:

import time
start_time = time.time()
cache = { 1: 1 }
def chain(cache, n):
     if not cache.get(n,0):
         if n % 2: cache[n] = 1 + chain(cache, 3*n + 1)
         else: cache[n] = 1 + chain(cache, n/2)
     return cache[n]
m,n = 0,0
for i in xrange(1, 1000000):
    c = chain(cache, i)
    if c > m: m,n = c,i

print n
print time.time() - start_time, "seconds"

Now, this solution only took ~3.5 seconds.

First Question:

Now, as I am a python beginner I cannot see why there is so much of a difference in these two approaches and how can I modify my code to make it more effecient.

Second Question:

While solving project euler questions is there any time constraints one should keep in mind and is my code really that ineffecient.

  • 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-08T22:20:53+00:00Added an answer on June 8, 2026 at 10:20 pm

    In the first version you may calculate the length of some chains several times because they are subchains in other chains.

    In the second solution you are only calculating the length of each chain once because of the caching. This optimization technique is called memoization.


    A more dramatic example of memoization is calculation of the Fibonacci numbers. Here’s the simple recursive solution:

    def fib(n):
        if n < 2:
            return n
        else:
            return fib(n-1) + fib(n-2)
    

    It takes exponential time because fib(n) evaluates fib(n-1) and fib(n-2), but fib(n-1) also evaluates fib(n-2), so you end up doing the exact same calculation again. Try calculating fib(35) or higher with this algorithm.

    By caching the results of fib(x) for each x you can avoid recalculating the same result, improving the performance to linear time.

    def fib2(n):
        if n < 2:
            return n
        elif n in cache:
            return cache[n]
        else:
            result = fib2(n-1) + fib2(n-2)
            cache[n] = result
            return result
    

    Related

    • What is memoization and how can I use it in Python?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I solved Problem 10 of Project Euler with the following code, which works through
I am trying to solve Project Euler problem in Clojure using recursion. The following
I'm currently working on project euler problem 14 . I solved it using a
I just solved the first problem from Project Euler in JavaFX for the fun
I am using Haskell to solve problem 99 in euler project, where I must
I was trying to solve Project Euler problem 10 using python, but my program
I am trying to solve Q10 from Project Euler. I am using JavaScript and
I solved a problem on Project Euler but it took about 4 minutes to
For a little context, i'm trying to solve Project Euler problem 31 using excellent
I'm solving problems in Project Euler . Most of the problems solved by big

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.