Except for the number 1, the composite numbers are black and the prime numbers are light blue. The only advantage to a separate function is the sieve could be extended to n from the get go, which might be more efficient. Then again, simply does give this, so I'm not sure if a separate function is needed. If the user wants that they can slice the list returned by the function or use. Since all primes are always computed in the sieve anyway, I don't think the function needs to accept an argument to compute the the kth to nth prime (what sieve does). I think it would be simpler to have a function that returns the first n primes. Now, according to formal definition, a number ‘n’ is prime if it is not divisible by any number other than 1 and n. Approach 1: Print prime numbers using loop. Perhaps there could be a better interface for calling that that returns a list rather than an array.Īlso, sieve confusingly uses 1-based indexing, but slices still use the semantics where the last element is not included, so sieve returns the first 10 primes. Then use a for loop to iterate the numbers from 1 to N Then check for each number to be a prime number.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |