I don't think that this function is doing what you want it to do. If
you ask for a^b, it returns a^1 in most cases.

Try this instead.

int power(int a, int b)
{
  int result = 1;
  if (b == 1) result = a;
  else if (b>1)
  {
    result = power(a,b/2);
    result *= result;
    if (b%2) result *= a;
  }
  return result;
}

On Aug 8, 10:37 pm, rohit <[email protected]> wrote:
>  int get_power(int a, int b)
> {
> if(!b)
> return 1;
> if(b%2)
>  return a * get_power(a, b/2);
>  return get_power(a, b/2);
>  }
>
> int func(int p)
> { int sum = 0;
>  for(int i = 1; i <= p; ++i)
> {
> sum += get_power(i, 5);}
>
> return sum;
>
> }

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

Reply via email to