Pythagorean Approximations Assignment

(Scratch programming and spreadsheet analysis for LEGO robot building)


In LEGO kits for building robots, there is generally a substantial supply of "beams" or "liftarms" in various lengths (e.g., see beam image) as well as pins (e.g. as shown in this image of pins from to hold them together.

While LEGO beams come in varying lengths, they all have holes spaced at a regular interval that we will refer to as one LEGO unit (about 8mm). In most LEGO constructions, beams are generally placed horizontally or vertically along an underlying virtual grid where the holes fall. But it may also be desirable to place beams along a diagonal as in this image of a triangular construction where most of the beams are horizontal and vertical but a set of gears is sandwiched between beams running diagonally. In this example, the beams essentially form a right triangle with sides of length 7, 11, and 13 in LEGO units, but these lengths do not exactly satisfy the Pythagorean Theorem.

Recall that the Pythagorean Theorem provides a relationship between the side lengths in a right triangle, a triangle that contains a right (90) angle. It tells us that

a2 + b2 = c2
where c is the length of the hypotenuse (the longest side, which is opposite the right angle) and a and b are the lengths of other sides (legs). (A nice site with graphical illustrations is

In the previously referenced LEGO example, we seem to have a violation of the Pythagorean Theorem:

72 + 112  =  49 + 121  =  170  ≠  169  =  132
The reality is that because the numbers are close enough to working out (170 is approximately 169), it is possible to slightly deform the LEGO pieces so that they fit together into what is essentially a right triangle. The goal of the exercise below is to find triples of side lengths, such as 7-11-13 that are close enough to satisfying the Pythagorean Theorem to be constructible in practice with LEGO.

Scratch programming

Your first task is to create a Scratch program that performs computations on potential Pythagorean triples with side lengths up to 14 (the longest length between the end holes in standard LEGO beams). Use a loop to consider all possible lengths from 1 to 14 for the shortest leg, and incorporate another loop inside to consider possible lengths for the long leg. For the long leg, considers lengths from the current length of the shortest leg up to 14. The "repeat until" or "repeat" blocks in the "Control" palette should be helpful for constructing these loops.

For each combination of leg lengths as described above, compute:

Note that you can perform all the necessary arithmetic with blocks in the "Operators" palette; notice, especially "round" near the bottom and the block at the bottom with a pulldown menu including "sqrt" (for square root) and "abs" (for absolute value).

You are to write all your data to a list. Lists function very much like simple variables and are created through the same "Variables" palette. Once you create a list to hold your data, you can use the "delete" block for this list near the beginning of your program to delete all the data in the list; your program can then repeatedly use the "add" block for this list to fill it with data (in a row-by-row fashion). The first thing added to your list should be just the row "Short Leg,Long Leg,Hypotenuse,Approx Hyp,Error,Abs Error,Slope" (without the quotation marks), corresponding to the data listed above that you need to compute for each combination of leg lengths. As you consider each combination of leg lengths, add a new item (row) to the list, with the leg lengths and computed values in the correct order and separated by commas (using "join" blocks from the "Operators" palette). Before adding a row of data, check that the hypotenuse and slope are in the range of interest to us: approximate hypotenuse at most 14 (so it can be realized with a single LEGO beam) and slope at most 5 (so that it is substantially more interesting than something that runs horizonatally or vertically). (The "if then" block in the "Control" palette and logic operators in the "Operators" palette should be useful for this purpose.)

Once your program is running succesfully, you will be able to store your resulting data in a file as follows. In the Stage area of your Scratch window, you will see a scrollable list headed with the name of the list where you have written your data (assuming the name of this list has been left checked in the list of lists that shows under "Make a List" when viewing the "Variables" palette). Right click (or control-click) on the name of your list in the Stage area, and select (left click) "export" from the menu that comes up. The data will store in your Downloads folder in a ".txt" file named to correspond to the name of your list. Change the file name to end in ".csv" instead of ".txt".

Spreadsheet analysis

For this portion of the assignment, you will need to access Google Sheets on the web or have on your computer a spreadsheet program that can work with a CSV (comma-separated values) file, which is what was generated in the Scratch programming portion of the assignment. Microsoft Excel and Numbers for Mac will both work fine (except for a little quirk in Numbers for Mac that will be explained below), and these normally are the default programs for opening a CSV file; thus, you should be able to just double-click on the file you saved from Scratch (and renamed) to open it in your spreadsheet program. Alternatively, you can visit to create a new spreadsheet and import your CSV file. Your next task is to manipulate the spreadsheet in such a way that you can quickly read off the triple of three integral side lengths that produces the least absolute error for a specific slope. To put your spreadsheet in the desired form, sort all the rows in ascending order of the absolute error values, and also sort in ascending order of the slope values. Think about which of these two columns you want to sort on first so that all the rows with the same slope will end up together in the spreadsheet. (In any of Microsoft Excel, Numbers for Mac, or Google Sheets, you can sort according to the numbers in a column by clicking on the single-letter header inserted by the spreadsheet program at the top of the column. In Excel, you will follow up by making a selection from the "Sort & Filter" menu near upper right; in the other spreadsheet programs click on the small arrowhead symbol at the right of the header to bring up the menu you need. Also note that in Numbers, you must be careful to get a consistent interpretation of the data types across the different rows before sorting. To fix the Numbers quirk about data types, you should select the data cells in the entire column or columns of interest (by clicking a cell and then dragging the bottom right corner), click on the "Format" button at upper right, click on the "Cell" tab just below, and then in the pulldown menu under "Data Format", select "Automatic" (to replace "Mixed").)


Turn in your Scratch program and your sorted spreadsheet using the mechanisms specified by your teacher.