a different approach

12/02/2017

Last month, during the period when I was offering satsang, I felt to maintain a balance by writing a piece of software. The arbitrary task I picked was to search for so-called narcissistic numbers (see footnote for the mathematical definition). I had heard that there are 88 such numbers and that the largest has 39 digits, making it a very big number indeed.


The first version of the software worked very nicely, trying every possible number starting at 1. After a few hours it had already tested the first million million numbers – good going was my first thought. Then I realised that at that rate of progress, the code would take at least a hundred million million times the remaining expected lifetime of the planet, to find all the numbers. Some small improvements (see footnote) would reduce the time by a factor of 20 or so. However, something more radical was needed.


I wrote a new version of the software which took a completely different approach. Instead of testing every possible number, it tested every distinct collection of digits (more detail in footnote). This was still a huge space to search through. However, I reckoned that it was a lot less huge than the space being searched by the first approach.  And so it transpired. The different approach worked a treat and in a few hours I was looking at a list of 88 narcissistic numbers.


I tell this story here because it struck me that the spiritual search can unfold in a similar way. Looking everywhere is a good approach but it can take many lifetimes. Sometimes a different approach can be equally effective and allow us to find something in this lifetime…



Footnotes for the mathematically minded:


A narcissistic number is a natural number whose value is equal to the sum of each digit in the number raised to the power of the number of digits in the number. For example, the 3 digit number 153 is narcissistic because:


13 + 53 + 33  =  1 + 125 + 27  =  153


I spotted a couple of constraints allowing slight optimisation of the exhaustive search algorithm: Firstly, a number ending in zero is narcissistic if and only if its successor is narcissistic. Secondly, setting aside the least significant digit, a narcissistic number must have an even number of odd digits amongst the rest of its digits. I will leave it as an exercise for the reader to prove each of these constraints. The other optimisation I made was simply to make use of all 8 processors in my laptop, rather than leaving all the work to one. At this point, the cooling fans were also working flat out!


The different approach was to generate all possible combinations of digits, ignoring their sequence. In other words, 444925 has the same digits as 944524 and therefore the same sum of the powers. The digits in that sum can then be ticked off against the collection of digits being considered. I didn’t get around to determining the complexity of this algorithm (the size of the space to be searched). It must have been much better than that of the first algorithm.