Skip to content

Commit 23b2a29

Browse files
kvedalaadityasheth2000github-actions
authored
feat: Project Euler Problem 7 - #167 (#598)
* Please check this solution to Q7 of Project Euler * rename file * fix code formatting * added doc * updating DIRECTORY.md * added see-also references Co-authored-by: adityasheth305 <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent ce26303 commit 23b2a29

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@
324324
* [Sol](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_6/sol.c)
325325
* Problem 7
326326
* [Sol](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_7/sol.c)
327+
* [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_7/sol2.c)
327328
* Problem 8
328329
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_8/sol1.c)
329330
* [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/problem_8/sol2.c)

project_euler/problem_7/sol.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/**
22
* \file
3-
* \brief [Problem 7](https://projecteuler.net/problem=7) solution
3+
* \brief [Problem 7](https://projecteuler.net/problem=7) solution.
4+
* @see Another version: problem_7/sol2.c
45
*/
56
#include <stdio.h>
67
#include <stdlib.h>
78

8-
/** Main function */
9+
/** Main function
10+
* @return 0 on exit
11+
*/
912
int main(void)
1013
{
1114
char *sieve;

project_euler/problem_7/sol2.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* \file
3+
* \brief [Problem 7](https://projecteuler.net/problem=7) solution.
4+
* @see Faster version problem_7/sol.c
5+
*/
6+
#include <stdio.h>
7+
8+
/** Main function
9+
* @return 0 on exit
10+
*/
11+
int main()
12+
{
13+
int n;
14+
scanf("%d", &n);
15+
int number_of_prime = 0;
16+
for (int i = 2;; i++)
17+
{
18+
int divisors = 0;
19+
for (int j = 1; j <= i; j++)
20+
{
21+
if (i % j == 0)
22+
{
23+
divisors++;
24+
}
25+
}
26+
if (divisors == 2)
27+
{
28+
number_of_prime++;
29+
if (number_of_prime == n)
30+
{
31+
printf("%d", i);
32+
break;
33+
}
34+
}
35+
}
36+
37+
return 0;
38+
}

0 commit comments

Comments
 (0)