I started to learn Python a few days ago and wrote a few sample programs. I noticed that integers in Python are not limited in size, unlike many other programming languages like PHP, C, C++ and Java. So my first programs in Python were simple calculations of big numbers, for example big powers of 2 – I even calculated numbers with hundreds of thousands of digits. So I decided to use Python to calculate the mathematical constant e. I wrote a short program to calculate the first 5000 digits of e (actually that’s 5000 digits after the dot), and surprisingly I didn’t have to use any module, not even the math module. I only used integers to calculate e times 10 to the power of 5000. I searched Google for “the first 5000 digits of e” and found out a page with the first 2 million digits of the number e, which I viewed to check if my calculations were correct. I also found a page titled “How I computed e to 20000 digits“, where I found out it took the author more than 9 hours to calculate the first 20000 digits of e. It’s surprising, because my program calculates the first 5000 digits of e in less than a second, and also when calculating 20000 digits it takes about one or two seconds. I don’t know what caused the author’s program to take so much time, but I decided to post my e calculation program here:
digits = 5000 add = 500 f = 10**(digits + add) e = 0 n = 0 while (f > 0): # add current inverse factorial to e. e += f # calculate next inverse factorial. n += 1 f /= n # print e e /= (10**add) print e print n
Notice that I used the variable “add” with the value of 500 – I actually calculated 5500 digits of e and then omitted the last 500 digits. This is because the calculation is not accurate, because I used integers and not exact numbers. When calculating 5000 digits without using “add” (or when add = 0), the last 4 digits are not correct. So I could use a much smaller number for add, but I decided to go for 500 digits just to be on the safe side. It took 1929 iterations to calculate the first 5000 digits of e, and the number of iterations grows with the number of digits – for example, it takes 13646 iterations to calculate the first 50000 digits of e.
I went on and wrote another Python program to calculate square roots, and used it to calculate the square root of 2 (although the same program can be used to calculate the square root of any positive integer, whether the square root is an integer or not). Here is the program to calculate the first 5000 digits of the square root of 2:
# calculate next square root of the number. def calculate_next_square_root(square_root): next_square_root = ((number / square_root) + square_root) / 2 return next_square_root number = 2 digits = 5000 add = 500 number *= 10**((digits + add) * 2) square_root = 1 * (10**(digits + add)) # calculate next square root of the number. next_square_root = calculate_next_square_root(square_root) n = 0 while (next_square_root != square_root): # replace square root with next square root. square_root = next_square_root # calculate next square root of the number. next_square_root = calculate_next_square_root(square_root) n += 1 # print square_root square_root /= (10**add) print square_root print n
Here I used a function – calculate_next_square_root(square_root), although it’s possible to write the same program without functions. It’s interesting to note that this program takes only 13 iterations to calculate the first 5000 digits (after the dot) of the square root of 2, while the e program took 1929 iterations. And when calculating the first 50000 digits, this program takes only 17 iterations. This is because the number of iterations it takes is proportional to the logarithm of the number of digits, because every new iteration doubles the number of correct digits of the square root calculated. So If we wanted to calculate the first google digits of the square root of 2, this program would take only about 332 iterations. The problem is that we will need enough memory to store numbers with google digits, which we don’t have and we don’t expect to have in the future. So we can’t use this program to calculate the first google digits of the square root of 2. But we can use it to calculate the first 100000 digits of the square root of 2 – I tried and it took 18 iterations.
Another thing – the square root program returns correct results even with add = 0. This is because the result is always the closest integer to the square root calculated (2 times 10 to the power of digits*2), rounded down. If the square root is an integer, the result will be accurate. If not, the result is the closest integer rounded down. I will not go into the mathematics to prove it, but this algorithm is accurate even without using “add”. But I left it at 500 digits just in case.