## World Gen Mods Ramp Up Minecraft Play

dark mode light mode Search Menu
Search

# Happy Numbers

Bridget Coila on Flickr

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.

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:

1. First, see if our number has more than one digit.
1. 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.
2. If no, multiply the number by itself to get a total.
2. 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).
1. If yes, our initial number is an unhappy number. Stop evaluating.
2. If no, repeat this multi-digit evaluation process in Step 1 until the result is 1.
3. 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:

1. See if the current number is 1.
1. If yes, we're starting the evaluation process.
2. If no, the current number is what we set in Step 4 below, the next number to evaluate.
2. See if our current number has more than one digit.
1. 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.
2. If no, multiply the number by itself to get a total.
3. 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).
1. If yes, our current number is an unhappy number. Stop evaluating. Add the current number to our list of unhappy numbers.
2. 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.
4. See if the current number is less than or equal to 1000, the limit and last number to evaluate.
1. If yes, increase our current number by one. Repeat steps 1, 2, and 3.
2. 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.

#### Happy Numbers List

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

#### Happy Numbers for Teachers

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

#### 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?)

http://tardis.wikia.com/wiki/Happy_prime