πŸ› οΈToolsShed

Kalkulator FPB dan KPK

Menghitung Faktor Persekutuan Terbesar dan Kelipatan Persekutuan Terkecil.

GcdLcm.inputHint

GcdLcm.infoTitle

  • GcdLcm.infoGcd
  • GcdLcm.infoLcm
  • GcdLcm.infoAlgorithm

Pertanyaan yang Sering Diajukan

Implementasi Kode

import math

def gcd(a: int, b: int) -> int:
    """Euclidean algorithm for Greatest Common Divisor."""
    while b:
        a, b = b, a % b
    return a

def lcm(a: int, b: int) -> int:
    """Least Common Multiple via GCD."""
    return abs(a * b) // gcd(a, b)

def gcd_multi(*nums: int) -> int:
    """GCD of multiple numbers."""
    result = nums[0]
    for n in nums[1:]:
        result = gcd(result, n)
    return result

def lcm_multi(*nums: int) -> int:
    """LCM of multiple numbers."""
    result = nums[0]
    for n in nums[1:]:
        result = lcm(result, n)
    return result

# Examples
print(gcd(48, 18))           # 6
print(lcm(4, 6))             # 12
print(gcd_multi(12, 18, 24)) # 6
print(lcm_multi(4, 6, 10))   # 60

# Using Python's built-in math module (Python 3.9+)
print(math.gcd(48, 18))      # 6
print(math.lcm(4, 6, 10))    # 60

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.