Wednesday, November 18, 2015

Combinations - nCr


   In the previous post on Factorials I mentioned that I needed a factorial algorithm for a Combinations project and the Combinations project for a Random Walker project.
   As you can see in the formula for the number of combinations of n things taken r at a time with no duplications there are three factorials to compute to get to the nCr.
   This is a screenshot of the Combinations project. The graphic of the fruit is to illustrate that if n = 4 (the pear, orange, banana, and strawberry) then there are four combinations (shown in the four rows) when taken 3 at a time. The project first asks for n, then r and then computes nCr.
   The project can be viewed and downloaded by clicking on this link.
https://scratch.mit.edu/projects/88028765/
   A free document in PDF format describing the coding in more detail can be obtained on request. Send an email to grandadscience@gmail.com.


Friday, November 13, 2015

Factorial


   This project computes Factorial N written as N!
   Here is the definition of Factorial N.
    Writing code is not a one-step process. Over the years I've found that breaking the coding process into three parts helps students understand that first of all, coding is an exercise in problem-solving and breaking the coding problem into a series of smaller problems is the most efficient method for coding.
   Here are the three Parts and seven Steps I encourage students to use when coding.
   I Do the Math with Paper & Pencil Part
     • (1) Analyze the problem
   II Do the Algebraic Thinking Part
     • (2) Identify the variables
     • (3) Determine the relationships between the variables
   III Do the Algorithmic Thinking Part
     • (4) Create a Logic Flow diagram
     • (5) Translate the relationships (math syntax) into (computer
             syntax)
     • (6) Combine the pieces of code (into the algorithm that solves
        the problem)
     • (7) Test the algorithm (debug)
   I describe in detail how to apply this method to the problem of coding a Factorial algorithm in a six-page document. Any interested reader can obtain a copy of this copyright-free document in pdf format by sending an email to grandadscience@gmail.com
   Here is the opening screen of the project. The user is asked to input N! The number 11 was typed into the blue-lined box at the bottom of the screen.
   In this picture the computation of 11! is reported as 39,916,800.
   This project can be viewed and downloaded by clicking on the following link to the Scratch web site.
https://scratch.mit.edu/projects/87399897/

Saturday, November 7, 2015

Powers of 2 Calculator

   In the Random Walker project that computes the probability of landing on position p after f flips of the coin there is the need to compute powers of 2.
   If you click on the green operators tab you will find the [sqrt  ( 0 )] block. Next to the sqrt in the block is a small downward-pointing triangle that opens a pull-down menu to reveal the list of operators built into Scratch. Note that there is not a 2x operator.
   By definition, 2x means ‘use 2 as a factor x times’. This short script does exactly that and is probably the way most Scratch programmers build a powers of 2 script.
   I enjoy coding work-arounds but my first thoughts didn’t coalesce around the definition method coded above. Instead, it focused on another method using logarithms.
   Let y = 2x. Then taking the logarithm of both sides, log(y) = 2log(x). Since x is the exponent, I created a variable in Scratch named exponent.
   Next, I created a green log of 2 block.
   I picked a green multiplication block from the green operator menu and set the exponent variable as one factor and the [log of 2] block as the other variable to build the block shown below.
   There is an inverse (anti-log) block in the green pull down operators menu labeled [10x of (  )] that computes log(y). 
   The [exponent * log of (2)] block is then set into the [10^ of (  )] block.
   But this computation will be a decimal close to the integer value of 2x.
   There is a [round (   )] block in the green operators menu.
   The [(10^ of (exponent)* (log of (2))] is placed in the [round (   )] block.
   This block is set into a [say (                ) block to report the needed powers of 2.
   This project can be viewed and downloaded by clicking on the following link.
https://scratch.mit.edu/projects/86877882/









Thursday, November 5, 2015

Cycloid - The Helen of Geometry

   The cycloid is the curve traced by a point on the circumference of a circle which rolls along a straight line without slipping.
   The cycloid has been called "The Helen of Geometry" in reference to the beauty of Helen of Troy (her face launched a thousand ships) and the infighting between mathematicians as they fought over the properties of the curve and who did what and when.
   In this Scratch project I code the cycloid using two different methods.
   The first method (see graphic below) uses the parametric equations for the cycloid,
           x = r(theta -sin(theta))
           y = r(1 - cos (theta))
where r is the radius of the generating circle.
   In the coding of the equation for x, there is a problem. If theta is given in degrees (º) then the value of (theta - sin(theta)) will not vary much since the sine varies from 0 to 1. Therefore it's necessary to change the first theta in the parenthesis from degrees to radians. The formula for converting degrees to radians is  
                         radians = degrees * (pi/180)
and this formula can be seen in the script for x in the parametric form.
   The second method (seen in the bottom half of the graphic) shows how a point on the rim of the circles traces a path (locus) identical to the shape generated by the parametric equations. Click on the video to see the Scratch program in action.
   For readers interested in the history and mathematics of the cycloid a free PDF about the Cycloid can be downloaded at this link:
   Apparently Galileo worked on the problem of the area under the curve for 50 years!
   The cycloid, when inverted, is called a brachistochrone. This curve has many surprising properties and will be the topic of a future Scratch project.