Seive's Algorithm for sum of all prime numbers less than one million
Almost all programmers have written a prime detection function in their Programming 101 stage. This problem seems to be easily solved by a beginner and you’re right it is an easy problem, but many of us approach it the wrong way. The most obvious way would be to make a function that classifies a number as prime or not and then another function that loops through all the numbers up to a million and then checks each number. There’s nothing wrong with this but depending on the prime classifier function, calculating the sum of all prime numbers up to 1 million can take anywhere from 1 second to 10 minutes! Let me write a prime classifier function first … function classifyPrime ( num ) { if ( num < 2 ) return false ; else { for ( let i = 2 ; i < num ; i ++ ) { if ( num % i === 0 ) return false } } return true ; } Now to get the sum we loop over all numbers up to 1 million let sum = 0 ; for (