While Loops

5 / 5

A number $n > 1$ is prime if there is no number $i$ with $1 < i < n$ such that $n$ is divisible by $i$.

  • $2$ is prime because there are no numbers between $1$ and $2$;
  • $3$ is prime because it is not divisible by $2$;
  • $4$ is not prime because it is divisible by $2$;
  • $5$ is prime because it is not divisible by either $2$, $3$, or $4$;
  • $6$ is not prime because it is divisible by $2$ and $3$.

To check whether n is divisible by i, we can use remainder after division. In Python, this is obtained by the % operator.

n = 15
i = 4
print(n%i) # output: 3
i = 3
print(n%i) # output: 0

To check whether n is divisible by i, we can use n%i==0.

All prime numbers

Input an integer n. Print all prime numbers from 2 to n separated by spaces. For example, if n=17 then the output is 2 3 5 7 11 13 17.

Congratulations!

You passed the !

Welcome!

Would you like a guided tour on how to use Vibewise?

Your email has been verified!