NOTE: I have currently stopped work on Project Euler, and probably will for some time to come. I still believe working out solutions to Project Euler with NumPy is both a great way to learn vectorized computing and one of the most performant ways of doing the calculations, but do not have to time to put in anymore.
This Jupyter Notebook contains solutions to Project Euler, planned for problems 51 to 75. For sake of practice, speed and ease of manipulation, these are done with the NumPy library, providing numerical arrays meant for fast computation.
See:
In [1]:
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
By replacing the 1st digit of the 2-digit number *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.
By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.
Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.
In [41]:
# Start with 9-digit numbers, go upwards from there.
# Need to go through permutations of nnnnnnnn1, nnnnnnnn2, ...
# nnnnnnn1n, nnnnnnn2n, etc.
# Call these "rotations"? In numpy parlance, it's "roll".
a = np.concatenate([np.zeros(9 - 1, np.int32) , np.array([1], np.int32)])
b = np.roll(a,1)
print(a, b)
# For the zeros in those arrays, we try numbers range(1, 10)
# and see if they're all prime. For the non-zeros in the array,
# we iterate over digits range(10). If 9-digit numbers don't
# work, we try with 10 digits, ans so on.