Find the closest prime number under a certain integer ```n``` that has the maximum possible amount of even digits.
For ```n = 1000```, the highest prime under ```1000``` is ```887```, having two even digits (8 twice)
Naming ```f()```, the function that gives that prime, the above case and others will be like the following below.
```
f(1000) ---> 887 (even digits: 8, 8)
f(1210) ---> 1201 (even digits: 2, 0)
f(10000) ---> 8887
f(500) ---> 487
f(487) ---> 467
``` 
Features of the random tests:
```
Number of tests = 28
1000 <= n <= 5000000
```
Enjoy it!!
import itertools
compress = itertools.compress
from bisect import bisect_right,bisect_left

def prime(n):
    if n < 2:
        return []
    r = [False,True] * (n//2)+[True]
    r[1],r[2]=False, True
    for i in range(3,int(1 + n**0.5),2):
        if r[i]:
            r[i*i::2*i] = [False] * int((n+2*i-1-i*i)/(2*i))
    r = list(compress(list(range(len(r))),r))
    if r[-1] %2 == 0:
        r.pop() 
    return r
primes = prime(5*10**6) 

def even(n): 
    even_count = 0 
    while n > 0: 
        rem = n%10 
        if not rem%2: 
            even_count += 1
        n //= 10
    return even_count

def f(n): 
    n -= 1
    upper_bound = bisect_right(primes,n)
    best = -1 
    bestCount = 0
    for i in reversed(list(range(0,upper_bound))): 
        if len(str(primes[i])) <= bestCount:
            break 
        count = even(primes[i])
        if bestCount < count: 
            best = primes[i]
            bestCount = count 
        elif bestCount == count:
            if primes[i] > best: 
                best = primes[i] 
                bestCount = count 
    return best

import itertools
compress = itertools.compress
from bisect import bisect_right,bisect_left

def prime(n):
    if n < 2:
        return []
    r = [False,True] * (n//2)+[True]
    r[1],r[2]=False, True
    for i in range(3,int(1 + n**0.5),2):
        if r[i]:
            r[i*i::2*i] = [False] * int((n+2*i-1-i*i)/(2*i))
    r = list(compress(list(range(len(r))),r))
    if r[-1] %2 == 0:
        r.pop() 
    return r
primes = prime(5*10**6) 

def even(n): 
    even_count = 0 
    while n > 0: 
        rem = n%10 
        if not rem%2: 
            even_count += 1
        n //= 10
    return even_count

def f(n): 
    n -= 1
    upper_bound = bisect_right(primes,n)
    best = -1 
    bestCount = 0
    for i in reversed(list(range(0,upper_bound))): 
        if len(str(primes[i])) <= bestCount:
            break 
        count = even(primes[i])
        if bestCount < count: 
            best = primes[i]
            bestCount = count 
        elif bestCount == count:
            if primes[i] > best: 
                best = primes[i] 
                bestCount = count 
    return best
