## Happy Numbers

Image by Bridget Coila on Flickr

Some numbers simply have a positive attitude. They're fun to play with.

Recreational math problems and puzzles are key to learning how to solve problems. The ability to carefully tease out an answer from a complicated problem, reducing complexity and confusion to the simplest possible units, also is key to learning computer science and programming.

Every issue of this magazine will have at least one puzzle and math problem. Solutions will be offered at the bottom of the page but you have to promise not to to cheat and look up the answer. It's the honor system.

### What is a Happy Number?

While numbers do not feel emotion, people do respond emotionally when numbers behave consistently in surprising and amusing ways. Happy Numbers can be reduced to 1 with a simple formula. Finding Happy Numbers can be a satisfying chore for a few minutes or a few hours.

To determine if a number is happy, or not, follow these simple rules with any number:

If the number is a single digit, square it (multiply it by itself). If the result or the original number has multiple digits, take each digit by itself and square the digit (multiply it by itself).

Repeat until you get to the number 1.

Let's try a few numbers:

- 1 times itself (1 x 1) equals 1 and is, therefore, a happy number.
- 2 times itself (2 x 2) equals 4. 4 squared (4 x 4) is 16 which, when split, becomes (1 x 1) + (6 x 6) or 1 + 36 equals 37. 37 split up becomes (3 x 3) + (7 x 7) or 9 + 49 equals 58. 58 split up becomes "¦

Okay, let's stop to explain that if the numbers 4, 16, 37, 58, 89, 145, 42, 20, and 4 show up in this order, we know the number will never resolve to 1. Therefore, those numbers will never be happy. In fact, the numbers will repeat this cycle forever. That’s sad.

Try other numbers, starting with 3 and with a sharp eye for the 4, 16, 37, and 58 results pattern to tell you the number is not happy.

So take out a sheet of paper, find something to write with, and see if how many happy numbers you can find.

### Answers

Here are answers to the math puzzle. Hopefully you tried to solve the problem instead of skipping down this page.

These are the happy numbers from 1 to 1000. How many did you find?

1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, 103, 109, 129, 130, 133, 139, 167, 176, 188, 190, 192, 193, 203, 208, 219, 226, 230, 236, 239, 262, 263, 280, 291, 293, 301, 302, 310, 313, 319, 320, 326, 329, 331, 338, 356, 362, 365, 367, 368, 376, 379, 383, 386, 391, 392, 397, 404, 409, 440, 446, 464, 469, 478, 487, 490, 496, 536, 556, 563, 565, 566, 608, 617, 622, 623, 632, 635, 637, 638, 644, 649, 653, 655, 656, 665, 671, 673, 680, 683, 694, 700, 709, 716, 736, 739, 748, 761, 763, 784, 790, 793, 802, 806, 818, 820, 833, 836, 847, 860, 863, 874, 881, 888, 899, 901, 904, 907, 910, 912, 913, 921, 923, 931, 932, 937, 940, 946, 964, 970, 973, 989, 998, 1000

For clever readers, do you notice something odd about the numbers that end in zero in this list? That would be:

10, 70, 100, 130, 190, 230, 280, 310, 320, 440, 490, 680, 700, 790 820, 860, 910, 940, 970, 1000

They are 10 times the first 20 numbers:

1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100

Happy Numbers are a pattern and include a pattern within them.

Numbers can be beautiful, and sublime.

### Pseudo Code

Happy numbers, and other numeric patterns, can be identified programatically with code. You might never have to write code to look for happy numbers, but you might code to look for a pattern of numbers (even numbers versus odd, for example) and do something when you find part of the pattern.

The first step to creating pseudo code to evaluate happy numbers is to remind ourselves how to determine if a number is happy or not. You take any number, break the number into single digits, then multiply each digit by itself and add up the sum of all you multiplications. You repeat this process until either you get the number 1 (the number you started with is happy) or your results match a pattern that indicates an unhappy number (4, 16, 37, 58, 89, 145, 42, 20).

Let's put these steps into a simple set of instructions, or pseudo code, to evaluate whether or not one number is happy or unhappy:

- First, see if our number has more than one digit.
- If yes, split the number into single digits and multiply each digit by itself. Then add up the multiplication results into a single sum, or total.
- If no, multiply the number by itself to get a total.

- See if the total, from either Step 2 operation, is a number in the list of numbers that indicates it is an unhappy number (4, 16, 37, 58, 89, 145, 42, 20).
- If yes, our initial number is an unhappy number. Stop evaluating.
- If no, repeat this multi-digit evaluation process in Step 1 until the result is 1.

- When the result is 1, we confirm it is a happy number.

Notice how we use conditional “if” statements to determine the next step in our process. Conditional statements are very common in software programming.

Let's make this slightly more complicated. Let's imagine we want to process all numbers from 1 to 1000 to find happy and unhappy numbers. As we work through our numbers, automatically with code, we will compile a list of happy numbers and another list of unhappy numbers.

Here's how we could adapt our pseudo code to automate the testing of many numbers to find out which are happy and unhappy. We'll start with a current number set to 1 and evaluate numbers up to 1000:

- See if the current number is 1.
- If yes, we're starting the evaluation process.
- If no, the current number is what we set in Step 4 below, the next number to evaluate.

- See if our current number has more than one digit.
- If yes, split the current number into single digits and multiply each digit by itself. Then add up the multiplication results into a single sum, or total.
- If no, multiply the number by itself to get a total.

- See if the total, from either Step 2 operation, is a number in the list that indicates it is an unhappy number (4, 16, 37, 58, 89, 145, 42, 20).
- If yes, our current number is an unhappy number. Stop evaluating. Add the current number to our list of unhappy numbers.
- If no, repeat this multi-digit evaluation process in Step 1 until the result is 1. When the result is 1, we confirm our current number is a happy number. Add the current number to the list of happy numbers.

- See if the current number is less than or equal to 1000, the limit and last number to evaluate.
- If yes, increase our current number by one. Repeat steps 1, 2, and 3.
- If no, display our list of happy numbers and unhappy numbers.

Pseudo code can take many forms, from simple to more complex. The goal of pseudo code is to work through a problem on paper or onscreen before taking time to code. You can find problems earlier which means less buggy code. Plus pseudo code makes more clear the connection between actual coding and math problems like Happy Numbers.

### Learn More

#### Happy Numbers List

http://en.wikipedia.org/wiki/Happy_number

#### Happy Numbers for Teachers

http://www.articlesforeducators.com/article.asp?aid=1

#### 7 and Happy Numbers

http://www.guardian.co.uk/science/grrlscientist/2012/feb/27/1

http://youtu.be/kC6YObu61_w

#### Why 4 is the Nemesis of Happy Numbers

http://io9.com/5934819/why-four-is-the-nemesis-of-happy-numbers

#### Happy Number

http://mathworld.wolfram.com/HappyNumber.html

#### How to Code Happy Numbers

http://rosettacode.org/wiki/Happy_numbers

#### Dr. Who and Happy Numbers (Hey, Why Not?)

### Also In The October 2019 Issue

Bring out your virtual carving knives — it’s time to give your digital pumpkins some spooky faces!

30+ ideas for STEAM-theme gifts for kids of all ages!

Teach kids basic coding skills by letting them program Botley to zoom around the room, draw shapes, and even avoid obstacles!

Some numbers simply have a positive attitude. They're fun to play with.

How 3D printing could help us get to Mars, and create new tools, homes, spacecrafts — even organs!

No discussion of design is complete without the history of lorem ipsum. It's more than placeholder text you stuff into a visual design.

"Hello World!" is one of the first programs you learn how to code. Here's the phrase in 4 languages with links to 100 more examples.

Learn the delicious-sounding secrets that websites use to keep your passwords safe from hackers.

A simple, quirky theorem with big applications, from picking socks to counting hairs.

Are you ready to create your virtual own solar system? With a little Python code and a little math, the sky’s the limit!

Learn some of the tricks game developers use to simulate an extra dimension.

How scammers can trick you into downloading malware onto your own computer.

There are pros and cons to networking all the “smart” devices in your home. What surprises does the future hold?

Sometimes, even the most dynamic languages need to classify and check data. Now, you can add your own types to any language!

Is it possible to steal software? And how do we know who owns code?

Learn how to start making 3D games on this cool platform with over ninety million users.

Teaching strategies to better equip your students for a future full of open-ended problems.

Check out this nifty feature that helps programs distinguish between variables with different scopes.

Create a simple electronic game with CircuitPython and Adafruit, and test your reflexes against friends and family!

Links from the bottom of all the October 2019 articles, collected in one place for you to print, share, or bookmark.