How to Write a Happy Number Algorithm

Algorithms are the foundation of a code. Yes, it’s true that they do not have a direct hand in the execution of a code, but, at the same time, you must also remember that an entire code is always based on the foundations laid by a universal algorith...

Article Posted in: Tools and Techniques

Algorithms are the foundation of a code. Yes, it’s true that they do not have a direct hand in the execution of a code, but, at the same time, you must also remember that an entire code is always based on the foundations laid by a universal algorithm, in particular.

To write a flawless code, you must know the different ways of writing a flawless algorithm. Hence, the importance of an algorithm can never be ignored in any way whatsoever.

Right, now let’s get back to our original topic: “The Happy number algorithm.” Let’s see how we can write a happy number algorithm in a few simple steps.

Ebooks.com - Save time and money!

Image- Happy number 7

Source- Max Pixel

A basic overview of happy numbers: What are they?

Numbers can’t have emotions; can they? They why are we referring to these numbers as “happy” numbers? For no specific reason, to be honest. It’s just something that’s purely mathematical in nature.

A number can be said to be “happy” if and only if it satisfies a certain degree of mathematical conditions or process.

Conditions for being a happy number

1.The number, in question, must be a positive integer.

2. The sum of the individual squares of the digits of the number must be  equal to 1 to confirm the fact it’s a happy number, on the whole. The individual squares can be done for an infinite number of times until and unless the result “1” is achieved for happy number confirmation. If  “1” cannot be achieved in any way whatsoever, the given number is an unhappy number.

We will explain the conditions above with an example for easier comprehensibility. Have a look.

Given number = 19 (Assumed just for the sake of our explanation).

The number 19 is a positive integer. So that satisfies the first condition of the happy number problem. We can proceed with further calculations for happy number confirmation.

Sum of the squares of the individual digits of the number:

Stage* 1: 12 + 92 = 82 (≠ 1)

So you see the second condition remains unfulfilled. Hence, we go again.

Stage* 2: 82 = 82 + 22 = 68 (≠ 1)

Stage* 3: 68 = 62 + 82 = 100 (≠ 1)

Stage* 4: 100 = 12 + 0 + 0 = 1 [Condition fulfilled]

Hence, we can conclude that the given number IS a happy number, on the whole.

So that takes cares of the “basic overview” part of our discussion. Now we move on to the algorithm.

The Happy Number algorithm

Before we delve deep into the algorithm, there’s a thumb rule that you need to be aware of [This thumb rule WILL be used in our algorithm to ease things up]:

If the result at ANY stages* of our calculation (refer to the stages represented above in our calculation) coincides with a number in the list of 4, 16, 20, 37, 42, 58, 89, 145, the given number is SURELY an unhappy number.

Right, now let’s start the algorithm.

1. Start.

2. Accept any positive integer from the user and store it in a variable, say user_input.

3. If “user_input” is a negative integer,

    Display “Wrong input. It’s negative. Please insert a positive integer for verification.”

    else

    continue to step 4.

4. Extract the digits one-by-one from the user input number that’s stored in the variable user_input.

5. Square each digit individually & find the sum of the same to get a single final result. Store the final result in the variable user_input (refer to step number 2)

7. If final result stored in user_input is present in the list of 4, 16, 20, 37, 42, 58, 89, 145

    Display “The number is UNHAPPY”

   else,

          If user input number is “1”

                   Display “The number is HAPPY”

           else,

                     Repeat step 4.

8. Stop.

So you see the algorithm is pretty basic but it does the job well. The main trick lies in the thumb rule; the one that’s based on the list of 4, 16, 20, 37, 42, 58, 89, 145 (discussed above). Once you become aware of such a rule, the entire thing becomes as easy as a piece of cake. With that, we’ll bring this article to a close for now. Hope you really had a good and enlightening read.

Little Passports

Article Posted in: Tools and Techniques
Tags: Education Learning

MyPrivateTutor UAE

MyPrivateTutor is an online marketplace for all kinds of tutoring and learning services. We provide a platform to help learners find home tutors, online tutors, expert trainers,tutorial centers, training institutes and online tutoring schools. Learners can also find local classes, workshops, online courses and tutorials on a huge variety of topics. We operate in 12 countries worldwide and our community of learners, teachers and training businesses numbers over 200,000. We provide an opportunity for individual tutors and tutoring businesses to promote and grow their business.

Looking for Tutor or Coaching Class?

Tell us your learning requirements in detail and get immediate responses from qualified tutors and institutes near you.

Post Learning Requirement