Skip to content

Instantly share code, notes, and snippets.

@raldone01
Created September 15, 2023 21:30
Show Gist options
  • Save raldone01/e6def926f2c8ce7edda51659cad74ef1 to your computer and use it in GitHub Desktop.
Save raldone01/e6def926f2c8ce7edda51659cad74ef1 to your computer and use it in GitHub Desktop.
Fast LCM and GCD
cmake_minimum_required(VERSION 3.10.2)
project(c_lcm)
set(CMAKE_CXX_STANDARD 17)
add_executable(c_lcm main.cpp)
#include <iostream>
#include <stdint.h>
#include <math.h>
uint64_t gcd(uint64_t a, uint64_t b) {
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// Base case
if (a == b)
return a;
while (b != 0) {
uint64_t t = b;
b = a % b;
a = t;
}
return a;
}
uint64_t lcm(uint64_t a, uint64_t b) {
return a / gcd(a, b) * b;
}
int main() {
std::cout << lcm(32454,334232) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment