## King’s Day Speech

I just came across Guido van Rossum’s King’s Day Speech from April 2016. I recommend reading it. Until yesterday I didn’t know that the G in Guido is pronounced like the letter ?’ in Hebrew, nor that the Netherlands has a king… But even today, there is nothing I know for certain, even the fact I exist is an assumption and not a known fact. I don’t even know if I have a computer. But anyway I recommend reading this speech.

By the way, if you don’t know who Guido is, you’re probably not an experienced Python programmer, so don’t apply for a job in Speedy Net…

* I’m sure if WordPress was written in Python, it wouldn’t convert the Hebrew letter ?’ to a question mark. That’s what happens when programmers don’t know how to program…

## My first programs in Python

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
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
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
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