Sieve of Eratosthenes and its algorithm
It is a simple and ancient method for finding all Prime numbers less than or equal to N and here the value of N is one thousand.
Algorithm to find the sum of Prime numbers less than or equal to one thousand by Sieve of Eratosthenes,
- We create a boolean array of size equal to the given number (N) and mark each position in the array True.
- We initialize a variable p equal to 2 and s equal to 0.
- If the variable p is prime then mark each multiple of number False in the array.
- Update the variable p by an increment of 1 i.e p = p+1.
- Repeat step 2 until the square of the variable is less than the given number (N).
- The elements in the array with True contain all Prime numbers less than or equal to the given number and the elements of the array which is our Prime number.
- After the above process, we will simply find the sum of the prime numbers.
Code:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer