[Edu-sig] Brute force solutions
urnerk at qwest.net
Wed Sep 21 01:58:32 CEST 2005
Per my 'Pentagon math' thread, I think the golden ratio (phi) is an
important one to explore in K-12. It's one of those key nodes in our
Given a regular pentagon, the ratio of a diagonal to an edge is phi. In
other words, that famous pentacle pattern is phi-edged, vis-à-vis a
containing pentagon of edges 1.
Although we have many closed form algebraic methods for deriving phi,
computers give us this power to solve through brute force. Some may frown
on using such "mindless methods" but I'm thinking to go ahead and introduce
them -- not exclusively, but as an exhibit along the way.
In the Python function below, we're looking for a way to break a line of
unit length into two segments, the shorter 'a' and the longer 'b', such that
the ratio b:a is as close as feasible to the ratio of the whole to b (1:b).
We move 'a' to the right by tiny increments, and keep track of which value
minimizes the difference between these two ratios:
>>> def findphi(step = 0.01):
Lengthen a stepwise, while testing how closely
b/a approaches 1/b, and return b/a for the least
diff = b = 1.0
a = phi = 0
while a < b:
a += step
b = 1 - a
e = abs(b/a - 1/b)
if e < diff:
phi = b/a
diff = e
Some lessons that might be learned from this approach:
(1) when working with floats, you want to compare differences, not check for
equalities, e.g. looking for when b/a == 1/b would be a bad idea.
(2) you get greater accuracy in exchange for more cycles
(3) visualization helps
The last point is especially important. The code itself says nothing about
lines. The diagram is what explains the logic behind the algorithm -- which
is why it's included right in the comments, as primitive ASCII art (bad
form, or a good idea?).
PS: happy birthday Dawn Wicca (9/20/2005)
More information about the Edu-sig