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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:06:23+00:00 2026-06-13T09:06:23+00:00

I’m currently trying to work through the Project Euler questions with Python (something I’ve

  • 0

I’m currently trying to work through the Project Euler questions with Python (something I’ve only picked up today). The question I’m working is question 5, where

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

I have worked through the problems using Java before, so using the same method as I did before, I just created a loop that iterated, however it seems that my code never ends.

Python:

i = 1
while 1:
    if i%2 == 0 and i%3==0 and i%4==0 and i%5==0 and i%6==0 and i%7==0 and i%8==0 and i%9==0 and i%10==0 and i%11==0 and i%12==0 and i%13==0 and i%14==0 and i%15==0 and i%16==0 and i%17==0 and i%18==0 and i%19==0:
        print i
        break
    i+=1

Java:

public class p5
{
    public static void main(String[] args)
    {
        for (int i=1;;i++)
        {
            if (i%1==0&&i%2==0&&i%3==0&&i%4==0&&i%5==0&&i%6==0&&i%7==0&&i%8==0&&i%9==0&&i%10==0&&i%11==0&&i%12==0&&i%13==0&&i%14==0&&i%15==0&&i%16==0&&i%17==0&&i%18==0&&i%19==0&&i%20==0)
            {
                System.out.println(i);
                break;
            }
        }
    }
}

Java executed that in under 3 seconds on my computer, whereas the Python code never seemed to end. Any tips?

Edit:

Apparently I typed something wrong, which caused it to never end. However, even with the entire thing written correctly (with the same output as my Java), it still took 1 minute and 20 seconds, whereas for Java it took around 1 – 2 seconds. Am I doing anything wrong? Or is Python’s performance that bad (which shouldn’t be afaik)

  • 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-13T09:06:24+00:00Added an answer on June 13, 2026 at 9:06 am

    Arithmetics with primitive ints is much faster in Java then with full integer objects in CPython i.e., the performance difference of 30 times that you see is not surprising for your code.

    Note the algorithm is very inefficient and it won’t work for a slightly larger input e.g., for numbers from 1 to 50 (It takes too long and the correct result is larger than max int/long in Java).

    It takes ~100 micro-seconds to compute the result for all numbers from 1 to 100 on my machine with a more efficient algorithm:

    #!/usr/bin/env python
    from functools import reduce
    
    def gcd(a, b):
        """Greatest common divisor (factor)."""
        while b: # Euclid's algorithm
            a, b = b, a % b
        return a
    
    def lcm(*args):
        """Least common multiple."""
        # lcm(a, b, c) == lcm(lcm(a, b), c)
        return reduce(lambda a, b: a * b // gcd(a, b), args)
    
    def f(n):
        """Smallest positive number evenly divisible by all numbers from 1
           to n including.
        """
        return lcm(*range(1, n + 1))
    
    print(f(10)) # -> 2520
    print(f(50)) # -> 3099044504245996706400
    

    To estimate the performance:

    #!/usr/bin/env python
    import timeit
    from functools import partial
    
    def measure():
        for n in [10, 50, 100]:
            print("%d: %5.2f microseconds" % (n, timeit.timeit(partial(f, n),
                                                               number=1000*1000)))
    measure()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this

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.