0

C problem

I found this methode to check wether a number is prime ir not but i didn't get the meaning of that i*i ..? _________________________ int isPrime(int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return 0; return 1; } ___________________________

3rd Jun 2019, 5:38 PM
Dynno
Dynno - avatar
1 ответ
+ 1
It's just to make the function shorter, because if there is a number bigger than the square root of the number that is divisible by it, then there must be a number smaller than the square root. It would perfectly work like this: for (int i = 2; i < n; i++)... (although this isn't perfect, because it will register 1 as a prime number too)
3rd Jun 2019, 6:00 PM
Airree
Airree - avatar