Showing posts with label exponent. Show all posts
Showing posts with label exponent. Show all posts

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/