problem link:
Discuss: In this problem you have to use mathematical fact. if (2 power k − 1) is prime number then the number is perfect. or to check prime number you have to use the number n not the number generate by formula.
try yourself before see the code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int prime(ll n)
{
int flag=0,i,j;
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
flag=1;
}
if(!flag)
return true;
return false;
}
int main()
{
ll n,i,j,m,perfect;
while(cin >>n&&n!=0)
{
m=pow(2,n)-1;
perfect=m*pow(2,n-1);
i=prime(m);
if(i)
cout <<"Perfect: "<<perfect<<'!'<<endl;
else
{
j=prime(n);
if(j)
cout <<"Given number is prime. But, NO perfect number is available."<<endl;
else
cout <<"Given number is NOT prime! NO perfect number is available."<<endl;
}
}
return 0;
}
Discuss: In this problem you have to use mathematical fact. if (2 power k − 1) is prime number then the number is perfect. or to check prime number you have to use the number n not the number generate by formula.
try yourself before see the code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int prime(ll n)
{
int flag=0,i,j;
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
flag=1;
}
if(!flag)
return true;
return false;
}
int main()
{
ll n,i,j,m,perfect;
while(cin >>n&&n!=0)
{
m=pow(2,n)-1;
perfect=m*pow(2,n-1);
i=prime(m);
if(i)
cout <<"Perfect: "<<perfect<<'!'<<endl;
else
{
j=prime(n);
if(j)
cout <<"Given number is prime. But, NO perfect number is available."<<endl;
else
cout <<"Given number is NOT prime! NO perfect number is available."<<endl;
}
}
return 0;
}
No comments:
Post a Comment