Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
sol.c File Reference

Problem 5 solution More...

#include <stdio.h>
Include dependency graph for sol.c:

Functions

unsigned long gcd (unsigned long a, unsigned long b)
 
unsigned long lcm (unsigned long a, unsigned long b)
 
int main (void)
 

Detailed Description

Problem 5 solution

Function Documentation

◆ gcd()

unsigned long gcd ( unsigned long  a,
unsigned long  b 
)

Compute Greatest Common Divisor (GCD) of two numbers using Euclids algorithm

12 {
13  unsigned long r;
14  if (a > b)
15  {
16  unsigned long t = a;
17  a = b;
18  b = t;
19  }
20  while ((r = (a % b)))
21  {
22  a = b;
23  b = r;
24  }
25  return b;
26 }

◆ lcm()

unsigned long lcm ( unsigned long  a,
unsigned long  b 
)

Compute Least Common Multiple (LCM) of two numbers

32 {
33  unsigned long long p = (unsigned long long)a * b;
34  return p / gcd(a, b);
35 }
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function

39 {
40  unsigned long ans = 1;
41  unsigned long i;
42  for (i = 1; i <= 20; i++)
43  {
44  ans = lcm(ans, i);
45  }
46  printf("%lu\n", ans);
47  return 0;
48 }
Here is the call graph for this function:
lcm
unsigned long lcm(unsigned long a, unsigned long b)
Definition: sol.c:31
gcd
unsigned long gcd(unsigned long a, unsigned long b)
Definition: sol.c:11