recursion depth problem

mensanator at aol.com mensanator at aol.com
Sun Apr 22 23:45:44 EDT 2007


On Apr 22, 9:13�pm, proctor <12cc... at gmail.com> wrote:
> On Apr 22, 7:10 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
>
>
>
>
>
> > On 22 Apr 2007 17:06:18 -0700, proctor <12cc... at gmail.com> declaimed the
> > following in comp.lang.python:
>
> > > >     else:
> > > >         # only one of carry in, b1, or b2 is set
>
> >                         #or none is set! Missed the 0,0,0 condition <G>
>
> > > >         return (0, b1 + b2 + c)
>
> > > thank you.  you guys are keeping me busy!
>
> >         Heh... I'm sure what I scratched together could be optimized more
> > (make functions out of the input/output conversions; add some error
> > checking on input data, allow for non-list arguments in adder()...)
>
> >         After all, if one wants a binary counter, one should implement a
> > binary addition operation, and basic digital has ways... Unfortunately,
> > Python now has a Boolean type, so boolean AND/OR doesn't give the
> > desired results... And using bitwise seems wasteful <G> However...
> > replace the badd() with the following obscure thing if you really want
> > to get close to hardware emulation...
>
> > def badd(b1, b2, ci=0):
> >     """
> >         badd(b1, b2, ci) => (co, sum)
> >     """
> >     (co1, hs1) = (b1 & b2, b1 ^ b2)
> >     (co2, hs2) = (ci & hs1, ci ^ hs1)
> >     return (co1 | co2, hs2)
>
> >         A pair of "half-adders"; and the extra bit to make a "full-adder".
> > It wouldn't be efficient to do it as one long return, just due to
> > duplicated (b1 ^ b2) sub-terms
>
> > return ( (b1 & b2) | (ci & (b1 ^ b2)), (ci ^ (b1 ^ b2)))
>
> > but it does work <G>
> > --
> >         Wulfraed        Dennis Lee Bieber               KD6MOG
> >         wlfr... at ix.netcom.com             wulfr... at bestiaria.com
> >                 HTTP://wlfraed.home.netcom.com/
> >         (Bestiaria Support Staff:               web-a... at bestiaria.com)
> >                 HTTP://www.bestiaria.com/
>
> :-)
>
> this is good stuff.  for learning especially!  thank you again!

I once made a complete full adder in perl with strings
so that I could have unlimited precision (this is before I found
out about Big Arithmetic libraries). Since I was only doing the
Collatz Conjecture, all I needed was to multiply by 3 (shift
left by appending '0' to string and adding to original string
using the full adder), dividing by two (shift right by slicing
off rightmost character from string) and incrementing
(pre-set the carry bit to '1').

It worked perfectly.

But it was slower than snake shit.

>
> proctor




More information about the Python-list mailing list