mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Merge branch 'project_euler/problem_10' into project_euler/master
# Conflicts: # DIRECTORY.md
This commit is contained in:
commit
51448a7399
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
||||
*.swp
|
||||
*.exe
|
||||
*.out
|
||||
.vscode/
|
||||
|
@ -229,6 +229,9 @@
|
||||
* Problem 09
|
||||
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2009/sol1.c)
|
||||
* [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2009/sol2.c)
|
||||
* Problem 10
|
||||
* [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2010/sol1.c)
|
||||
* [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2010/sol2.c)
|
||||
|
||||
## Searching
|
||||
* [Binary Search](https://github.com/TheAlgorithms/C/blob/master/searching/Binary_Search.c)
|
||||
|
37
project_euler/Problem 10/sol1.c
Normal file
37
project_euler/Problem 10/sol1.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char is_prime(long n)
|
||||
{
|
||||
for (long i = 2; i < sqrtl(n) + 1; i++)
|
||||
if ( n % i == 0)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
long long sum_of_primes(long N)
|
||||
{
|
||||
long long sum = 2;
|
||||
|
||||
for (long i = 3; i < N; i+=2) /* skip even numbers */
|
||||
if (is_prime(i))
|
||||
sum += i;
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
long n = 100;
|
||||
|
||||
if (argc == 2) /* if command line argument is provided */
|
||||
n = atol(argv[1]); /* use that as the upper limit */
|
||||
|
||||
printf("%ld: %lld\n", n, sum_of_primes(n));
|
||||
|
||||
return 0;
|
||||
}
|
51
project_euler/Problem 10/sol2.c
Normal file
51
project_euler/Problem 10/sol2.c
Normal file
@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
long n = 100;
|
||||
long long sum = 0;
|
||||
char *sieve = NULL;
|
||||
|
||||
if (argc == 2) /* if command line argument is provided */
|
||||
n = atol(argv[1]); /* use that as the upper limit */
|
||||
|
||||
/* allocate memory for the sieve */
|
||||
sieve = calloc(n, sizeof(*sieve));
|
||||
if(!sieve)
|
||||
{
|
||||
perror("Unable to allocate memory!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* build sieve of Eratosthenes
|
||||
In the array,
|
||||
* if i^th cell is '1', then 'i' is composite
|
||||
* if i^th cell is '0', then 'i' is prime
|
||||
*/
|
||||
for (long i = 2; i < sqrtl(n); i++)
|
||||
{
|
||||
/* if i^th element is prime, mark all its multiples
|
||||
as composites */
|
||||
if (!sieve[i])
|
||||
{
|
||||
for (long j = i * i; j < n + 1; j += i)
|
||||
{
|
||||
sieve[j] = 1;
|
||||
}
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
|
||||
for (long i = sqrtl(n)+1; i < n; i++)
|
||||
if (!sieve[i])
|
||||
sum += i;
|
||||
|
||||
free(sieve);
|
||||
|
||||
printf("%ld: %lld\n", n, sum);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user