Gabriele Ferro Home

Prime numbers and The Sieve of Eratosthenes

To find prime numbers up to a certain number we can go back to ancient Greece.

In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.


Eratosthenes of Cyrene (born in today's Libya about 300 years BC) was

a Greek polymath: a mathematician, geographer, poet, astronomer, and music theorist. He was a man of learning, becoming the chief librarian at the Library of Alexandria.

His work is comparable to what is now known as the study of geography, and he introduced some of the terminology still used today.

He is best known for being the first person to calculate the circumference of the Earth, which he did by using the extensive survey results he could access in his role at the Library; his calculation was remarkably accurate. He was also the first to calculate the tilt of the Earth's axis, once again with remarkable accuracy. Additionally, he may have accurately calculated the distance from the Earth to the Sun and invented the leap day. He created the first global projection of the world, incorporating parallels and meridians based on the available geographic knowledge of his era.

Eratosthenes was the founder of scientific chronology; he endeavoured to revise the dates of the chief literary and political events from the conquest of Troy. Eratosthenes dated The Sack of Troy to 1183 BC.

In number theory, he introduced the sieve of Eratosthenes, an efficient method of identifying prime numbers.

And this is exactly the method I wanted to use to find the solution to the freeCodeCamp challenge.

Day 67: 21 October 2020

**Today’s Progress:**

Completed the Intermediate Algorithm Scripting Challege: Sum All Primes

A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself. For example, 2 is a prime number because it is only divisible by 1 and 2. In contrast, 4 is not prime since it is divisible by 1, 2 and 4.

Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.

First attempt vs solution

Can you spot the off-by-one error?


I probably should have added 1 directly in the initArray function instead of adding it to the input parameter.

**Link to tweet:**

[twitter-follow screen_name='20Gabriele20']

February 2024


Gabriele Ferro Home
Copyright © Ferro Gabriele. All rights reserved.