0% found this document useful (0 votes)
47 views8 pages

Iterative Search Algorithms

The document describes Kaprekar's constant and operations, which involve taking a 4-digit number with distinct digits, arranging the digits in descending and ascending order, subtracting the results, and repeating the process until reaching 6174. It provides examples of Kaprekar sequences and discusses how the same iterative process can be used to determine a 10-digit self-descriptive number by ensuring each digit represents the count of that digit value in the number.

Uploaded by

Chandrahas Halai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
47 views8 pages

Iterative Search Algorithms

The document describes Kaprekar's constant and operations, which involve taking a 4-digit number with distinct digits, arranging the digits in descending and ascending order, subtracting the results, and repeating the process until reaching 6174. It provides examples of Kaprekar sequences and discusses how the same iterative process can be used to determine a 10-digit self-descriptive number by ensuring each digit represents the count of that digit value in the number.

Uploaded by

Chandrahas Halai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Iterative Search Algorithms

Iterative Search Algorithms for unique numbers that satisfy given


conditions

- 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 define Kaprekar's operations as a function k(x). The iterations in


Kaprekar's operations stop when k(a) = a. Here, we now know that a = 6174.
That is k(6174) = 6174.

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

Let us generate a few more Kaprekar's sequences:


3) 3355, 2178, 7443, 3996, 6264, 4176, 6174
4) 4434, 0999, 8991, 8082, 8532, 6174
5) 3579, 6174
6) 1357, 6174
7) 0246, 6174
8) 2468, 6174

Now, let us look at one more interesting number puzzle.

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?

The desired number is actually a self-descriptive number in base 10 (decimal


number) number system.

A self-descriptive number is an integer m that in a given base b is b digits long


in which each digit d at position n (the most significant digit being at position 0
and the least significant at position b−1) counts how many instances of
digit n are in m.

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.

We can begin solving the problem with a clean slate, i.e.


00000 00000
(I am breaking the number into two groups of five digits for better readability.)
Here we have ten zeroes. Hence, as per given conditions we should put a 10 in
the first digit. Firstly 10 is not a digit and secondly putting any digit other than
zero in the place of first digit will alter our count of zeroes in the number. The
number of zeroes will be decreased by one, that is, now the number of zeroes
will now be 9. Let us now put 9 in the first digit.
Hence, now our number will be
90000 00000

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.

First pass or First Iteration:


(You will understand later why I am calling this step the First pass / iteration)
Checking the First digit:
This digit should have the count of zeroes in the number.
We see that it contains 9 and when we count the number of zeroes we
see that it is also 9. Hence no change is made to the number. The
number will be:
90000 00000

The digits from second to the ninth all contain zeroes and they are
correct.

Checking the Tenth or the Last digit:


This digit should have the count of 9s in the number.
We see that there is one 9 in the number. Hence, we put a 1 in that
place. Thus, our number becomes:
90000 00001

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

Checking the Second digit:


This digit should have the count of 1s in the number. At present it
contains a 0. Let us count the number of 1s in the number. We see that
there is one 1 (in the last digit). Hence, we will put a 1 in that place.
Thus, the number becomes:
81000 00001

The digits from third to the Eighth all contain zeroes and they are
correct.

Checking the Ninth digit:


This digit should have the count of 8s in the number. At present it
contains a 0. Let us count the number of 8s in the number. We see that
there is one 8 (in the first digit). Hence, we will put a 1 in that place.
Thus, the number becomes:
81000 00011
Checking the Tenth or the Last digit:
This digit should have the count of 9s in the number.
At present that place contains a 1. Let us count the number of 9s in the
number. We find that there are no 9s in the number. Hence, we put a 0
in that place. Thus, our number becomes:
81000 00010

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

Checking the Second digit:


At present this place contains a 1. Let us count the number of 1s in the
number. We see that there are two 1s instead of one. Hence, we will
replace the 1 with 2. Thus, the number becomes:
72000 00010

Checking the Third digit:


At present it contains a 0. Let us count the number of 2s in the number.
We see that there is one 2. Hence, we will put a 1 in that place. Thus, the
number becomes:
72100 00010

The digits from fourth to the seventh all contain zeroes and they are
correct.

Checking the Eighth digit:


At present it contains a 0. Let us count the number of 7s in the number.
We see that there is one 7 (in the first digit). Hence, we will put a 1 in
that place. Thus, the number becomes:
72100 00110

Checking the Ninth digit:


At present that place contains a 1. Let us count the number of 8s in the
number. We find that there are no 8s in the number. Hence, we put a 0
in that place. Thus, our number becomes:
72100 00100

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.

Checking the Seventh digit:


At present this place contains a 0. Let us count the number of 6s in the
number. We see that there is one 6 (in the first digit). Hence, we will put
a 1 in that place. Thus, the number becomes:
62100 01100

Checking the Eighth digit:


At present that place contains a 1. Let us count the number of 7s in the
number. We find that there are no 7s in the number. Hence, we put a 0
in that place. Thus, our number becomes:
62100 01000

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 me call these operations as the Chandrahas' search operations. We can


define these operations as a function c(x). The iterations in the search
operations stop when c(a) = a. Here, we now know that a = 6210001000. That
is c(6210001000) = 6210001000.

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.

There will be similar mathematical problems which can be solved using


iterative search algorithms.

You might also like