English 中文(简体)
• 如何为两个数字找到最差的多功能(LCM)
原标题:How to find Least Common Multiple (LCM) for two numbers

我使用了Euclid方法找到L.C.M两个数字。

l.c.m=a*b/(gcd(a,b))

How can I do this without using this algorithm? I have an idea of first getting all factors of these two numbers and storing them in array. Then take 1 element from array 1 and search for it in array2, if it present there then remove it from there and make the result multiply by that num.

是否是这样?

最佳回答

几乎是这样。 第4和第8条的内容是什么? 显然有8个(2个<>3),但是在你的方法中,你发现2个。 你们不仅需要跟踪所有因素,而且需要了解这些因素的出现频率。

问题回答

我认为,你建议的算法是:,检查是否为你工作。

LCM(Least Common Multi)总是大于或等于这两个数字的更大。 因此,如果我们发现LCM &的话,我们首先会核实,如果发现LCM &,则人数越多,数字就越多,只有两个数字的LCM。

package com.company;
import java.util.Scanner;

public class Main {

public static void main(String args[]) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter the first Number : ");
    int number1 = scan.nextInt();
    System.out.print("Enter the second number : ");
    int number2 =scan.nextInt();

    int multiple;

    if(number1 >= number2) {
        multiple = number1;
    } else {
        multiple = number2;
    }

    Boolean loopContinue = true;

    while(loopContinue) {
        if(multiple % number1 == 0 && multiple % number2 == 0) {
            System.out.println("LCM of Two Numbers is " + multiple);
            loopContinue = false;
        }
        multiple++;
    }
  }
}

**LCM, 2 number using while loop**

package whileloop1;

import java.util.Scanner;

public class Lcm {

public static void main(String[] args) {
    Lcm obj = new Lcm();
    obj.twoNumberLcm();
}
void twoNumberLcm       
{
    Scanner Sobj = new Scanner(System.in);
    System.out.println("Enter your First Number ");
    int num1 = Sobj.nextInt();
    System.out.println("Enter your Second Number ");
    int num2 = Sobj.nextInt();
    int big,small;
    if(num1>num2)
    {
        big = num1;
        small = num2;
    }
    else
    {
        big = num2;
        small = num1;
    }
    System.out.println(" Bigger number is " + big);
    System.out.println(" Smaller number is " + small);
    int smallcopy = small;
    int bigcopy = big;
    int count =1;
    while(count>0)
    {
        while(big>=small)
        {
            if(big == small)
            {
                System.out.println("Least common mutiples of given two numbers" + small);
                count--;
                break;
            }
            small=small+smallcopy;
        }
        big = big+bigcopy;
    }
}

You can get LCM of two number by getting GCD at first. Here is the solution for the above.

package com.practice.competitive.maths;

import java.util.Scanner;

public class LCMandGCD {

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int testCases = scanner.nextInt();
            while (testCases-- > 0) {
                long number1 = scanner.nextInt();
                long number2 = scanner.nextInt();
                long gcd = computeGCD(number1, number2);
                long lcm = computeLCM(number1, number2, gcd);
                System.out.println(lcm + " " + gcd);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static long computeGCD(long number1, long number2) {
        while (number1 != number2) {
            if (number1 > number2)
                number1 -= number2;
            else
                number2 -= number1;
        }   
        return number2;
    }

    private static long computeLCM(long number1, long number2, long gcd) {
        return (number1*number2)/gcd;
    }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...