Printing prime number from 1 to 100

前端 未结 4 2209
南方客
南方客 2021-02-11 10:15

This program is supposed to output the prime numbers between 1 and 100. Will anyone please explain me the flow the programme below? I am having difficulty in writing the progra

4条回答
  •  有刺的猬
    2021-02-11 10:56

    How would you find a prime number with plain vanilla solution? If number is prime. It will not be a multiple of any number other than itself. So assume number is x. This number will not be divisible by any number when starting from 2 till x-1. Why start from 2 and not 1 because every number is divisible by 1. The above code is trying to replicate the same behavior. To find all primes between 1 till 99 (as per the loop):

    1. From 2 till number from (outer loop - 1)
    2. Try dividing the number and check if it's divisible. (remainder should be zero).
    3. If true number is not prime. Else number is prime.

提交回复
热议问题