On prime birthdays.
Posted on Sun 10 November 2019 in analysis
Using jupyter notebook, to figure out how my next prime brithday.
Find your prime and double prime birthday¶
Prime Birthday definition, we define a prime birthday as:
- when your reach an age which is a prime number (2 year old,3 year old,5th birthday,7th,11th...)
- when the year of your birthday is a prime. Double Prime Birthday:
- when both your age and the year are a prime
How frequent are prime birthday, in your lifetime and double prime ?
In [1]:
#Check if a number is prime
# Using Sieve of Eratosthenes ( https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes )
def ListPrimes(n):
#init
primes = [True for i in range(0,n+1)]
for index,value in enumerate(primes):
if index == 1 or index == 0:
primes[index] = False
if primes[index] == True:
next_index = index*2
while next_index <= n:
primes[next_index] = False
next_index += index
return primes
def isPrime(n):
primes = ListPrimes(n)
return primes[n]
In [2]:
#Basic test
isPrime(3)
Out[2]:
In [3]:
isPrime(458)
Out[3]:
In [4]:
#Based on year you are born, calculte all prime birthdays.
def prime_birthday(year_born,age_limit=125):
primes = ListPrimes(year_born+age_limit)
bday = []
count = 0
for age in range(age_limit+1):
if primes[age] or primes[year_born+age]:
count += 1
bday.append({'age':age,'year':year_born+age,'Prime age':primes[age],'Prime Year':primes[year_born+age]})
return bday
In [5]:
# example for someboday born in 1910
prime_birthday(1910)
Out[5]:
In [6]:
#Summarize
def SumPrimeBday(year_born,age_limit=125):
bday = prime_birthday(year_born,age_limit)
count_prime_age = 0
count_prime_year = 0
count_double = 0
for b in bday:
if b['Prime age']:
count_prime_age += 1
if b['Prime Year']:
count_prime_year += 1
if b['Prime age'] and b['Prime Year']:
count_double += 1
return {'Prime Ages':count_prime_age,'Prime Years':count_prime_year,'Double':count_double}
In [7]:
SumPrimeBday(1976)
Out[7]:
So the number of potential prime age birthdays will be the same for everybody, 30 if you live till 125.
In [8]:
SumPrimeBday(1970,80)
Out[8]:
But only 22 if you live till 80. So the difference depending on your age will be the birth year.
In [9]:
#Using french revolution and current year as window
pbday_by_birth_year =[SumPrimeBday(year) for year in range(1789,2020)]
In [10]:
#Plot
import matplotlib.pyplot as plt
%matplotlib inline
years = [year for year in range(1789,2020)]
yp = [y['Prime Years'] for y in pbday_by_birth_year]
plt.plot(years,yp)
plt.ylabel('Prime Years count')
plt.xlabel('Year of birth')
plt.show()
So being born in the 60/70 does provide an advantage in term of primes years you will encounter (assuming you live till 125).
In [11]:
yd = [y['Double'] for y in pbday_by_birth_year]
plt.plot(years,yd)
plt.ylabel('Double Prime Birthday count')
plt.xlabel('Year of birth')
plt.show()
In [12]:
import math
import statistics
statistics.mean(yp)
Out[12]:
In [13]:
statistics.mean(yd)
Out[13]:
In [14]:
print(statistics.mode(yd),statistics.mode(yp))
In [15]:
print(statistics.stdev(yd),statistics.stdev(yp))
In [16]:
print(statistics.median(yd),statistics.median(yp))
In [17]:
no_double = [years[index] for index,value in enumerate(yd) if value == 0]
In [18]:
#People born in those years will have no double prime birthday !!
print(no_double)
In [19]:
import numpy as np
# An "interface" to matplotlib.axes.Axes.hist() method
n, bins, patches = plt.hist(x=yd, bins=[i for i in range(20)], color='#0504aa',
alpha=0.7, rwidth=0.85)
plt.grid(axis='y', alpha=0.75)
plt.xlabel('Double Primes Birthday')
plt.ylabel('Frequency')
plt.title('Histogram')
maxfreq = n.max()
# Set a clean upper y-axis limit.
plt.ylim(ymax=np.ceil(maxfreq / 10) * 10 if maxfreq % 10 else maxfreq + 10)
Out[19]:
So it turns out having double prime birthday in your lifetime is not usual.
In [20]:
len(no_double)
Out[20]:
So between 1789 and 2020, 88 years will result in no double prime birthdays over 231 years.