Sharafat Karim | GitBook
CtrlK
📑 Blog🔭 GitHub🔗 Contact
Data Structure and Algorithms
Data Structure and Algorithms
  • 🧮Data Structure and Algorithms
  • 🌸Welcome!
  • 📚Standard Template Library
  • 🍽️Prerequisites
  • 🌏Structures
    • Intro
    • Preliminaries (basics)
    • String Processing
    • Array and matrix
    • Linked List
    • Stack & Queue
      • Stack Basics
      • Postfix notation
      • Quick Sort
      • Recursion
      • Tower of Hanoi
      • Queue
      • Priority Queue (one way list)
      • Priority Queue (2D matrix)
    • Tree
      • Binary Tree
      • Binary Search Tree
      • Heap and Heap Sort
      • Minimum Spanning Tree
    • Graph
      • 🔸Warshall's Algorithm
      • Linked Representation
      • Traversing - BFS, DFS & More
      • Dijkstra
      • Bellman-Ford
    • Sorting & Searching
      • Insertion Sort
      • Selection Sort
      • Bubble Sort
      • Merge Sort
  • 📚Math
    • Intro
    • Number Theory
    • Prime Numbers
    • GCD and LCM
    • Modular Arithmetic
    • Combinatorics
    • Probability
    • Big Integer
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. 📚Math

GCD and LCM

Euclid's method should do the trick efficiently,

#include <bits/stdc++.h>
using namespace std;

int gcd(int a, int b) {
    if (b == 0) return a;
    if (a % b == 0) return b;
    return gcd(b, a%b);
}

int lcm(int a, int b) {
    return (int)((a * b) / gcd(a, b));
}

int main() {
    cout << lcm(2, 6);
}
PreviousPrime NumbersNextModular Arithmetic

Last updated 1 year ago

Was this helpful?