## Happy Numbers

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 September 2013 Issue

### 1 and 0

Binary numbers, based on 1s and 0s, reflect the practical essence of computer hardware: electricity is either on or off. Learn how to write in binary numbers, and the (not so secret) code to transform English language letters into binary numbers and back again.

### Simon Haughton Talks about Kids, Python, and Computer Science

Simon recently wrote a short ebook, A children's guide to Python programming, to teach kids ages 5-8 computer programming with Python..

### Python

Named after Monty Python, this language is designed to be simple yet powerful, easy to code with lots of features.

### Variables

In most or all software programming languages, variables work like containers to hold numbers, phrases, or other important stuff used in several places in your code. Here's how they work in several common languages.

### Happy Numbers

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

### Local Coding and Technology Groups

Girls Who Code, CoderDojo, and other local groups are great places to learn how to program, meet people, and help others learn.

### How to Build a Computer

Building your own computer is a great way to not only save money, and get more processing power, but also to learn about the less obvious parts of software programming.

### News Wire Stories for September 2013

Interesting stories about computer science, software programming, and technology for the month of August 2013.

### Online Coding Schools

There are plenty of places online to learn one or more software programming languages. Here's a short list with some guidelines to evaluate all your options.

### Computer science is no more about computers than astronomy is about telescopes, biology about microscopes, or chemistry about beakers and test tubes. Science is not about tools. It is about how we use them, and what we find out when we do.

### Learn More Links for September 2013

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

### Claude Shannon

His work ties together two topics for this issue of the magazine: binary numbers and circuit design. Without Shannon, computers and computer science could have been very different.