Skip to content

Commit bb6c62a

Browse files
kvedalabuffetgithub-actions
authored
feat: Project Euler Problem 5 - #162 (#599)
* rename existing code as sol3 * Added naive implementation for Problem 5 * Added a solution for Euler Problem 5 with easy improvements * rename new files * code formatting * update documentations * fix docs * updating DIRECTORY.md Co-authored-by: buffet <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 23b2a29 commit bb6c62a

File tree

4 files changed

+124
-3
lines changed

4 files changed

+124
-3
lines changed

DIRECTORY.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@
319319
* Problem 401
320320
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_401/sol1.c)
321321
* Problem 5
322-
* [Sol](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_5/sol.c)
322+
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_5/sol1.c)
323+
* [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_5/sol2.c)
324+
* [Sol3](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_5/sol3.c)
323325
* Problem 6
324326
* [Sol](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_6/sol.c)
325327
* Problem 7

project_euler/problem_5/sol1.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* \file
3+
* \brief [Problem 5](https://projecteuler.net/problem=5) solution - Naive
4+
* algorithm (slowest)
5+
*
6+
* \see Faster: problem_5/sol2.c
7+
* \see Fastest: problem_5/sol3.c
8+
*/
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
/** Pretty naive implementation. Just checks every number if it's devisable by 1
13+
* through 20
14+
* @param n number to check
15+
* @returns 0 if not divisible
16+
* @returns 1 if divisible
17+
*/
18+
static char check_number(unsigned long long n)
19+
{
20+
for (unsigned long long i = 1; i <= 20; ++i)
21+
{
22+
if (n % i != 0)
23+
{
24+
return 0;
25+
}
26+
}
27+
28+
return 1;
29+
}
30+
31+
/**
32+
* @brief Main function
33+
*
34+
* @return 0 on exit
35+
*/
36+
int main(void)
37+
{
38+
for (unsigned long long n = 1;; ++n)
39+
{
40+
if (check_number(n))
41+
{
42+
printf("Result: %llu\n", n);
43+
break;
44+
}
45+
}
46+
47+
return 0;
48+
}

project_euler/problem_5/sol2.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* \file
3+
* \brief [Problem 5](https://projecteuler.net/problem=5) solution - Naive
4+
* algorithm (Improved over problem_5/sol1.c)
5+
* @details Little bit improved version of the naive `problem_5/sol1.c`. Since
6+
* the number has to be divisable by 20, we can start at 20 and go in 20 steps.
7+
* Also we don't have to check against any number, since most of them are
8+
* implied by other divisions (i.e. if a number is divisable by 20, it's also
9+
* divisable by 2, 5, and 10). This all gives a 97% perfomance increase on my
10+
* machine (9.562 vs 0.257)
11+
*
12+
* \see Slower: problem_5/sol1.c
13+
* \see Faster: problem_5/sol3.c
14+
*/
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
18+
/**
19+
* @brief Hack to store divisors between 1 & 20
20+
*/
21+
static unsigned int divisors[] = {
22+
11, 13, 14, 16, 17, 18, 19, 20,
23+
};
24+
25+
/** Checks if a given number is devisable by every number between 1 and 20
26+
* @param n number to check
27+
* @returns 0 if not divisible
28+
* @returns 1 if divisible
29+
*/
30+
static int check_number(unsigned long long n)
31+
{
32+
for (size_t i = 0; i < 7; ++i)
33+
{
34+
if (n % divisors[i] != 0)
35+
{
36+
return 0;
37+
}
38+
}
39+
40+
return 1;
41+
}
42+
43+
/**
44+
* @brief Main function
45+
*
46+
* @return 0 on exit
47+
*/
48+
int main(void)
49+
{
50+
for (unsigned long long n = 20;; n += 20)
51+
{
52+
if (check_number(n))
53+
{
54+
printf("Result: %llu\n", n);
55+
break;
56+
}
57+
}
58+
return 0;
59+
}

project_euler/problem_5/sol.c renamed to project_euler/problem_5/sol3.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
/**
22
* \file
3-
* \brief [Problem 5](https://projecteuler.net/problem=5) solution
3+
* \brief [Problem 5](https://projecteuler.net/problem=5) solution (Fastest).
4+
* @details Solution is the LCM of all numbers between 1 and 20.
5+
*
6+
* \see Slowest: problem_5/sol1.c
7+
* \see Slower: problem_5/sol2.c
48
*/
59
#include <stdio.h>
610

711
/** Compute [Greatest Common Divisor
812
* (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of two numbers
913
* using Euclids algorithm
14+
* @param a first number
15+
* @param b second number
16+
* @return GCD of `a` and `b`
1017
*/
1118
unsigned long gcd(unsigned long a, unsigned long b)
1219
{
@@ -27,14 +34,19 @@ unsigned long gcd(unsigned long a, unsigned long b)
2734

2835
/** Compute [Least Common Multiple
2936
* (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple) of two numbers
37+
* @param a first number
38+
* @param b second number
39+
* @return LCM of `a` and `b`
3040
*/
3141
unsigned long lcm(unsigned long a, unsigned long b)
3242
{
3343
unsigned long long p = (unsigned long long)a * b;
3444
return p / gcd(a, b);
3545
}
3646

37-
/** Main function */
47+
/** Main function
48+
* @returns 0 on exit
49+
*/
3850
int main(void)
3951
{
4052
unsigned long ans = 1;

0 commit comments

Comments
 (0)