Saturday, February 18, 2017

Is it Prime? - Python

A fun experiment to conduct with students is to compare how fast a Python can determine if a number is prime versus how fast Scratch can do it.

Below is (clunky) Python code:


I checked to see how fast the Python program could determine that 7,465,001 is not prime.  It did it in about 7 seconds.  It took Scratch about 90 seconds to find a factor.  Ask the students why they think Python solves the problem 13 times faster than Scratch.   Their answers will interest you and give you insight as to how they understand computers and programming.

Another learning opportunity for students is to see how creating a program with Scratch makes it much easier to program with Python.  If they created the "Is it Prime" program on Scratch, they will have solid pseudocode to write  the program in Python.

Saturday, January 14, 2017

Binary to Base 10 with Python

Python has a unique syntax for exponents:   Instead of the standard 3^2 representing 3 to the second power, Python uses 3**2.  It makes sense.  I guess.  In a weird sort of way.  Of course, if no one told you, you might spend a while troubleshooting your code thinking the only thing that couldn't POSSIBLY be wrong is 3^2=9.  But....in the python world, 3**2 is 9.

The code for Binary to Base 10 in Python was a little more straightforward than Scratch.


Tuesday, January 3, 2017

Data Analysis (percentages) with Python

This is a great example of how encryption experts and homeland security 'spies' can analyze large quantities of data and flag elements that might be suspicious or important. 

This Python module allows you to compare a given sentence, paragraph, or phrase and compare it to a 350,000 word English dictionary to determine whether the sentence is written in English.  It simply asks the question, "What percentage of the words in the given sentence were in the dictionary file?"  It then makes a decision based on that percentage.

The students may not be up for coding this, but a teacher could easily walk students through a sample sentence to determine what percentage of the words were in the dictionary file.

Thursday, November 17, 2016

Divisibility Rules with Scratch

Elementary and middle school students benefit from knowing basic divisibility rules.  The four basic divisibility rules are:

A number can be divided by 2 if it ends in 2,4,6,8, or 0

A number can be divided by 2 if the sum of its digits can be divided by 3.  (Also helps students understand the difference between digits and numbers.)

A number can be divided by 5 if the number ends in 5 or 0.

A number can be divided by 10 if it ends in 0.

There are others, but these are essential.

If students are able to program a computer to follow the rules, then you can be pretty sure that they understand the rules.  Also, this program uses a mod operation, which is a cool thing for a middle school kid to learn.

Source code is here .  There are better ways to do this, I'm sure.

Friday, November 4, 2016

Transposition Cipher, Python

Kids who are motivated to learn a programming language will love Al Sweigart's books.  He's written four Python programming books, including a beginner game programming book and an intermediate game programming book.  They are freely downloadable, or you can buy printed copies.

I'm going through his Hacking Secret Ciphers with Python, which is a fun introduction to using Python to encrypt and decrypt messages.

This transposition code was pretty cool.  I want to teach this!  I also want to turn it into a Scratch program! 




Saturday, July 9, 2016

Richard Feynman and Teaching

From the  Farnam Street blog.  Excerpts from the book, "Genius : the life and science of Richard Feynman  by James Gleick."
 
Process vs. Outcome
Feynman proposed that first-graders learn to add and subtract more or less the way he worked out complicated integrals— free to select any method that seems suitable for the problem at hand.A modern-sounding notion was, The answer isn’t what matters, so long as you use the right method. To Feynman no educational philosophy could have been more wrong. The answer is all that does matter, he said. He listed some of the techniques available to a child making the transition from being able to count to being able to add. A child can combine two groups into one and simply count the combined group: to add 5 ducks and 3 ducks, one counts 8 ducks. The child can use fingers or count mentally: 6, 7, 8. One can memorize the standard combinations. Larger numbers can be handled by making piles— one groups pennies into fives, for example— and counting the piles. One can mark numbers on a line and count off the spaces— a method that becomes useful, Feynman noted, in understanding measurement and fractions. One can write larger numbers in columns and carry sums larger than 10.
To Feynman the standard texts were flawed. The problem

29
+3

was considered a third-grade problem because it involved the concept of carrying. However, Feynman pointed out most first-graders could easily solve this problem by counting 30, 31, 32.
He proposed that kids be given simple algebra problems (2 times what plus 3 is 7) and be encouraged to solve them through the scientific method, which is tantamount to trial and error. This, he argued, is what real scientists do.

“We must,” Feynman said, “remove the rigidity of thought.” He continued “We must leave freedom for the mind to wander about in trying to solve the problems…. The successful user of mathematics is practically an inventor of new ways of obtaining answers in given situations. Even if the ways are well known, it is usually much easier for him to invent his own way— a new way or an old way— than it is to try to find it by looking it up.”

It was better in the end to have a bag of tricks at your disposal that could be used to solve problems than one orthodox method. Indeed, part of Feynman’s genius was his ability to solve problems that were baffling others because they were using the standard method to try and solve them. He would come along and approach the problem with a different tool, which often led to simple and beautiful solutions.

Wednesday, July 6, 2016

LCM with Scratch

Source code here.  If anything, this is a good way to illustrate that using multiples to find LCM can be extremely time consuming, even for a computer.  The factoring method becomes much more appealing when faced with finding the LCM of 270 and 120.

By the way, it took Scratch 46 seconds to find the LCM of 270 and 120 using this algorithm.  It took Python about 2 seconds using a similar algorithm.