Answer the question
In order to leave comments, you need to log in
Looking for prime numbers?
Hello everyone, help with the program for finding prime numbers from the textbook:
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int i, j;
for (i = 2; i < 1000; i++){
for (j = 2; j <= (i / j); j++){
if (!(i % j)){
break;
}
}
if (j > (i / j)) cout << i << " - simple number\n";
}
_getch();
return 0;
}
Answer the question
In order to leave comments, you need to log in
Let's say i is composite, i.e. decomposes into p 1 *p 2 *..*p n , then, to make sure that it is composite, it is enough to find its smallest divisor (let it be p 1 ).
But it is definitely not greater than the product of the remaining divisors (p 2 *..*p n ) == i / p 1 - that's all.
As for the longclaps
range , I wrote it correctly, I will explain the rest:
This block of code:
if (!(i % j)){
break;
}
if (j > (i / j))
checks whether the cycle of iterating over divisors has completely ended, if j > (i / j) means that the inner loop was not interrupted, which means that there were no divisors for i, and therefore it is simple and i is displayed on the screen.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question