Loading...

algogeeks@googlegroups.com

[Prev] Thread [Next]  |  [Prev] Date [Next]

[algogeeks] Re: Implement division without using divide operator Vinay Chilakamarri Tue Oct 09 12:09:53 2007

in the previous logic, dont forget to address a negative divisor, dividend
and alternative cases like those... it shud be pretty easy

On 10/8/07, Vinay Chilakamarri <[EMAIL PROTECTED]> wrote:
>
> hey there i did play around with bit shifting sometime ago and was able to
> do that.. but yeah i guess the one i posted is a bit different from what i
> actually did before and is not applicable here. here is what we can do
>
> int funcNoDiv(int divisor, int dividend)
> {
> if(divisor == dividend)
>   {
>      return 0;
>   }
>
>   else if( divisor > dividend)
>   {
>       return dividend;
>   }
>
>   else // divisor < dividend
>   {
>      while(divisor <= dividend)
>     {
>         divisor += divisor;
>     }
>
>   return dividend - divisor; //eg: pass 2, 19 ; you get 1 as return value
> }
>
> Add a condition for 0 divisor in the above logic.. Ill post the bit
> shifting algo soon
>
> ~_
> On 10/7/07, Ajinkya Kale <[EMAIL PROTECTED]> wrote:
> >
> > @Vinay : Can you please explain the algorithm of your code..
> >
> > On 10/7/07, Vinay Chilakamarri < [EMAIL PROTECTED]> wrote:
> > >
> > > How about recursive way:
> > >
> > > int funcNoDiv(int divisor, int dividend)
> > > {
> > > //base case
> > >  if(divisor == dividend)
> > >   {
> > >      return 1;
> > >   }
> > >
> > >   else if( divisor > dividend)
> > >   {
> > >       return dividend;
> > >   }
> > >
> > >   else // divisor < dividend
> > >   {
> > >      while(divisor <= dividend)
> > >     {
> > >         divisor = divisor << 1;
> > >          remainder = remainder << 1;
> > >      }
> > >
> > >   //now divisor > dividend
> > >
> > >    divisor = divisor >> 1;
> > >    dividend = dividend >> 1;
> > >    quotient = quotient + funcNoDiv(dividend - divisor, tempdivisor)
> > > }
> > > On 10/7/07, megha <[EMAIL PROTECTED] > wrote:
> > > >
> > > >
> > > > Hi all,
> > > >
> > > > I am looking for the program/algorithm to implement division without
> > > > using divide operator?
> > > >
> > > > Any idea?
> > > >
> > > > Thanks
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > > > > >
> > >
>

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