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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:39:50+00:00 2026-06-15T13:39:50+00:00

Problem 5: 2520 is the smallest number that can be divided by each of

  • 0

Problem 5: 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 solved the problem 5 of Project Euler

Here is the Java code:

 static long FindLcm(long a,long b)
 {
     long lcm,hcf = 0;
     long i=1;
     long ger=a>b?a:b;
     while(i<ger)
     {
         if((a%i==0) && (b%i==0))
             hcf=i;
         i++;
     }
     lcm=(a*b)/hcf;
     return lcm;
 }
 static void FindMultiple()
 {
     long lcm=1;
     for(long i=2;i<=20;i++)
     {
         lcm=FindLcm(lcm,i);
     }   
     System.out.println("Lcm="+lcm);
 }

How can optimize this?

  • 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-15T13:39:52+00:00Added an answer on June 15, 2026 at 1:39 pm

    Your FindMultiple() method is not bad,

    static void FindMultiple()
    {
        long lcm=1;
        for(long i=2;i<=20;i++)
        {
            lcm=FindLcm(lcm,i);
        }
        System.out.println("Lcm="+lcm);
    }
    

    it implements a fairly good algorithm. Your problem is that your FindLcm() contains a nasty performance bug.

    static long FindLcm(long a,long b)
    {
        long lcm,hcf = 0;
        long i=1;
        // This sets ger to max(a,b) - why?
        long ger=a>b?a:b;
        // This would return a wrong result if a == b
        // that never happens here, though
        while(i<ger)
        {
            if((a%i==0) && (b%i==0))
                hcf=i;
            i++;
        }
        lcm=(a*b)/hcf;
        return lcm;
    }
    

    You are looping until you reach the larger of the two arguments. Since the cumulative LCMs grow rather fast, that takes a lot of time. But the GCD (or HCF, if you prefer) of two (positive) numbers cannot be larger than the smaller of the two. So looping only until the smaller of the two arguments is reached makes the number of iterations at most 20 here, do that 19 times (for i = 2, ..., 20), it’s a trivial amount of computation.

    Changing to

    long ger = a < b ? a : b;
    while(i <= ger) {
    

    gives me (adding timing code, not measuring the printing):

    17705 nanoseconds
    Lcm=232792560
    

    So less than 20 microseconds for the computation. We can easily push that below 6 microseconds if we use the euclidean algorithm to find the greatest common divisor,

    static long gcd(long a, long b) {
        while(b > 0) {
            a %= b;
            if (a == 0) return b;
            b %= a;
        }
        return a;
    }
    

    and below 5 if we directly use the GCD as

    lcm *= i/gcd(lcm,i);
    

    in FindMultiple().

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

Sidebar

Related Questions

Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem: to combine PATHs with filenames, such that I can easily source many files.
Problem: I have a table that prints out vertical but I would like it
Problem in MATLAB Code for solving desired 'n' number of simultaneous equations of the
Problem: I'm streaming my video from a php file with stream_get_contents(); using Flowplayer as
Problem occured when i tried to display xml data that has been taken by
Problem Description: Multi-player Game that works over the some kind of network, in game
Problem background: I have a Qt/QML Symbian application targeting Qt 4.7.4, that requires a
Problem: I have an address field from an Access database which has been converted
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has

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.