Math: GCD LCM
What is the GCD?
In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that is a divisor of both numbers. For example, the GCD of 8 and 12 is 4.
The greatest common divisor is also known as the greatest common factor (gcf), highest common factor (hcf), greatest common measure (gcm), or highest common divisor.
辗转相除法 method of successive division
#include <stdio.h> #include <sys/types.h> #include <unistd.h> //#include <sys/wait.h> int main() { int m, n, r; scanf("%d %d", &m, &n); if (m > n) { m = n ^ m; n = m ^ n; m = m ^ n; } while (m) { r = n % m; n = m; m = r; } printf("greatest common divisor %d\n", n); }
Recursion
package org.example; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int m = scanner.nextInt(); int n = scanner.nextInt(); System.out.println(gcd(m, n)); } public static int gcd(int m, int n) { if (m > n) { m = n ^ m; n = m ^ n; m = m ^ n; } int t = n % m; if (t == 0) return m; else return gcd(m, t); } }
更相减损法
def gcd(m: int, n: int) -> int: if m > n: m, n = n, m while n - m != m: t = n - m m, n = m, t if m < t else (t, m) return m
# 1、48和54 # 48=2*2*2*2*3 # 54=2*3*3*3 # 48和54的最大公约数是:2*3=6 # 2、32和96 # 32=2*2*2*2*2 # 96=2*2*2*2*2*3 # 32和96的最大公约数是:2*2*2*2*2=32 # 3、120、180、210 # 120=2*2*2*3*5 # 180=2*2*3*3*5 # 210=2*3*5*7 # 120、180和210的最大公约数是:2*3*5=30 def hcf(a, b): def decomposite_prime_factor(x): i = 2 # 1 is not prime number, so start from 2 t = [] def coolie(): nonlocal i, x if x % i == 0: x //= i t.append(i) coolie() else: if x == 1: return None i += 1 coolie() coolie() return t l0 = decomposite_prime_factor(a) l1 = decomposite_prime_factor(b) l2 = [] for i in l0: for j in l1: if i == j: l2.append(i) l1.remove(i) break n = 1 for v in l2: n *= v print('greatest common divisor ({},{}):{}'.format(a, b, n)) hcf(24, 60)
LCM
The abbreviation LCM stands for 'Least Common Multiple' or the Lowest Common Multiple. The least common multiple (LCM) of two numbers is the lowest possible number that can be divisible by both numbers. It can be calculated for two or more numbers as well. There are different methods to find the LCM of a given set of numbers. One of the quickest ways to find the LCM of two numbers is to use the prime factorization of each number and then the product of the highest powers of the common prime factors will be the LCM of those numbers. Let us learn how to find the lowest common multiples of numbers on this page.
function lcm(m, n) { if(m > n) { [m, n] = [n, m] } let t = n while(!(t % m === 0 && t % n === 0)) { t += m console.log(`\x1b[7m${t}\x1b[0m`) } return t } console.log(`\x1b[7mLeast common multiple: ${lcm(2, 3)}\x1b[0m`)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-05-29 Lua: Metatable
2022-05-29 Lua: coroutine
2022-05-29 Lua: Loop
2022-05-29 Lua: table
2020-05-29 Linux系统登陆过程
2020-05-29 DNS & BIND
2020-05-29 利用openssl建立私有CA