Question: Write R code to return a vector of prime numbers within range from 0 to 1 0 0 0 The sieve works by marking the
Write R code to return a vector of prime numbers within range from to The sieve works by marking the composite numbers filtering them out. What is left on top are the prime numbers. Steps: A prime number has only two factors: and itself. A composite number has more than two factors. The number is neither prime nor composite because it has only one factor so we remove Now we move on to number We know that any of the multiples of except cannot be prime because one of their factors is in addition to and itself So we move through multiples of and mark them as composites. Now we move to number If was a factor of any of the numbers seen so far it would have been cancelled. But it is not so the only factors of are and and hence it is prime. Therefore if we reach any number and it has not been cancelled it has to be prime. So we mark three as prime. We move through and cancel all its multiples as they cannot be prime. Lets look at number It has been marked as composite because it had as factor. But what about its multiples? We don't have to consider its multiples because they would have been cancelled already as they would have been multiples of factors of Now we look at Since it has not been cancelled we mark it prime and then mark all its multiples as composites. We only need to do this process till sqrt Why
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
