You need to know if the number is simple. I tried to take the first solution from here ( Is the number simple ) and stuff it into my code, but then it turns out that 1 is simple, 4 is simple, but it is not. Please do not need complex algorithms that supposedly increase performance, not before me now. I want to understand why my code does not work (skips 4k, for example). And since I took the code from that author, I wonder why his answer is positive.
#include <stdio.h> int main() { bool simple = true; int n; scanf_s("%d", &n); for (int i = 2; i < sqrt(n); i++) { if (n == 1) simple == false; if (n % i == 0) { simple = false; return 0; } } if (simple) printf("YES"); else printf("NO"); return 0; }
должно быть i<=sqrt(n), иначе квадрат простого числа тоже будет простым.- Shadasviar