Chain of primes X22094


Statement
 

pdf   zip

You have to program the function @has_prime_chain@ below. Remember that a non negative integer nn is prime if and only if nn is greater than one and the only divisors of nn are one and nn. The following auxiliar function may be helpful.

def is_prime(n):
    '''
    Requires a non negative integer n.
    Returns True when n is prime 
    Returns False when n is not prime
    '''
    if  n < 2:
        return False
    d = 2
    while d*d <= n:
        if n%d == 0:
            return False
        d += 1
    return True
  • Write a function @has_prime_chain(f, k)@ that given a list ff of non negative integers and an integer kk greater than zero returns the first valid index of ff where a chain of primes of size kk starts. If there is no such valid index it has to return 1-1. A chain of primes is a block of consecutive numbers in the list all of them being prime, delimited by non prime numbers or the ends of the list.

Scoring

The function counts 100 points.

Sample session

Sample session
>>> has_prime_chain([6, 2, 3, 5, 2], 3)
-1
>>> has_prime_chain([6, 2, 3, 5, 2], 4)
1
>>> has_prime_chain([1, 2, 3, 4, 5, 7, 11], 3)
4
>>> has_prime_chain([2, 3, 4, 5, 6, 7], 1)
3
Information
Author
ProAl
Language
English
Official solutions
Python
User solutions
Python