This algorithm won't work as primes might have different power. for
eg. N=12

12 is divisible by 2 so it ends up being 3. then 3 divides by 3. So it
prints possible. But the actual answer is no.
Correct code:

for (int i=2;i<=sqrt(n);i++)
{
    float ans = log(n)/log(2);
    int an = ans;
    if (ans-an==0 && an!=1)
    {
        cout<<"possible "<<i<<"^"<<an<<" = "<<n;
        break;
    }
}
if (i>sqrt(n))
{
    cout<<"not possible";
}

On Dec 26, 9:03 pm, SAMMM <somnath.nit...@gmail.com> wrote:
> From Wht I can understand from problm to check whether N can be
> expressed a m^n : Eg: 1331=11^3
>
> What comes to my mind is to get all prime factors from 2 to SQRT(N)
> [Prime Sieve] , Here N is the Given Integer .
>
> Now Iterate over the prime number(p) from 2 to Sqrt(N)
>  do
>    T=N;
>    if(!(T%p)) while(!(T%p)) T/=p;
>    if(T==1) {printf("Yes possible");break;}
>  done
>  if (p>Sqrt(N)) printf("Not Possible");

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to