100 examples of classic Java programming exercises: Example 24: Find the least common multiple

Don't feel inferior, and improve your strength.
Who
is the best in the Internet industry? If the article can bring you energy, that's the best thing! Please believe in yourself, come
on o~

Java classic programming exercises, beginners can refer to learning

Insert picture description here
Click the link below to
summarize 100 examples of Java classic programming exercises

Title description:

Find the least common multiple

Problem-solving ideas:

欧几里得算法

Code:

public class Main {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(gcd(12, 6));
        System.out.println(lcm(3,7));
    }

    public static int gcd(int a, int b) {
    
    
        if (b == 0) {
    
    
            return a;
        }
        return gcd(b, a % b);
    }

    public static int lcm(int x, int y) {
    
    
        return (x * y) / (gcd(x, y));
    }
}

Guess you like

Origin blog.csdn.net/m0_47256162/article/details/113761665