def candidates_4(maxi): n2phi = {} primes = getPrimes(maxi) for p in primes: n2phi[p] = p - 1 for n, m in combinations(primes, 2): if n * m < maxi: n2phi[n * m] = n2phi[n] * n2phi[m] return [(n / p, n) for n, p in n2phi.items() if isPermutation(n, str(p))]
def candidates_3(maxi): n2phi = {} primes = getPrimes(maxi) for p in primes: n2phi[p] = p - 1 for n, m in combinations(primes, 2): if n * m < maxi: n2phi[n * m] = n2phi[n] * n2phi[m] for n in range(2, maxi): if not n in n2phi.keys(): n2phi[n] = phi(n) if n % 2 == 0: fact = 2 m = fact * n while m <= maxi: n2phi[m] = fact * n2phi[n] fact *= 2 m = fact * n return [(n / p, n) for n, p in n2phi.items() if isPermutation(n, str(p))]
def getCandidates(lengthOfCandidates, n): primes = getPrimes(int("9"*lengthOfCandidates)) return [i for i in primes if len(str(i)) == lengthOfCandidates and hasAsLeastNSameDigits(i, n)]
''' Created on Jun 2, 2015 n² + an + b = prime, where |a| < 1000 and |b| < 1000 b is prime ''' from utilities.divisors import getPrimes, isPrime if __name__ == '__main__': maximum = (1, 1, 1) for b in getPrimes(1000): for a in range(-999, 1000, 2): res = b n = 0 while isPrime(res): n += 1 res = n ** 2 + a * n + b if(maximum[-1] < n): print("scooby doo ", maximum[-1], n) maximum = (a, b, n) print(a, b, n) print(maximum) print(maximum[0] * maximum[1])
''' The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. How many circular primes are there below one million? ''' from utilities.divisors import getPrimes, isPrime def allRotations(x): return [str(x)[i:] + str(x)[:i] for i in range(len(str(x)))] if __name__ == '__main__': print(len([x for x in getPrimes(1000000) if all(isPrime(int(rot)) for rot in allRotations(x))]))
d2d3d4=406 is divisible by 2 d3d4d5=063 is divisible by 3 d4d5d6=635 is divisible by 5 d5d6d7=357 is divisible by 7 d6d7d8=572 is divisible by 11 d7d8d9=728 is divisible by 13 d8d9d10=289 is divisible by 17 Find the sum of all 0 to 9 pandigital numbers with this property. ''' from itertools import permutations from utilities.divisors import getPrimes primes = getPrimes(17) def foursHasProperty(pandigital): return all([int(pandigital[i:i + 3]) % primes[i - 1] == 0 for i in range(1, 8)]) if __name__ == '__main__': print(sum([int("".join(p)) for p in permutations("0123456789") if foursHasProperty("".join(p))]))
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. Which prime, below one-million, can be written as the sum of the most consecutive primes? """ from utilities.divisors import getPrimes, isPrime def consecutivePrimesFrom(primes, start, maxvalue): result = [] for i in range(start + 1, len(primes)): partsum = sum(primes[start : i + 1]) if isPrime(partsum) and partsum < maxvalue: result.append(primes[start : i + 1]) if partsum > maxvalue: return result return result if __name__ == "__main__": maxvalue = pow(10, 6) primes = getPrimes(maxvalue) candidates = [ consecutivePrimesFrom(primes, i, maxvalue)[-1] for i in range(maxvalue) if len(consecutivePrimesFrom(primes, i, maxvalue)) > 0 ] maxi = max(candidates, key=lambda l: len(l)) print(sum(maxi), maxi)
def clever_primes(maxi, distance): return [p for p in getPrimes(maxi) if abs(p - int(sqrt(maxi))) < distance]
def ugly(): for p in getPrimes(): if foursHasProperty(str(p)): yield p
def testGetPrimes(self): self.assertEqual([], getPrimes(0)) self.assertEqual([2, 3, 5, 7], getPrimes(7)) self.assertRaises(Exception, getPrimes, divisors.biggestPrime + 1)