I’ve been doing little programming projects for my own amusement. I thought about doing Advent of Code like the cool kids are (Looking at you Mike Zamansky who among others is blogging about those challenges) but I just can’t get into them. I need to scratch my own itch as it were. The other day a friend posted on Facebook that the date was a Pythagorean Theorem Day – 12-16-20 because a right triangle with a height of 12, a width of 16 has a hypotenuse of exactly 20. It’s a cute idea if we ignore that the year is really 2020 not just 20.
I commented that calculating the next Pythagorean Theorem Day would be a fun little programming project. Today I thought I would try it myself. The set up is pretty easy. Nested loops for month and day and using the usual math.
There are two issues. One is that not all months have the same number of days. The other is that we want an exact whole number for the hypotenuse. The first was solved by creating an integer array with the number of days in each month and using those values in my equation.
The second took some thought. There are probably some really cool mathematical ways to do this. I thought about this and decided to cheat a bit. I converted my floating point result into a string, cast it into an integer, converted the integer value to a string, and then did a string compare. The result of the compare determined if I displayed the result or not. There are 12 such dates in case you are interested. Only two of them are after 20 though.
I leave figuring the rest out as an exercise to the interested (or assigned) programmer.
A few things make this a nice student project in my opinion. Nested loops of course, using an array to simplify the programming, and what ever you use to determine if the hypotenuse result is an integer or not. And it is short so you can use it as a demo or an in-class project.
What do you think? How else would you determine if the result was an integer or not?
Hello Alfred
ReplyDeleteYou roped me in! I rounded the calculated hypotenuse to zero decimal places and compared that with the unrounded value. Will give this mini project to some of my students in the new year.
Gordon