Home Database Mysql Tutorial 2、求m和n的最大公约数与最小公倍数(最大公约数:转辗相除法)

2、求m和n的最大公约数与最小公倍数(最大公约数:转辗相除法)

Jun 07, 2016 pm 03:48 PM
greatest common divisor least common multiple division

2、求m和n的最大公约数与最小公倍数(最大公约数:转辗相除法)。 public class Jiejue2 { public static void main(String args[]) { System.out.println(gongyue(8, 6)); System.out.println(gongbei(8,6)); } //求m和n的最大公约数 public static int gon

2、求m和n的最大公约数与最小公倍数(最大公约数:转辗相除法)。

public class Jiejue2 {

public static void main(String args[]) {
     System.out.println(gongyue(8, 6));
     System.out.println(gongbei(8,6));
    }
   
    //求m和n的最大公约数
    public static int gongyue(int m, int n) {
     while(m % n != 0) {
      int temp = m % n;
      m = n;
      n = temp;
     }
     return n;
    }
   
    //求m和n的最小公倍数
    public static int gongbei(int m, int n) {
     return m * n / gongyue(m, n);
    }
   
}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

What are the advantages and disadvantages of C++ function macro definition? What are the advantages and disadvantages of C++ function macro definition? Apr 11, 2024 pm 04:54 PM

Although function macro definition can simplify code and improve performance, it also has disadvantages: type insecurity, debugging difficulties, naming conflicts, and code redundancy. After weighing the pros and cons, it's crucial to make informed decisions when using function macros.

How to write an algorithm to find the least common multiple in Python? How to write an algorithm to find the least common multiple in Python? Sep 19, 2023 am 11:25 AM

How to write an algorithm to find the least common multiple in Python? The least common multiple is the smallest integer between two numbers that can divide the two numbers. In mathematics, solving the least common multiple is a basic mathematical task, and in computer programming, we can use Python to write an algorithm for solving the least common multiple. The following will introduce the basic least common multiple algorithm and give specific code examples. The mathematical definition of the least common multiple is: If a is divisible by n and b is divisible by n, then n is the least common multiple of a and b. To solve the minimum

Detailed explanation of how to use C language to find the greatest common divisor Detailed explanation of how to use C language to find the greatest common divisor Feb 18, 2024 pm 11:10 PM

Detailed explanation of the method of finding the greatest common divisor in C language The greatest common divisor (GCD, Greatest Common Divisor) is a commonly used concept in mathematics, which refers to the largest divisor among several integers. In C language, we can use many methods to find the greatest common divisor. This article will detail several of these common methods and provide specific code examples. Method 1: Euclidean division is a classic method for finding the greatest common divisor of two numbers. Its basic idea is to continuously divide the divisors and remainders of two numbers

How to use the least common multiple algorithm in C++ How to use the least common multiple algorithm in C++ Sep 19, 2023 pm 01:48 PM

How to use the least common multiple algorithm in C++ The least common multiple (Least Common Multiple, referred to as LCM) refers to the smallest common multiple of two or more integers. In mathematics and computer science, finding the least common multiple is a common problem, and C++ provides a simple and effective way to calculate the least common multiple. This article will introduce how to use the least common multiple algorithm in C++ and provide specific code examples. First, let’s understand the definition of least common multiple. for

How to find the least common multiple in C language How to find the least common multiple in C language Sep 28, 2023 am 10:41 AM

The method of solving the least common multiple in C language is to use loops and conditional statements. By continuously adding a number, it is judged whether the number can be divided by two numbers at the same time until the least common multiple is found.

Detailed explanation of C++ function calling mechanism Detailed explanation of C++ function calling mechanism Apr 11, 2024 pm 02:12 PM

The function calling mechanism in C++ involves passing arguments to a function and executing its code, returning the result if one exists. There are two ways to pass parameters: pass by value (modifications are made inside the function) and pass by reference (modifications are reflected in the caller). In value passing, value modifications within the function do not affect the original value (such as printValue), while modifications in reference passing affect the original value (such as printReference).

Detailed explanation of division and rounding operations in Golang Detailed explanation of division and rounding operations in Golang Jan 28, 2024 am 10:15 AM

Detailed explanation of Golang division and rounding method 1. Introduction In Golang programming, division operations sometimes require rounding operations. If you want to divide a floating point number by another floating point number and round the result, you can use the related functions in the math package provided by Golang. 2. Rounding down Rounding down refers to rounding a value down to the nearest smaller integer. In Golang, rounding down can be achieved using the Floor function in the math package. The sample code is as follows: packagema

See all articles