[Tutor] Python performance on Windows system

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 29 Oct 2001 17:07:36 -0800 (PST)


On Mon, 29 Oct 2001, Joel Ricker wrote:

> In the back of my mind I thought that the use of binary numbers to
> pack information would be a better way to go and as I can see I was
> right.  My problem is that its just an idea -- I don't quite know how
> to implement effectively binary constructs to store data.  I know how
> binary numbers work (ie 010 = 2, 1111 = 15) and thats it -- all the
> comp sci classes I've ever taken never went into detail as to how to
> use them.  >> and << operators mean really nothing to me.  I've seen
> them used in code before especially mathematical computations and
> would like to learn more. Can anyone recommend any resources of study
> either online or book form that covers this topic thoroughly?


The book "Structure and Interpretation of Computer Programs" has an
example on finding prime numbers using a probabilistic method:

http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%25_sec_1.2.6

However, if you look at the link, you'll find that all the source code
looks... umm... really parenthesized.  *grin* 


They use a programming language called Scheme for their demonstrations,
but their also explain the theory behind the code, so you should be able
to get some ideas from it.


If someone has time, perhaps he or she can translate some of the source
code from Scheme to Python?  For example, SICP's method on the "expmod",
the exponential of a number modulo another number:

;;;
(define (expmod base exp m)
  (cond ((= exp 0) 1)
        ((even? exp)
         (remainder (square (expmod base (/ exp 2) m))
                    m))
        (else
         (remainder (* base (expmod base (- exp 1) m))
                    m))))
;;;


has a somewhat close translation in Python:

###
def square(x): return x * x

def isEven(x): return x % 2 == 0

def expmod(base, exp, m):
    if exp == 0:
        return 1
    if isEven(exp):
        return square(expmod(base, exp/2, m) % m)
    return base * expmod(base, exp - 1, m) % m
###


But it sounds like you've interested in mathematics; if so, SICP is an
excellent book to take a look at... even if it doesn't use Python...
*grin*


Good luck to you.