Skip to main content

Posts

Featured

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 (

Latest Posts

5 Most Common Security Threats & How You Can Prevent Them

7 Most Dangerous Hackers Of All Time

Record your screen online for free : RecordScreen.io

How to know if your email address has been compromised(hacked)

HSCTF 8, WeCTF 2021 - writeups (started after a quiet a while)

“Over the Wire” : “Bandit” - Walkthrough (level 0-34)