Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sol1.c File Reference

Problem 10 solution More...

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for sol1.c:

Functions

char is_prime (unsigned long n)
 Function to check if a number is prime.
 
unsigned long long sum_of_primes (unsigned long N)
 Computes sum of prime numbers less than N.
 
int main (int argc, char *argv[])
 Main function.
 

Detailed Description

Problem 10 solution

Author
Krishna Vedala

Function Documentation

◆ is_prime()

char is_prime ( unsigned long  n)

Function to check if a number is prime.

12{
13 for (unsigned long i = 2; i < sqrtl(n) + 1; i++)
14 if (n % i == 0)
15 return 0;
16
17 return 1;
18}

◆ main()

int main ( int  argc,
char *  argv[] 
)

Main function.

34{
35 unsigned long n = 100;
36
37 if (argc == 2) /* if command line argument is provided */
38 n = atol(argv[1]); /* use that as the upper limit */
39
40 printf("%ld: %llu\n", n, sum_of_primes(n));
41
42 return 0;
43}
unsigned long long sum_of_primes(unsigned long N)
Computes sum of prime numbers less than N.
Definition sol1.c:21
Here is the call graph for this function:

◆ sum_of_primes()

unsigned long long sum_of_primes ( unsigned long  N)

Computes sum of prime numbers less than N.

22{
23 unsigned long long sum = 2;
24
25 for (long i = 3; i < N; i += 2) /* skip even numbers */
26 if (is_prime(i))
27 sum += i;
28
29 return sum;
30}
char is_prime(unsigned long n)
Function to check if a number is prime.
Definition sol1.c:11
Here is the call graph for this function: