# more pure mathematics – fibonacci and mersenne

So I recently posted my small script to calculate pi to a couple hundred thousand digits. This time I thought I would post a couple short python scripts.

The first python script calculates the fibonacci sequence for 100,000 iterations.

def fibonacci(n, f):

fib = [0, 1]

for i in range(n):

fibNum = fib[0] + fib[1]

fib[0] = fib[1]

fib[1] = fibNum

f.write(str(fib[0]) + " ")

return fib[0]

**
**

**with open("fib.txt", "w") as f:
fibonacci(100000, f)
**

The results of this will be a 997 MB file. It does take a bit to run but certainly produces quite a list of numbers.

The next is a python script to calculate the mersenne sequence for 100,000 iterations.

def mersenne(n, f):

x = 0

for i in range(n):

X = 2 * x + 1

x = X

f.write(str(X) + " ")

return x

**
**

`with open("mersenne.txt", "w") as f:`

mersenne(100000, f)

The results of this will be a 1.5 GB file

I should probably make the effort to time the scripts… just for grins…

- chapter two in my fitness journey
- The positive effects of physical fitness

Pingback: Pi at the intersection of pure and applied mathematics | One Mans Anthology