[Edu-sig] Python programming challenge
Kirby Urner
urnerk at qwest.net
Wed Jun 1 08:16:06 CEST 2005
Here's a good assignment for those wanting to test their Python skills:
Consider the paper and pencil algorithm for finding the square root of a
number as described in detail here, by means of a worked example:
http://www.homeschoolmath.net/other_topics/square-root-algorithm-example.php
Write a Python generator that yields one more digit of the square root of n,
given n as its argument. Optionally ignore placement of any decimal points.
Feel free to use more than one function, but have root2 be the top-level
generator.
In other words, your code might output something like this during testing:
>>> r = root2(297504)
>>> r.next()
'5'
>>> r.next()
'54'
>>> r.next()
'545'
>>> r.next()
'5454'
>>> r.next()
'54543'
>>> r.next()
'545439'
>>> r.next()
'5454392'
>>> r.next()
'54543927'
>>> r.next()
'545439272'
>>> r.next()
'5454392725'
And so on, indefinitely (memory a constraint).
Kirby
More information about the Edu-sig
mailing list