[algogeeks] Re: what is complexity of func(p)

2011-08-09 Thread Don
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 (b1)
  {
result = power(a,b/2);
result *= result;
if (b%2) result *= a;
  }
  return result;
}

On Aug 8, 10:37 pm, rohit rajuljain...@gmail.com 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 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.



Re: [algogeeks] Re: what is complexity of func(p)

2011-08-09 Thread shady
he is questioning the complexity and not the algorithm... btw, you are right


On Tue, Aug 9, 2011 at 8:41 PM, Don dondod...@gmail.com wrote:

 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 (b1)
  {
result = power(a,b/2);
result *= result;
if (b%2) result *= a;
  }
  return result;
 }

 On Aug 8, 10:37 pm, rohit rajuljain...@gmail.com 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 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.



-- 
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.



Re: [algogeeks] Re: what is complexity of func(p)

2011-08-09 Thread rajul jain
thanks to all

On Tue, Aug 9, 2011 at 8:45 PM, shady sinv...@gmail.com wrote:

 he is questioning the complexity and not the algorithm... btw, you are
 right


 On Tue, Aug 9, 2011 at 8:41 PM, Don dondod...@gmail.com wrote:

 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 (b1)
  {
result = power(a,b/2);
result *= result;
if (b%2) result *= a;
  }
  return result;
 }

 On Aug 8, 10:37 pm, rohit rajuljain...@gmail.com 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 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.


  --
 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.


-- 
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.



[algogeeks] Re: what is complexity of func(p)

2011-08-08 Thread Dave
@Dipankar: And O(log 5) = 1, so the complexity is O(p).

Dave

On Aug 8, 10:43 pm, Dipankar Patro dip10c...@gmail.com wrote:
 This is a simple one:

 get_power has a complexity of O(logb), since it is dividing b by 2 each time
 it is called.
 and the get_power func is called for p times.

 the overall complexity is O(plog5) , p times O(log5)

 On 9 August 2011 09:07, rohit rajuljain...@gmail.com 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 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.

 --
 ___­

 Please do not print this e-mail until urgent requirement. Go Green!!
 Save Papers = Save Trees

-- 
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.