Created
September 15, 2023 21:30
-
-
Save raldone01/e6def926f2c8ce7edda51659cad74ef1 to your computer and use it in GitHub Desktop.
Fast LCM and GCD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.10.2) | |
project(c_lcm) | |
set(CMAKE_CXX_STANDARD 17) | |
add_executable(c_lcm main.cpp) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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