Continued Fractions

8 views
Skip to first unread message

Peter Farrell

unread,
Mar 9, 2015, 10:50:46 PM3/9/15
to mathf...@googlegroups.com
Oleg made the suggestion of using Python to explore Continued Fractions, one of Don Cohen's favorite fields of exploration. A quick look at Wikipedia reminded me the continued fraction for Phi, the golden mean, is 1 + (1/(1 + 1/(1 + ...

An easy way to do this in Python is using recursion (covered in my book, though not for continued fractions). Look at this code:

from __future__ import division

def Phi(n):
    if n == 0:
        return 1
    if n > 0:
        return 1+(1/(Phi(n - 1)))

We're defining a function called "Phi" but within the definition we're using the Phi function! Any sensible human would refuse such a silly request (I tell my students that's like a recipe for a loaf of bread including the step "Bake a loaf of bread") but apparently computers have no trouble with it.

The function will execute n "levels" of the fraction. You can create another function to print out the approximations after each level:

def printPhi(num):
    for i in range(num):
        print Phi(i)


It works! Try it.

Peter Farrell

Oleg Gleizer

unread,
Mar 10, 2015, 12:29:19 AM3/10/15
to mathf...@googlegroups.com
Hi Peter,

I was looking for a good book that uses computer programming to support the math we teach at the Math Circle for quite some while - yours seems to be exactly the one I was looking for!

We are approaching the finals' week here at UCLA. I will only be able to start working on the new course for the Math Circle once I am finished grading, in a couple of weeks from here. We will go very slow, because my class at the Circle are mostly 6-graders. Most of them are very talented - I have seven national champions in various math competitions in my class - but they are still 6-graders. Some of them will be faster than me with Python, but continued fractions would be quite hard for them at this age.

I will keep you posted on our progress and I would probably be asking some questions regarding programming.

Oleg
--
You received this message because you are subscribed to the Google Groups "MathFuture" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathfuture+...@googlegroups.com.
To post to this group, send email to mathf...@googlegroups.com.
Visit this group at http://groups.google.com/group/mathfuture.
For more options, visit https://groups.google.com/d/optout.

kirby urner

unread,
Mar 10, 2015, 9:02:31 AM3/10/15
to mathf...@googlegroups.com
More links on continued fractions with Python
Some material CFs as well as Bernoulli Numbers with Python here: 

http://www.4dsolutions.net/ocn/numeracy0.html

I'm looking for the non-recursive Python algorithms for continued fractions I used to know.

Kirby




--

Peter Farrell

unread,
Mar 10, 2015, 2:55:48 PM3/10/15
to mathf...@googlegroups.com
Thanks, Kirby,

Lots of great resources for Python exploring!

Peter

kirby urner

unread,
Mar 10, 2015, 3:18:36 PM3/10/15
to mathf...@googlegroups.com
Yeah thanks!

I'm showing off a publishing workflow a student might adopt in wanting to build a portfolio:  learn enough programming to control something that in turn makes professional (magazine quality) graphics and/or 3D printer files. 

Have those outputs be your work then, and part of what you showcase on your web pages (possibly employer-facing).  Just as we teach LaTeX or other markup to help convey mathematical symbols in published works, so should we look for ways to make colorful, textured, sharable graphical content, both moving and still.

POV-Ray is great for the ray tracer but I'm well aware it's but one of many tools in that niche.  There's an overwhelmingly vast API to master if one wishes to get a PhD in povray.org documentation, but mastering the basics is not that daunting a proposition.  I get by with just a tiny subset of the full blown scene description language.  We're learning math and making art, not trying to become astronauts on first bounce.

Kirby

Relevant posting about this workflow:
http://mathforum.org/kb/thread.jspa?threadID=2682957



John Bibby

unread,
Mar 10, 2015, 3:32:01 PM3/10/15
to mathf...@googlegroups.com
No. It's more like saying "to make a big loaf of bread, first make some smaller ones" I.e. Do it for n-1 and then do it for n. JOHN BIBBY
--

kirby urner

unread,
Mar 10, 2015, 3:35:30 PM3/10/15
to mathf...@googlegroups.com
On Tue, Mar 10, 2015 at 12:32 PM, John Bibby <johnbibby...@gmail.com> wrote:
No. It's more like saying "to make a big loaf of bread, first make some smaller ones" I.e. Do it for n-1 and then do it for n. JOHN BIBBY



I.E. learn to make bread molecules, and once you get there, recurse up the tree to return a loaf.




kirby urner

unread,
Mar 10, 2015, 3:38:06 PM3/10/15
to mathf...@googlegroups.com
Of course it might not be a tree but I'd think a forking genetic algorithm would be a good way to get bread in a hurry. 

That way, just one lucky process (branch) needs to figure out the Secret of the Bread Molecule, and voila, you've got your loaf.

But you might get meat loaf instead, if you under specify.

Kirby


Peter Farrell

unread,
Mar 11, 2015, 1:41:39 PM3/11/15
to mathf...@googlegroups.com
About POV-Ray, I just remembered Vapory, "a library to render POV-Ray scenes directly from Python" on Zulko's blog here:


I'll try it out during all my free time.

Peter

kirby urner

unread,
Mar 11, 2015, 2:03:27 PM3/11/15
to mathf...@googlegroups.com

Hey that looks useful!

Even just eyeballing his Python then looking at POV-Ray Scene Description Language is instructive and positively reinforcing.

There's much to be said for isomorphic notations.

Thanks for the referral!

Kirby

Peter Farrell

unread,
Mar 11, 2015, 2:16:13 PM3/11/15
to mathf...@googlegroups.com
No problem, Kirby,

I read with great interest Zulko's post about his answer to Processing, Gizeh. I installed it and tried to get it to work without any success. You can read the long sad story in the comments to his blog post about Gizeh.

I just installed POV-Ray and Vapory and couldn't get the first example to work. Maybe it's my installing/Pythonpath skills.

Peter

--
You received this message because you are subscribed to a topic in the Google Groups "MathFuture" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mathfuture/t_LmgccTIwM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mathfuture+...@googlegroups.com.

kirby urner

unread,
Mar 11, 2015, 3:43:53 PM3/11/15
to mathf...@googlegroups.com


I would recommend just getting povray to work all by its lonesome.  It comes with example files.

If you want some Python code to run that spits out a povary-ready .pov file, here's some fresh code:

https://mail.python.org/pipermail/edu-sig/2015-March/011203.html

The graphic it makes, when rendered:

https://www.flickr.com/photos/kirbyurner/16140315443/

I installed and ran this stuff on Mac OSX i.e.

$ povray +H768 +W1024 +Q9 +A0.3  bridges.pov


is how it looks at the command line, once the Python script writes the .pov.
Kirby
Reply all
Reply to author
Forward
0 new messages