Problem p00222 - Generation 3

Orig Description

Quadruplet Primes
  A set of four prime numbers arranged in the form (a, a+2, a+6, a+8) is called a quadruplet prime. Among the four prime numbers that make up the quadruplet prime, the largest number is called the size of the quadruplet prime. For example, the smallest quadruplet prime of size 13 is the set (5, 7, 11, 13), and the next largest quadruplet prime of size 19 is the set (11, 13, 17, 19).
  Write a program that takes an integer n (13 ≤ n ≤ 10,000,000) as input and outputs the size of the largest quadruplet prime whose size is less than or equal to n.
Input
  The input consists of multiple datasets. The end of input is indicated by a line containing a single zero.
For each dataset, a single integer n is given on a line.
  The number of datasets is no more than 2000.
Output
  For each input dataset, output the size of the largest quadruplet prime on a single line.
Sample Input
13
14
15
16
17
18
19
20
10000
0
Output for the Sample Input
13
13
13
13
13
13
19
19
9439

Extracted Specification

An integer n (13 ≤ n ≤ 10,000,000).
Multiple datasets are provided. The end of input is indicated by a line containing a single zero.
The number of datasets is no more than 2000.

### Example Input:

```
13
14
15
16
17
18
19
20
10000
0
```

### Function Signature:
Write a function f(inputs) that takes in the input. 
def f(inputs: List[int]): 
    ''' 
    inputs: a list of integers
    '''

Test Case Generator

To generate test cases for the "Quadruplet Primes" problem, we need to ensure that we generate valid inputs within the specified range and also handle multiple datasets. Here is a test case generator function for this problem:

```python
import random

def generate_primes_up_to(n):
    """ Helper function to generate all prime numbers up to n using the Sieve of Eratosthenes. """
    sieve = [True] * (n + 1)
    sieve[0] = sieve[1] = False  # 0 and 1 are not primes
    for start in range(2, int(n**0.5) + 1):
        if sieve[start]:
            for multiple in range(start * start, n + 1, start):
                sieve[multiple] = False
    return [num for num, is_prime in enumerate(sieve) if is_prime]

def find_quadruplet_primes(primes):
    """ Helper function to find all quadruplet primes from the list of primes. """
    quadruplet_primes = []
    prime_set = set(primes)
    for prime in primes:
        if prime + 2 in prime_set and prime + 6 in prime_set and prime + 8 in prime_set:
            quadruplet_primes.append(prime + 8)
    return quadruplet_primes

def tcgen():
    # Number of datasets
    num_datasets = random.randint(1, 2000)
    
    # Generate a list of datasets
    datasets = [random.randint(13, 10000000) for _ in range(num_datasets)]
    
    # Append 0 at the end to indicate the end of input
    datasets.append(0)
    
    # Generate all primes up to the maximum value in the datasets
    max_n = max(datasets)
    primes = generate_primes_up_to(max_n)
    
    # Find all quadruplet primes
    quadruplet_primes = find_quadruplet_primes(primes)
    
    # Function to find the largest quadruplet prime less than or equal to n
    def largest_quadruplet_prime(n):
        for qp in reversed(quadruplet_primes):
            if qp <= n:
                return qp
        return None
    
    # Generate outputs for each dataset
    outputs = [largest_quadruplet_prime(n) for n in datasets if n != 0]
    
    return datasets, outputs

# Example of usage:
datasets, outputs = tcgen()
print("Sample Input:")
print("\n".join(map(str, datasets)))
print("Sample Output:")
print("\n".join(map(str, outputs)))
```

### Explanation:

1. **generate_primes_up_to**: This helper function generates all prime numbers up to a given number \( n \) using the Sieve of Eratosthenes.

2. **find_quadruplet_primes**: This helper function finds all quadruplet primes from a list of prime numbers. It returns a list of the largest numbers in each quadruplet.

3. **tcgen**: This function:
    - Randomly decides the number of datasets (between 1 and 2000).
    - Generates random values of \( n \) for each dataset within the given range (13 to 10,000,000).
    - Ensures the end of input with a 0.
    - Uses the helper functions to generate all necessary primes and find the quadruplet primes.
    - For each dataset value, finds the largest quadruplet prime that is less than or equal to \( n \) and prepares the output.

This generator provides both the input datasets and the corresponding outputs. You can use the generated inputs to test your solution and verify its correctness against the outputs.

Extract Arguments

def extract_arguments(fh):
    inputs = []
    while True:
        n = int(fh.readline().strip())
        if n == 0:
            break
        inputs.append(n)
    return inputs

# if __name__ == "__main__":
#     input_path = sys.argv[1]
#     with open(input_path, 'r') as fh: 
#     inputs = extract_arguments(fh)
#     f(inputs)