C Prime Factor The prime factors of any integer should be fa
C++
Prime Factor
The prime factors of any integer should be familiar. You find all the prime integers that divide exactly
(without remainder) into an integer. We exclude 1 (which is a factor of every integer) and the number
itself (which, again, is a prime factor of every particular number).
•for example, the prime factors of 30 are 2,3,5 since 2*3*5=30. Note that 6, 15 and 10 are also factors but they are
not prime factors.
Powerful Number
A powerful number is an integer with the following property. For every prime factor in the number, the square of the prime factor must divide exactly (without remainder) into the original number.
•30is not a powerful number. Its prime factors are 2,3,5. 2^2 does not divide exactly into 30, 32 does, 52
does not. All of the factors squared must divide exactly into the number for it to be powerful.
•72 is a powerful number. Its prime factors are 2,3 (2^3* 3^2= 72). 2^2 divides exactly into 72 (18* 4) as does 3^2
(9 * 8).
Perfect Power
A perfect power number is an integer that can be expressed as a single expression of the form m^k. That is, there is some integer m which, raised to the integer power k, is the integer n in question.
Functions
function: is_prime : return is bool. Argumentis a singlelongn.If n is prime it returns true,otherwise it returns false.
function:is_powerful: return is bool. Argumentis a singlelongn.If n is powerful it returns true,otherwise it returns false.
Utilizes is_prime (or should).
function: is_perfect_power: return is bool.Argumentis a single long n. If n isa perfect power it returns true, otherwise
it returnsfalse.
function: is_achilles: return is bool.Argument is a single long n. If n is an Achilles number i treturns true, otherwise it returns false.
Utilizesis_powerful and is perfect_power(orshould).
Solution
#include <iostream>
using namespace std;
bool is_prime(long n)
{
for(int i=2;i<=(n/2);i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
bool is_powerful(long n)
{
long count,temp;
temp = n;
for(int i=2;i<=(temp/2) && n!=1;i++)
{
count=0;
while(n%i==0)
{
n = n/i;
count++;
}
if(count==1)
{
return false;
}
}
return true;
}
bool is_perfect_power(long n)
{
if(is_prime(n))
{
return true;
}
long count=0,temp;
temp=n;
for(int i=2;i<=(temp/2) && n!=1;i++)
{
while(n%i==0)
{
n = n/i;
count=1;
}
if(count==1)
{
if(n==1)
{
return true;
}
else
{
n = temp;
count=0;
}
}
}
return false;
}
int main()
{
cout<<is_prime(23)<<endl;
cout<<is_prime(24)<<endl;
cout<<is_prime(2)<<endl;
cout<<\"********\"<<endl;
cout<<is_powerful(72)<<endl;
cout<<is_powerful(30)<<endl;
cout<<is_powerful(23)<<endl;
cout<<\"********\"<<endl;
cout<<is_perfect_power(23)<<endl;
cout<<is_perfect_power(216)<<endl;
cout<<is_perfect_power(33)<<endl;
return 0;
}


