- Sun 10 November 2019
- analysis
- Christophe
- #Python
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]:
True
In [3]:
isPrime(458)
Out[3]:
False
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]:
[{'age': 2, 'year': 1912, 'Prime age': True, 'Prime Year': False}, {'age': 3, 'year': 1913, 'Prime age': True, 'Prime Year': True}, {'age': 5, 'year': 1915, 'Prime age': True, 'Prime Year': False}, {'age': 7, 'year': 1917, 'Prime age': True, 'Prime Year': False}, {'age': 11, 'year': 1921, 'Prime age': True, 'Prime Year': False}, {'age': 13, 'year': 1923, 'Prime age': True, 'Prime Year': False}, {'age': 17, 'year': 1927, 'Prime age': True, 'Prime Year': False}, {'age': 19, 'year': 1929, 'Prime age': True, 'Prime Year': False}, {'age': 21, 'year': 1931, 'Prime age': False, 'Prime Year': True}, {'age': 23, 'year': 1933, 'Prime age': True, 'Prime Year': True}, {'age': 29, 'year': 1939, 'Prime age': True, 'Prime Year': False}, {'age': 31, 'year': 1941, 'Prime age': True, 'Prime Year': False}, {'age': 37, 'year': 1947, 'Prime age': True, 'Prime Year': False}, {'age': 39, 'year': 1949, 'Prime age': False, 'Prime Year': True}, {'age': 41, 'year': 1951, 'Prime age': True, 'Prime Year': True}, {'age': 43, 'year': 1953, 'Prime age': True, 'Prime Year': False}, {'age': 47, 'year': 1957, 'Prime age': True, 'Prime Year': False}, {'age': 53, 'year': 1963, 'Prime age': True, 'Prime Year': False}, {'age': 59, 'year': 1969, 'Prime age': True, 'Prime Year': False}, {'age': 61, 'year': 1971, 'Prime age': True, 'Prime Year': False}, {'age': 63, 'year': 1973, 'Prime age': False, 'Prime Year': True}, {'age': 67, 'year': 1977, 'Prime age': True, 'Prime Year': False}, {'age': 69, 'year': 1979, 'Prime age': False, 'Prime Year': True}, {'age': 71, 'year': 1981, 'Prime age': True, 'Prime Year': False}, {'age': 73, 'year': 1983, 'Prime age': True, 'Prime Year': False}, {'age': 77, 'year': 1987, 'Prime age': False, 'Prime Year': True}, {'age': 79, 'year': 1989, 'Prime age': True, 'Prime Year': False}, {'age': 83, 'year': 1993, 'Prime age': True, 'Prime Year': True}, {'age': 87, 'year': 1997, 'Prime age': False, 'Prime Year': True}, {'age': 89, 'year': 1999, 'Prime age': True, 'Prime Year': True}, {'age': 93, 'year': 2003, 'Prime age': False, 'Prime Year': True}, {'age': 97, 'year': 2007, 'Prime age': True, 'Prime Year': False}, {'age': 101, 'year': 2011, 'Prime age': True, 'Prime Year': True}, {'age': 103, 'year': 2013, 'Prime age': True, 'Prime Year': False}, {'age': 107, 'year': 2017, 'Prime age': True, 'Prime Year': True}, {'age': 109, 'year': 2019, 'Prime age': True, 'Prime Year': False}, {'age': 113, 'year': 2023, 'Prime age': True, 'Prime Year': False}, {'age': 117, 'year': 2027, 'Prime age': False, 'Prime Year': True}, {'age': 119, 'year': 2029, 'Prime age': False, 'Prime Year': True}]
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]:
{'Prime Ages': 30, 'Prime Years': 19, 'Double': 8}
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]:
{'Prime Ages': 22, 'Prime Years': 12, 'Double': 7}
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]:
16.25974025974026
In [13]:
statistics.mean(yd)
Out[13]:
3.8181818181818183
In [14]:
print(statistics.mode(yd),statistics.mode(yp))
0 16
In [15]:
print(statistics.stdev(yd),statistics.stdev(yp))
4.191276466540036 1.2792925769576962
In [16]:
print(statistics.median(yd),statistics.median(yp))
1 16
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)
[1789, 1791, 1793, 1795, 1797, 1801, 1803, 1805, 1807, 1811, 1813, 1815, 1817, 1819, 1823, 1825, 1827, 1831, 1833, 1835, 1837, 1839, 1841, 1843, 1847, 1849, 1851, 1853, 1855, 1857, 1861, 1863, 1867, 1873, 1879, 1881, 1883, 1885, 1889, 1891, 1893, 1895, 1897, 1901, 1903, 1907, 1909, 1913, 1915, 1917, 1919, 1921, 1923, 1925, 1927, 1933, 1935, 1937, 1939, 1941, 1943, 1945, 1951, 1953, 1955, 1957, 1959, 1961, 1963, 1965, 1967, 1969, 1973, 1975, 1979, 1981, 1983, 1987, 1989, 1993, 1999, 2003, 2005, 2007, 2011, 2013, 2017, 2019]
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]:
(0.0, 90.0)
So it turns out having double prime birthday in your lifetime is not usual.
In [20]:
len(no_double)
Out[20]:
88
So between 1789 and 2020, 88 years will result in no double prime birthdays over 231 years.