Hey guys, so we're gonna be making an Algorithm, using a basic Math sequence and a little programming.
For the basics, let me tell you what happy numbers are, the numbers who's individual digits' square adds upto 1.
You can go check out the Wikipedia Page for it, for a better understanding and a little extra knowledge which might come handy: http://en.wikipedia.org/wiki/Happy_number
So the things you'll need:
1. A PC with Python 2.7.5 installed in it.
2. Time. (I did need it)
3. And some knowledge about Happy Numbers.
Step 1: Beginning With the Code!
So what we're gonna start with is how to develop the code for finding out happy numbers till a certain upper limit specified by the user, checking that whether the number is Happy or not.
So what we know is that squaring the digits individually of a number and then adding them would give us another number, if it gives us 1, its Happy, if not, its Unhappy.
The picture above gives you the code of the basic program to find out the Happy and Unhappy number.
Just in case you didn't understand the code, we started off with two lists, one to store the happy numbers and the other to store the unhappy numbers.
I specified the upper limit of the program as 300 and used it in a FOR loop. "t" is just another variable i took, and now to segregate the digits apart from a given value of "i" we use 3 more variables, namely "a","b","c". In case you don't understand the use of these three variables, comment below, place your query, and i would reply soon enough.
Yeah, so now we have our three separate digits from a number, notice that we have only three variables because the maximum number of digits in our upper limit is 3, as the digits in the upper limit increase you might want to increase the variables for the digits as well.
So later on , in the While loop, we square the digits and add them and verify if they add upto 1 or not, and we keep on doing it 50 times per value of "i".
If at any instance our program gets 1 as the output for variable "t", it would omit the rest of the program after printing the desired output, or nothing, whatever you want basically.
If not, it would tell us the number is not a Happy number and would continue for the next value of "i".
Step 2: Using the Program to Develop an Algo
Okay so this was my high school final year Computers Project, completely original, and an idea straight out of my head.
So we had to demonstrate the working of a class and the methods inside it, I started off by defining a class HappyNumbers, having a method __init__() in it (that pun was unintentional). For those of you who dont know what init function does, its just a function that is called as soon as the Object to the class is created, or unless called specifically. It's also called a constructor method. My constructor over here is full of lists and strings which are all EMPTY, except one list, this is the list that would help us encrypting your given string value. This list should contain alphabets from a to z TWICE, in the alphabetical order and, then it should also contain alphabets a to k in alphabetical order, in the same list after the twice a to z order.
Moving on, I defined another method Initialize(), this is where we use our previous code, so basically the primitive code we made to find out the Happy numbers has to be utilized here with small amends, if you face any problem, feel free to ask.
Later in the code you define a function named IOChar(), so this is what the real deal is, this function takes the input from the user and encrypts it. As far as I'm concerned I love breaking up strings' alphabets to form a list of the alphabets used, I find it easier to handle this way and not lose track of what I'm doing.
So this is how the last method would work, for the index of the alphabet our loop encounters in the list of alphabets, it would choose the number of the same index from our list of happy numbers. Now the program would find the alphabet encountered in the list of alphabets in alphabetical order, that is our first list under the __init__() method.
Now we have three positions in three lists, that is:
1. Position of the alphabet encountered in the given string.
2. Position of the Happy number according to 1.
3. Position of the alphabet encountered in the list of arranged alphabets.
So now what you want to do is, add the happy number to the index of alphabet from position 3, get the new alphabet according to the list and replace the alphabet from the list made of the alphabets of the given string. Combine all of the elements of this list and you would get an Encrypted string.
Step 3: Thanks :D
Thank you so much for reading this tut, i hope it did help, and please feel free to ask any queries, critiques.
Just in case if you guys want the whole program or my whole project uploaded, do tell me, because im still working on fixing its bugs. What i like about this algo is that, it isnt very easy to decode, unless you know how you coded it. And yes i have made the Decrypting algorithm as well.