def first_approach(): coefficients = [] for a in range(-999, 1000): for b in range(-1000, 1001): if not is_prime(euler_polynomial(a, b, 0)): continue else: x = 1 while is_prime(euler_polynomial(a, b, x)): x += 1 coefficients.append((a, b, x)) a, b, _ = max(coefficients, key=lambda k: k[2]) print('Solution =', a * b)
def generate_candidates_from_prime(prime): candidates = set() for candidate in permutations(str(prime)): candidate = int(''.join(candidate)) if is_prime(candidate) and log10(candidate) >= 3: candidates.add(candidate) return candidates
def sum_strong_right_truncatable_harshad_primes(): result = 0 for h in takewhile(lambda x: x < LIMIT, right_truncatable_harshad_numbers()): if is_strong(h): for d in range(1, 10, 2): candidate = 10 * h + d if is_prime(candidate): result += candidate return result
def attempt(): result = 0 for places in range(2, 10): print(places, 'places') for pandigital in permutations('123456789'[:places]): pandigital = int(''.join(pandigital)) if is_prime(pandigital): print(' ', pandigital) result = max(result, pandigital) return result
def attempt(): limit = 10**6 prime = 0 terms = 1 prime_sums = [0] for p in prime_number_generator(): prime_sums.append(prime_sums[-1] + p) if prime_sums[-1] >= limit: break candidates = [] for j in range(len(prime_sums)): for k in range(j): candidate = prime_sums[j] - prime_sums[k] if is_prime(candidate): candidates.append((candidate, j - k)) break return max(candidates, key=lambda x: x[terms])[prime]
def is_strong(h): return is_prime(h / digit_sum(h))
def count_primes(set_of_numbers): return len({p for p in set_of_numbers if is_prime(p)})
"""Detect primitive roots of a prime module.""" import sys from mathext import euler_phi, relative_primes, is_prime MODULE = 99989 # MODULE = 31 if __name__ == '__main__': if not is_prime(MODULE): sys.exit() print('Module:', MODULE) print('This module has', euler_phi(MODULE - 1), 'primitive roots.') print('Probing the first primitive root ...') for a in range(2, MODULE - 1): print('Caandidate:', a) for k in range(1, MODULE - 1): x = pow(a, k, MODULE) if k < (MODULE - 1) and x == 1: print(a, 'is not a primitive root.') break else: print(a, 'is a primitive root for module', MODULE) pw0 = a break rp = relative_primes(MODULE - 1) print('The set of totients of the totient count is:', rp) pw = [] for k in rp: pw.append(pow(pw0, k, MODULE)) print('Primitive roots:', sorted(pw))