Iterative Search Algorithms
Iterative Search Algorithms
- Chandrahas M. Halai
Find the unique four-digit number, where all the digits are not the same, such
that when the number that you get by arranging the digits in ascending order
is subtracted from the number that you get by arranging them in descending
order you get the same four-digit number.
Let us choose any four-digit number and test whether it satisfies the given
condition. Let the number be 1947 the year our country got its independence.
Arranging the digits in 1947 in descending order we get 9741 and arranging
them in ascending order we get 1479. Now, subtracting 1479 from 9741 we
get:
9741 - 1479 = 8262
Now, let us do the same operations on 8262 as the new input. We get:
8622 - 2268 = 6354
Now, let us do the above operations on every new output in iteration. We get:
6543 - 3456 = 3087
8730 - 0378 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
Since, we are getting the same output for 6174, we have found the number we
were looking out for.
Let us try the same procedure on one more four-digit number. Let that number
be the current year 2021.
2210 - 0122 = 2088
8820 - 0288 = 8532
8532 - 2358 = 6174
7641 - 1467 = 6174
Again, we get the same number 6174.
If you do these operations on any four-digit number in which all the digits are
not the same (a four-digit number consisting at least two different digits), then
you will get the result as 6174 in not more than seven steps. This amazing
discovery was made by Indian mathematician Shri Dattatreya Ramchandra
Kaprekar (1905 – 1986) in the year 1949. In honour of Shri Kaprekar the
number 6174 is called Kaprekar’s constant and the above procedure is called
Kaprekar’s operations.
Let us say we begin Kaprekar's operation with an initial four-digit number x1.
The operations will stop when you reach the final value xn, where xn = 6174.
Before reaching the final value xn, we get all the intermediate values, x2, x3, ...,
xn-1. All these values form what is known as the Kaprekar's sequence. 6174, is
the limiting value of these sequences.
From the above two examples we have the Kaprekar's sequences as:
1) 1947, 8262, 6354, 3087, 8352, 6174
2) 2021, 2088, 8532, 6174
After a few hours of getting a new mobile phone number I forgot it. In India,
we have ten digit mobile phone numbers. Fortunately, I had paid premium
amount to get a special number. In this special number, the first digit (from left
to right) is equal to number of zeroes in that number, the second digit is equal
to the number of ones in that number, third digit is equal to the number of
twos in that number and so on and so forth. For example, if first digit of
number is 4 then there are four zeroes in that number. There is only one such
ten-digit number. So, what is my mobile phone number?
Solution:
There are ten-billion ten-digit numbers from 0000000000 to 9999999999.
Finding that one unique number amongst ten-billion is like finding a needle in a
haystack.
I have developed an algorithm that constructs the number using the specified
conditions.
Our algorithm will work on this number to arrive at the required number. We
begin by checking every digit in the number to ensure that it contains the right
count of the corresponding digits. If not, then the right count is put in that
particular place.
The digits from second to the ninth all contain zeroes and they are
correct.
Have we got the desired ten-digit number? Let us check every digit of the
number once again.
Second pass or Iteration:
Checking the First digit:
At present this place contains a 9. Let us count the number of zeroes in
the number. We see that there are eight zeroes instead of 9. Hence, we
will replace the 9 with 8. Thus, the number becomes:
80000 00001
The digits from third to the Eighth all contain zeroes and they are
correct.
Have we got the desired ten-digit number? Let us check every digit of the
number once again. We are going to repeat the same steps (procedure) once
again. We are going into a loop or iteration (iteration means repetition). When
we find that all the digits contain the right count, we come out of the loop.
Third pass or Iteration:
Checking the First digit:
At present this place contains an 8. Let us count the number of zeroes in
the number. We see that there are seven zeroes instead of 8. Hence, we
will replace the 8 with 7. Thus, the number becomes:
71000 00010
The digits from fourth to the seventh all contain zeroes and they are
correct.
The last digit contains a zero and that is correct. Hence there will be no
change in the number.
Have we got the desired ten-digit number? Let us check every digit of the
number once again.
Fourth pass or Iteration:
Checking the First digit:
At present this place contains a 7. Let us count the number of zeroes in
the number. We see that there are six zeroes instead of 7. Hence, we
will replace the 7 with 6. Thus, the number becomes:
62100 00100
The second and third digits contain a 2 and a 1 respectively and both are
found to be correct.
The digits from fourth to the sixth all contain zeroes and they are
correct.
The ninth and the last digits both contain a zero and that is correct.
Hence there will be no change in the number.
Have we got the desired ten-digit number? Let us check every digit of the
number once again.
Fifth pass or Iteration:
In this pass we find that all the digits are correct. No changes are made to the
number in this pass. This means that we have got our desired mobile phone
number.
Thus the mobile phone number is 62100 01000.
To get the mobile number we had applied the algorithm on the number 90000
00000. Instead of applying the algorithm on this, we can use any random ten-
digit number and still get the correct answer.
I have written a program in Python to carry out the above operations on any
ten-digit number. This can be found at:
https://chandrahasblogs.wordpress.com/2020/08/16/using-python-to-find-
the-needle-in-the-haystack/
Let us try the above operations on another number, 9988776654. The results
that we get after every iterations are as listed below:
Your 10 digit mobile number: 9988776654
[0, 0, 0, 0, 1, 1, 2, 0, 0, 0]
[7, 2, 2, 0, 0, 0, 0, 1, 0, 0]
[6, 1, 1, 0, 0, 0, 1, 0, 0, 0]
[6, 3, 0, 1, 0, 0, 1, 0, 0, 0]
[6, 2, 1, 0, 0, 0, 1, 0, 0, 0]
[6, 2, 1, 0, 0, 0, 1, 0, 0, 0]
Let us say we begin the search operation with an initial ten-digit number x1.
The operations will stop when you reach the final value xn, where xn =
6210001000. Before reaching the final value xn, we get all the intermediate
values, x2, x3, ..., xn-1. All these values form what is known as the Chandrahas'
search sequence. 6210001000, is the limiting value of these sequences.
From the above two examples we have the Chandrahas' search sequences as:
1) 9000000000, 9000000001, 8100000010, 7210000100, 6210001000
2) 9988776654, 0000112000, 7220000100, 6110001000, 6301001000,
6210001000
Let us generate a few more sequences:
3) 0123456789, 1221111111, 0710000100, 7210000100, 6210001000
4) 5461532497, 0111100100, 5500020000, 7010000100, 7210000100,
6210001000
The algorithms / operations to find the solutions for both the above problems
are similar. They are iterative search algorithms. You begin with any random
number as the initial value and perform operations on it. The output is used as
the new input. The operations are performed in iterations till the desired value
is reached. The above algorithms can be grouped in to a class of Iterative
Search Algorithms.