Menu
Coddy logo textTech

Introduction

Lesson 13 of 20 in Coddy's Mathematical Riddles course.

The least common multiple of two integers x and y, usually denoted by LCM(x,y), is the smallest positive integer that is divisible by both x and y.

For example:

  • LCM(24, 16) = 48
  • LCM(3, 5) = 15
  • LCM(4, 6) = 12.

It is used to add fractions. 

Example: 7/24+5/16 = 7 * (2/48) + 5 * (3/48) = (14+15) / 48 = 29/48.

It is used to find when certain couples with specific periods will meet again. Example: Dan comes to drink beer every third night, and John every forth. Thus, they will meet once every 12 nights.

It is connected to the greatest common divisor via:

LCM(x, y) = (x * y) / GCD(x, y)

challenge icon

Challenge

Easy

Write a function calcLCM that gets a vector of two integers, v, and calculates the smallest number that can be divided by each of the numbers without any remainder.

Try it yourself

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "solution.h"

int main() {
    int v[4096];
    int vn = 0;
    char line[65536];
    if (!fgets(line, sizeof(line), stdin)) line[0] = '\0';
    char* tok = strtok(line, " \t\r\n");
    while (tok) { v[vn++] = atoi(tok); tok = strtok(NULL, " \t\r\n"); }
    int r = calcLCM(v, vn);
    printf("%d\n", r);
    return 0;
}

All lessons in Mathematical Riddles

7Least common multiple

IntroductionA problem