Lab 9: Fractals Due: 08:00:00AM on 2022-12-02


This lab has four components, “GCD”, “Stairs”, “Sierpinski”, and “Recursive H”, all of which should be implemented in the same file. Get started by downloading the starter file Replace each pass statement in the required functions with your implementation.

The purpose of this lab is practice recursion and recursive implementations, so all function must be implemented recursively (the only loops allowed are for drawing triangles).

Greatest Common Divisor

One of the oldest algorithms, dating back to the 5th century BCE, is Euclid’s algorithm for computing the greatest common divisor (GCD) of two integers. It works as follows:

Write a recursive function gcd(a,b) that implements Euclid’s algorithm to return the GCD of any two positive integer parameters a and b. Your function should return the GCD and not use the input or print functions. For example:

>>> gcd(36, 81)
>>> gcd(13, 6)



Using turtle graphics, write a recursive function stairs(length, levels) to draw pictures such as those shown above. Assume the turtle starts in the top left corner, facing right (always test using stairs_demo, which will set the turtle’s starting position for you). The first square should have a side length of length, and each subsequent square should have 1/2 the size. There should be a total of levels squares. Do this all in one continuous drawing motion; do not pick up the pen, and do not repeat any lines. Employ the following strategy:

  1. draw a half-square,
  2. recursively draw the rest of the squares,
  3. draw the last half-square to return to the starting position and orientation. Note that powers of 2 work well for starting lengths.

Use the stairs_demo function in the skeleton file for testing.

Sierpinski Triangle

Next we’ll draw another interesting fractal shape called the Sierpinski triangle. Here is what it looks like taken out to six generations:

Sierpinksi triangle

There are many ways to draw this shape, but here is one. Start with the assumption that when you draw the triangle, the turtle will be end up in the same position and orientation it started from. Given that assumption, drawing a generation n triangle just involves drawing three n-1 triangles with sides half the length, like shown below.

Sierpinksi triangle

Draw a generation n-1 triangle, move to the location of the next n-1 triangle, draw again, move to the last triangle and draw that one, then restore the turtle to the original location and orientation. Generation 1 is just a single equilateral triangle.

More specifically, here are the drawings for generations 0, 1, 2, and 3:

Sierpinksi triangle

Write a function sierpinski(length, iterations). The parameter length will be the length of the base of the figure and iterations will determine how many generations to draw. Use the sierpinski_demo function in the starter file for testing.

Recursive H

A recursive H is an H where the end of each vertical line of the H may have another recursive H. The recursive H is defined by a level.

A level 1 recursive H is just an H:


A level 2 recursive H has another smaller H at the end of each line:


A level 3 recursive H has another smaller H at the end of the each of these Hs:


Notice that the smallest level of H always ends in a dot (a “level 0” H).

The size of an H is defined by the parameter length. The vertical bars are of length length and the horizontal bar of length 2 * length. Assume that the turtle starts in the middle of the drawing window, and thus the middle of the horizontal bar, facing to the right. At the end of drawing, the turtle should be at the same position and heading as when it started.

H1 Arrow

Write a function named recursive_h that takes two parameters: the length of the vertical bars (the horizontal bar will be twice the length) and number of recursive levels to draw. Level 1 should just draw an H with four dots at the end, level 2 an H with 4 smaller H’s at the end of the vertical bars, and so on. The smallest H’s will all have black dots at the end of their vertical bars. As shown in the images, the length of the vertical bar (length) should be halved with each level.

Use the recursive_h_demo function in the starter file for testing.

Creativity points

You may earn up to 2 creativity points on this assignment. Below are some ideas, but you may incorporate your own if you’d like. Make sure to document your extra point additions in comments at the top of the file.


When you’re done

Make sure that your program is properly documented:

In addition, make sure that you’ve used good code design style (including meaningful variable names, constants where relevant, vertical white space, etc.). For this lab, when run your program should call no functions and produce no output.

When you’re done, call the function drawing_demo() to run all three drawing functions, and then take a screenshot.

Submit your program via Gradescope. Your program program file must be named and your screenshot named lab9_screenshot.png (produced from the output of drawing_demo()). You can submit multiple times, with only the most recent submission (before the due date) graded. Note that the tests performed by Gradescope are limited. Passing all of the visible tests does not guarantee that your submission correctly satisfies all of the requirements of the assignment.

Gradescope will import your file for testing so that make sure that no code executes on import. That is when imported your program should not try to draw any images.


Features Points
gcd 4
stairs 5
sierpinski 5
recursive_h 6
Code design and style 5
Creativity points 2
Total 27

FAQ Excerpts Click entry title for more information