示例#1
0
def is_truncatable_prime(p):
    if p < 10:
        return False
    p_str = str(p)
    for i in range(1, len(p_str)):
        left = int(p_str[:i])
        right = int(p_str[i:])
        if not math_ext.is_prime(left) or not math_ext.is_prime(right):
            return False
    return True
示例#2
0
def prime_decompose(x):
    if x == 1 or math_ext.is_prime(x):
        return [x]
    factors = []
    for p in primes():
        if p > x:
            break
        if x % p == 0:
            q = 1
            while x % p == 0:
                x = int(x / p)
                q *= p
            factors.append(q)
            if math_ext.is_prime(x):
                factors.append(x)
                break
    return sorted(factors)
示例#3
0
def has_number_of_primes(numbers, prime_count):
    threashold = len(numbers) - prime_count
    for n in numbers:
        if not math_ext.is_prime(n):
            threashold -= 1
        if threashold < 0:
            return False
    return True
示例#4
0
def solve():
    max_elem = {"a": 1, "b": 41, "len": 40}
    for b in range(0, 1001):
        if abs(b) <= max_elem["len"] or not math_ext.is_prime(b):
            continue
        for a in range(-1000, 1001):
            if get_prime_length(a, b) > max_elem["len"]:
                max_elem = {"a": a, "b": b, "len": get_prime_length(a, b)}
    return max_elem["a"] * max_elem["b"]
示例#5
0
def solve():
    corners, primes = [], []
    for n in math_ext.corners():
        corners.append(n)
        if math_ext.is_prime(n, k=50):
            primes.append(n)
        if (len(corners) - 1) % 4 == 0 and len(corners) > 2:
            if len(primes) / len(corners) < 0.1:
                return math.floor((len(corners) + 2) / 4) * 2 + 1
示例#6
0
def is_goldbach(n):
    for i in range(1, n + 1):
        j = n - 2 * (i**2)
        print("{} = {} + 2 * {} ^ 2".format(n, j, i))
        if j < 0:
            return False
        if j == 1 or math_ext.is_prime(j):
            return True
    return False
示例#7
0
def get_max_consecutive_prime_sum(threashold):
    primes = list(slice_seq(math_ext.primes(), 0, threashold))
    start, end = 0, 1
    for i in range(len(primes)):
        for j in range(i + end - start, len(primes)):
            p = sum(primes[i:j])
            if p >= threashold:
                break
            if math_ext.is_prime(p):
                start, end = i, j
    return sum(primes[start:end])
示例#8
0
def is_circular_prime(x):
    return all([math_ext.is_prime(y) for y in get_circular_numbers(x)])
示例#9
0
def composite_numbers():
    return filter(lambda n: not math_ext.is_prime(n), itertools.count(4))
示例#10
0
def is_prime_pair(p1, p2):
    p3 = int(str(p1) + str(p2))
    p4 = int(str(p2) + str(p1))
    return math_ext.is_prime(p3) and math_ext.is_prime(p4)
示例#11
0
def test_is_prime():
    assert not math_ext.is_prime(-1)
    assert not math_ext.is_prime(0)
    assert not math_ext.is_prime(1)
    assert math_ext.is_prime(2)
    assert math_ext.is_prime(3)
    assert not math_ext.is_prime(4)
    assert math_ext.is_prime(5)
    assert not math_ext.is_prime(6)
    assert math_ext.is_prime(7)
    assert not math_ext.is_prime(8)
    assert not math_ext.is_prime(9)
    assert not math_ext.is_prime(10)

    pseudo_primes = [
        341, 561, 645, 1105, 1387, 1729, 1905, 2047, 2465, 2701, 2821, 3277,
        4033, 4369, 4371, 4681, 5461, 6601, 7957, 8321, 8481, 8911
    ]
    assert all([not math_ext.is_prime(n, k=50) for n in pseudo_primes])