## Consider the consecutive primes $p_1 = 19$ and $p_2 = 23$. It can be verified that $1219$ is the smallest number such that the last digits are formed by $p_1$ whilst also being divisible by $p_2$.

In fact, with the exception of $p_1 = 3$ and $p_2 = 5$, for every pair of consecutive primes, $p_2 \gt p_1$, there exist values of $n$ for which the last digits are formed by $p_1$ and $n$ is divisible by $p_2$. Let $S$ be the smallest of these values of $n$.

Find $\sum S$ for every pair of consecutive primes with $5 \le p_1 \le 1000000$.

### To find the value of S for each pair of consecutive primes, we can iterate through all prime numbers greater than 5, and for each pair (p1, p2), find the smallest number (S) that satisfies the given conditions. Finally, we sum up all the values of S.

Here’s the Python code to solve this problem:

“`python

import math

# Function to check if a number is prime

def is_prime(n):

if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
# Function to find the smallest number (S) for a given pair of primes (p1, p2)
def find_S(p1, p2):
# The last digit of p1
last_digit_p1 = p1 % 10
# Find the smallest number (S) where the last digits are formed by p1 and S is divisible by p2
S = p1
while S % p2 != 0 or S % 10 != last_digit_p1:
S += p1
return S
# Set the initial sum to 0
sum_S = 0
# Iterate through all prime numbers greater than 5
for p1 in range(5, 1000001):
if is_prime(p1):
# Find the next prime number greater than p1
p2 = p1 + 1
while not is_prime(p2):
p2 += 1
# Find the smallest number (S) for the given pair (p1, p2)
S = find_S(p1, p2)
sum_S += S
print("Sum of S for every pair of consecutive primes with 5 <= p1 <= 1000000: ", sum_S)
```
This code uses the `is_prime` function to check if a number is prime and the `find_S` function to find the smallest number (S) for a given pair of primes (p1, p2). Finally, it iterates through all prime numbers greater than 5, finds the sum of S for each pair, and prints the result.
Note: Since the problem statement specifies 5 <= p1 <= 1000000, the code checks for primes starting from 5. If you want to solve the problem for a different range, you can modify the range in the `for` loop.

##### More Answers:

Prime Cube PartnershipLarge Repunit Factors

Repunit Nonfactors