“Znajdź LCM z GCD” Kod odpowiedzi

GCD i LCM w C

long long gcd(long long int a, long long int b)
{
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
long long lcm(ll int a, ll int b)
{
    return (a / gcd(a, b)) * b;
}
// there is a math formula used in this code which you can search up about,
// but you can just use it as it is.
Xixika

Znajdź LCM z GCD

import java.util.*;

public class LCM {
  
  public static long lcm(int a, int b) {

    long x = (a/euclidGcd(a, b));

    return x * b;

  }

  public static int euclidGcd(int a, int b) {

    if (b == 0)
      return a;

    int aprime = a%b;
    
    return euclidGcd(b, aprime);
  }
  
  public static void main(String args[]) {
    Scanner scanner = new Scanner(System.in);
    int a = scanner.nextInt();
    int b = scanner.nextInt();

    System.out.println(lcm(a, b));
  }
}
Dale Applegate

LCM dwóch liczb za pomocą GCD

#include <stdio.h>
int main() {
    int n1, n2, i, gcd, lcm;
    printf("Enter two positive integers: ");
    scanf("%d %d", &n1, &n2);

    for (i = 1; i <= n1 && i <= n2; ++i) {
        
        // check if i is a factor of both integers
        if (n1 % i == 0 && n2 % i == 0)
            gcd = i;
    }

    lcm = (n1 * n2) / gcd;

    printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);
    return 0;
}
Jittery Jaguar

Odpowiedzi podobne do “Znajdź LCM z GCD”

Pytania podobne do “Znajdź LCM z GCD”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu