""" Prime generating integers https://projecteuler.net/problem=357 """ from eulerlib import Crible crible = Crible(100000000) def diviseurs(n): d = 1 while d * d <= n: q, r = divmod(n, d) if r == 0: if not crible.est_premier(d + q): return False d += 1 return True assert diviseurs(30) sum = 1 # pour n=1 for n in range(2, 100000000, 2): if diviseurs(n): sum += n print(n) print(sum)
""" Totient permutation https://projecteuler.net/problem=70 """ from eulerlib import Crible crible = Crible(10000000) phi = crible.EulerPhi() def est_permutation(a, b): chiffres = [0] * 10 while a != 0: a, r = divmod(a, 10) chiffres[r] += 1 while b != 0: b, r = divmod(b, 10) chiffres[r] -= 1 return all(c == 0 for c in chiffres) rmin, nmin = 10, 0 for n in range(2, 10000000): p = phi[n] r = n / p if r < rmin: if est_permutation(n, p): rmin, nmin = r, n print(nmin)
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. https://projecteuler.net/problem=37 """ from eulerlib import Crible crible = Crible(1000000) nb = 0 somme = 0 for p in crible.liste(): if p < 10: continue ko = False gauche = p droite = 0 i = 0 while gauche >= 10 and not ko:
""" Quadratic primes https://projecteuler.net/problem=27 """ from eulerlib import Crible crible = Crible(1000) premiers = crible.liste() premiers.extend([-i for i in premiers]) solution = 0 n_max, a_max, b_max = 0, 0, 0 for b in premiers: for a in range(-999, 1000): for n in range(0, 1000): p = n * n + a * n + b if not crible.est_premier(p): if n > n_max: # print("nouveau max", n, a, b) n_max, a_max, b_max = n, a, b break print(a_max * b_max) print("n²{:+d}n{:+d} ⇒ {} premiers".format(a_max, b_max, n_max))
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. What 12-digit number do you form by concatenating the three terms in this sequence? https://projecteuler.net/problem=49 """ from eulerlib import Crible crible = Crible(10000) premiers = crible.liste() premiers = [i for i in premiers if i >= 1000] nb = len(premiers) for i in range(nb - 2): p1 = premiers[i] # exclut la solution de l'énoncé if p1 == 1487: continue for j in range(i + 1, nb - 1): p2 = premiers[j] p3 = p2 + (p2 - p1)
""" Summation of primes The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. https://projecteuler.net/problem=10 """ from eulerlib import Crible MAX = 2000000 # solution avec crible crible = Crible(MAX) print(sum(crible.liste()))
""" from eulerlib import Crible def rotation(n): nombres = [] chiffres = 0 q = n while q != 0: q //= 10 chiffres += 1 decalage = 10**(chiffres - 1) for _ in range(chiffres): n, r = divmod(n, 10) n += r * decalage nombres.append(n) return nombres crible = Crible(1000000) resultat = 0 for i in crible.liste(): if all([crible.est_premier(j) for j in rotation(i)]): resultat += 1 print(resultat)