Hello!
I wanted the program telling if the number is a prime number or not.
Then, I also wanted, if the number is NOT prime, the program to write out at least one other prime number we can devide it by.
F.e, if we imput 119, I wanted at least 7 to be written out.
bool Prime (constint st)
{
bool answer = true;
for (int i = 2; i < st; i++)
if (st % i == 0)
{
cout << i << endl; // it prints out the first prime divisor
answer = false; // the number is not prime
break; // no need to continue the for loop
}
return answer;
}
Hello, Condor!
If we check if is a prime number or not ,
\squrt(77)= 8...
77=7*11
8>7
\\sqrt(119)=10...
119=7*17
7<10
itn
Square root from each number is really bigger then the first prime divisor, bt sqrt is sqrt and divisors are divisors, integers, so how can we connect it loggiclaly and see it in advance as a general rule?
Many thanks!
But how do we see it on eyes for general?
Many thanks!
OK. square root of 77 is greater than 8 and but we will take (int)sqrt(77).
Generally speaking, if a number n is not prime then it has necessarily a prime divisor d1 <= sqrt(n) who has a "pair" d'1 = n / d1 >= sqrt(n).
More than that, if n is a perfect square then one of the divisors will be di = sqrt(n) and d'i = n / di = sqrt(n) as his "pair". In such cases d'i = di
We may therefore look for divisors between sqrt(n) and n, but there are more numbers in that interval and the for loop will be "longer".
So, when we're looking for divisors of n it is enough to seek up to thesqrt(n).
OK, that line is to define and initialize the bool variable as true. When in the for loop the expression st % i == 0 is true then, obviously, answer = false (the number is not prime) and break; will stop the loop. That's all.
#include<iostream>
usingnamespace std;
bool Prime (int st)
{
bool answer = true;
for (int i = 2; i <= sqrt(st); i++)
if (st % i == 0)
{
cout <<"First prime divisor " << i << endl;
answer = false;
break;
}
return answer;
}
int main()
{
int st;
cout<<"Write number you wannna tes.: ";
cin>>st;
cout<<Prime(st);
cin.get();
return 0;
}
Write number you wannna tes.: 119
Write first prime divisor 7
0
Write number you wannna tes.: 169
Write first prime divisor 13
0
Write number you wannna tes.: 67
1
Line 9 for (int i = 2; i <= sqrt(st); i++) is probably inefficient. I don't think the compiler can optimise this, so the function call to sqrt() is re-executed on each iteration. It's safer in terms of efficiency to calculate and store the sqrt() before the start of the for loop, and test against the stored value.
#include <iostream>
#include <cmath>
usingnamespace std;
bool Prime (int st, int sqr)
{
bool answer = true;
for (int i = 2; i <= sqr; i++)
if (st % i == 0)
{
cout <<"Write first prime divisor " <<i << endl;
answer = false;
break;
}
return answer;
}
int main()
{
int st;
cout<<"Write number you wannna tes.: ";
cin>>st;
int sqr = sqrt(st);
cout<<Prime(st, sqr) << endl;
cin.ignore();
cin.get();
return 0;
}
Write number you wannna tes.: 119
Write first prime divisor 7
0
Write number you wannna tes.: 169
Write first prime divisor 13
0
Write number you wannna tes.: 67
1