Thursday, October 28, 2021

Coding in the New Grade 9 De-Streamed Math Course

 Coding is a new expectation in Ontario's Grade 9 De-Streamed math and is an area where a lot of my colleagues have asked for support and resources. Trying to figure out exactly is requires has been tricky: the MOE has yet to provide teacher supports in the form of examples, key concepts and sample tasks. However, my own sense is that there is a lot of good that can come when aim to use coding to intentionally learn about math concepts as opposed to coding things like Mario-type games. For this reason, I created a series of short Coding Challenges for the Grade 9 teachers to use with their classes and I have been in a number of schools trying these out.

Before I start any class, I ask what coding languages they have used before. The vast majority of students are familiar with Scratch (used mainly to create the aforementioned Mario-type games) but there is always a couple of students who have learned Python, Javascript or C++. I tell these students that although I will be showing the class a Scratch code, they are more than welcome to use the language of their choice to create a similar code on the condition that they explain this code to me later. 

I thought it would be good to get students to write some code that would produce a list of numbers as this is something that would tie in to some of the expectations from the Number strand. A nice introduction to each code is to provide a flowchart and ask the students to use this to write a list of numbers:

I'm quite happy to let the students struggle through this as their mistakes are often a result of not following the instructions precisely; this is something we need to bear in mind when we are coding!



After 10 minutes or so, we regroup and once we have agreed on how to follow a flowchart (and what numbers we should have written down), I give them a simple example of how this can be written using Scatch:
Care is needed her in building up the 3x+1 function: the same care that is need when using order of operations. This in itself is a worthwhile lesson. When students have successfully recreated this code, I ask them to alter it for these functions:
Now this code is something I produced and I am so far from being considered an expert coder, so I always can challenge students who have finished quickly to improve my code. One teacher noted that the output for the above code:
lacks a bit because it only gives the y-values, and that it would be better to see a pair of co-ordinates. I was initially unsure how to achieve this, but after watching what some students were trying, I came up with this:


Much better, I'm sure you would agree!

In a different class, students had already completed some of the coding challenges so we tried a different approach. We gave them the actual code and challenged them to follow this (instead of the flowchart) to create a list of numbers:
What I liked about this approach, is that we didn't have to spend time consolidating after they had written their answers: we simply asked them to recreate the code and see what the output is (in this case it is the... well, that would be telling wouldn't it?!) The students were then able to make sense of the code (especially the last two lines).
My challenge for this code was to improve it so that the user can choose the first two numbers (as opposed to starting with 1 and 1). It isn't long before students come up with something like this:

Now I can ask one of my favourite questions: 
Choose two starting numbers that will give you 1000 for the twelfth term.
As students work their way through this, some good questions come up:
  • are there any whole number solutions?
  • if you are using decimals, how close can you get?
  • how can you be more efficient in your search?
One student really took this last idea to hand. By fixing the first two numbers to be the same, he created a code that checked every possibility starting from ) 0.0001:



In another class, we started by giving them the flowchart for the wonderful Hailstones Numbers:
As students worked through this, some questions quickly emerged:


  • is there a number which doesn't lead to a 4-2-1 loop?
  • is there a different loop that a number can get caught in?
  • what is the longest amount of numbers that can be written before getting caught in a loop?
In this class, the teacher had been getting the students to write the Scratch code on their own and only showing them my code if absolutely necessary, so this is what we did for the Hailstone numbers. One challenge here is how to code the bit where we have to check to see if a number is even or odd. This necessitated a little interlude on modular arithmetic and 'if.. then...else' statements, but eventually led to this code:
On a side note, one particular student needed some extra examples to help him understand mod arithmetic but when the penny dropped, he declared with a massive smile: 'Mod arithmetic is really cool!'
As students used their code, they surmised that it would appear that every number gets stuck in the 4-2-1 loop. I challenged them to change their code so that the list stops at the first instance of 4-2-1 and tells us the length of this list. One student came up with this:

This is his explanation of the code:

We ended the class by relating the story of Fermat's Last Theorem and how this easy-to-understand problem eluded proof for centuries until Andrew Wiles heard about in a school math class and then devoted himself to this proof which he eventually achieved after 7 painstaking years. I then told them that Hailstone numbers are also referred to as the Collatz Conjecture and whilst mathematicians think that all numbers end in the 4-2-1 loop, no-one has actually proved this. 
'Maybe one of you here is inspired by this story, like Andrew Wiles was inspired by Fermat's Last Theorem. And maybe one of you will, years from now, create a beautiful proof for this and claim the $1 000 000 prize for doing so.' 
This got them excited!
'If so,' I continued, 'Don't forget to seek us out and at least buy us a cup of coffee!'

*        *        *
Personally, I have learned the following:
  • Coding can be a really great way to explore many math ideas that are in the new curriculum and it can be done in a way that doesn't feel like an addition. 
  • Have the students work with a coding buddy: shared thinking (even shared devices) lead to stronger learning.
  • Have some extension questions ready to go for those that finish quickly, and don't feel that you need to have the answers to these: trust the students to find a way and be happy to learn from them.
  • Be happy sharing your mistakes with the students, and show students how you talk through your thinking when de-bugging this mistake.