Created
September 15, 2022 09:36
-
-
Save Josephchinedu/c924afdcc860ff934e861501c25e4c8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Description: | |
Count the number of prime numbers less than a non-negative given number (N) | |
What is a prime Number: | |
Any number that's greater than one (1) and non - divisible by anything except One(1) and itself | |
Steps: | |
1. Define a boolean array of size n and set all element to True except index 0 and 1 | |
e.g: n = 5 | |
array = [False, False, True, True, True] | |
2. Start a loop with an index of i, from 2 till the sqaure root of N | |
Why we start the loop from 2, | |
it's because, we've set index 0, 1 to be False, there's no need to go over them again | |
Then if the current number is a prime number, if it's set to True in our array, we know for a fact that all of his multiples | |
cannot be prime, since prime number has no divider except 1 and itself. | |
The multiple of the current number are divided by our current number by 1, so we loop over all the multiples of 1 and we'll set | |
them to fasle | |
When we're done we'll return the sum number of True in our array. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment