Prime Palette

hello
A postcard of a figure created by Sondra Eklund depicting the prime factorizations of the first 100 integers. [Image courtesy of Steven Strogatz and Sondra Eklund via Twitter.]

Identifying and storing prime factors

First, I had to figure out how to identify the prime factors of a given number. I created a function named prime_factors which took in a numerical input and returned a list of numbers that are its prime factors.

def prime_factors(number):
""" Returns a list of prime factors of an input number. """

prime_factors = []
x = 333
dividend = number
divisor = 2

while x == 333:
if dividend == 1:
return prime_factors
else:
if dividend % divisor == 0:
prime_factors.append(divisor)
dividend = dividend/divisor
else:
divisor += 1
def prime_df(number):
""" Returns a list of prime factors of a range of numbers from 2
to the input number. """
num_prime_array = np.array(([2, 2, 1]))if number > 2:
for this_num in range(3, number+1):
num_primes = prime_factors(this_num)

#saving out numbers' unique primes and their counts
if this_num > 1:
unique, counts = np.unique(num_primes,
return_counts=True)
freq = np.asarray((list(unique), list(counts))).T
num_id = np.array([[this_num] * len(freq)]).T
num_id_unique_counts = np.hstack((num_id, freq))
num_prime_array = np.vstack((num_prime_array,
num_id_unique_counts))
num_prime_df = pd.DataFrame(num_prime_array)
else:
num_prime_df = pd.DataFrame([num_prime_array], index=[0])

num_prime_df.columns = ['Numbers', 'Primes', 'Powers']
return num_prime_df

Depicting prime factors

To envision the prime factors of a set of numbers, I wanted to use a typical cartesian graph, with the set of numbers on the x-axis and the prime factors of the set numbers on the y-axis. This would allow one to readily see the frequencies between factors, and similarly to Sondra’s striking depiction, which factors were commonly shared across numbers.

cmap = sns.blend_palette([color_1,color_2],n_colors=max(num_prime_df.Powers))

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Athula Pudhiyidath

Athula Pudhiyidath

I’m a data scientist with neuroscientific roots. Learn more about me here: https://athpud.com/