I recently came across a youtube video explaining a method for multiplication that was quite different from long multiplication. To explain why this was so interesting, let’s first talk about long multiplication and what’s wrong with it, and then we'll calculate the size of the visible universe in centimeters.
Although long multiplication is fairly easy to teach, it has drawbacks. Firstly, you need to generate one interim result for each of the digits in the multiplier, and then sum these interim results. This takes up a lot of space when the multiplier is quite large, and takes a bit of time. The final result derives the least significant digits first – that is, the rightmost digit.
The other method only ever produces two interim results, and it generates them at the same time. The final result is generated beginning with the most significant digit, and in fact you can begin reading off the result before you have finished the calculation. Pretty awesome huh?
So, where did this method come from? India. It was published in a book written 3,000 years ago. Impressive stuff. Even better, the method has a name (in Sanskrit), and encoded in the name are the instructions for employing the method. Know the name, know the method.
Let’s go back and consider long multiplication again. It works by multiplying each digit in one number by each digit in the other, and by ensuring that each pair of digits are only multiplied once. The Indian method does exactly the same thing, only in a different way, allowing the result to be generated from the most significant digit first. What the Indians had discovered, in fact, was the order in which the digits have to be multiplied in order to generate the result starting with the leading digit.
Computers can only understand numbers up to a certain value. If a number won’t fit on the CPU’s registers, it can’t understand it. This creates limitations. However, computers can handle strings, sequences of characters that can be digits, of a much longer length.
For example, an 8-bit computer can store a number up to 255 and still recognise it as a number. It can, however, store strings up to 256 characters in length (because zero is a number too). If those characters are all digits, it might not be able to perform arithmetic on it, but it can store a number this size.
Importantly, you can extract individual digits from a string and perform operations on it - it's a single digit, so you can treat it as a number. I immediately saw that the Indian method for multiplying large numbers was suited to this kind of operation.
I set about writing an algorithm that would perform all the necessary operations in the correct order. I encoded the algorithm in a class, written in PHP. PHP is a very simple, high-level computer language. If it can be done in PHP, it can be done in almost any programming language. Placing the code in a class was a way of sanctifying the code so it wasn’t liable to be trumped by other code. If none of that makes sense to you, don’t worry about it. It works.
The first test I performed were on two four digit numbers. I could verify the results using a calculator or by using a pen and paper. Then I used longer numbers, to the point where the calculator was returning those dreaded ‘e’ numbers. I could still calculate by hand to verify the results. Then I went on to multiply two numbers, each of which consisted of 4096 digits. My code performed the calculation in moments. It took a lot longer to verify the results by hand. A lot longer.
Ok, so this is all working. I decided to calculate something stupid – stupid as in senseless, but also stupid as in stupidly big numbers. I decided to calculate the size of the visible universe. So let's do this now. I originally did this in inches, but centimeters actually make more sense because a meter is defined by the speed of light.
First off, let's figure out how many seconds there are in a light year: 60 (seconds) x 60 (minutes) x 24 (hours) = 86,400 (seconds in a day) 86,400 x 365.25 = number of seconds in a year.
The speed of light is 299,792,458 meters per second, multiply that by 100 to turn it into centimeters, and then multiply the result with the number of seconds in a year to give us the distance light travels (in cm) in a year. 31,557,600 x 29,979,245,800
The visible universe is 13.8 billion light years in radius. We can't see further than this, because this is also the age of the universe. Any light from further away simply hasn't had enough time to reach us. However, those galaxies we can see 13.8bly away have, in the time it has taken their light to reach us, continued to move away from us. Where are they now? 46.5 billion light years away. This is known as the comoving distance. Double this radius to get the diameter, and we get 93 billion light years.
We know how big a light year is (in cm), so we simply have to multiply by 93 billion (or if we're lazy, multiply by 93 and then add nine zeroes). We don't have to get lazy, we have a computer algorithm that can do this for us. The result?
That result is 29 digits long, and is several orders of magnitude larger than a 64-bit computer can actually understand is a number.
You can download the PHP code, including a test script that calculates the size of the visible universe on thepage.