def getAnswer(self): primeLimit = 10000 primes = utility.getPrimes(primeLimit) i = 0 n = 1 numPrime = 0 numTotal = 1 while True: i += 1 for j in xrange(4): n = n + 2 * i if n >= primeLimit * primeLimit: primeLimit *= 2 primes = utility.getPrimes(primeLimit) if utility.isPrime(n, primes): numPrime += 1 numTotal += 1 if numTotal > numPrime * 10: return i * 2 + 1
def getAnswer(self): n = 2 while True: primes = [p for p in utility.getPrimes(pow(10, n))] for p in primes: if p < pow(10, n-1): continue for i in xrange(10): if str(p).count('%d' % (i)) == 0: continue count = 0 for j in xrange(10): if int(str(p)[0]) == i and j == 0: continue q = int(str(p).replace('%d' % (i), '%d' % (j))) if p == q or utility.isPrime(q, primes): count += 1 if count == 8: return p n += 1
def getAnswer(self): def isPermutations(p, q, r): p = str(p) q = str(q) r = str(r) for i in xrange(10): s = '%d' % i if p.count(s) != q.count(s) or q.count(s) != r.count(s): return False return True primes = utility.getPrimes(10000) for i in xrange(len(primes)): if primes[i] < 1000: continue for j in xrange(i + 1, len(primes)): p = primes[i] q = primes[j] r = q + q - p if r > 10000 or not utility.isPrime(r, primes): continue if not isPermutations(p, q, r): continue if p == 1487: continue return int(str(p) + str(q) + str(r)) return -1
def getAnswer(self): primes = utility.getPrimes(int(math.sqrt(987654321)) + 1) def isPrime(n): return utility.isPrime(n, primes) def enumerateCombinations(n, useEven): if len(n) == 1: yield n for i in xrange(len(n)): if not useEven and n[i] % 2 == 0: continue for c in enumerateCombinations(n[:i] + n[i+1:], True): yield c + [n[i]] def getPandigitalPrimes(nDigits): digits = [i+1 for i in xrange(nDigits)] for c in enumerateCombinations(digits, False): p = 0 for i in c: p *= 10 p += i if isPrime(p): yield p ret = 0 for i in xrange(9, -1, -1): for p in getPandigitalPrimes(i): if ret < p: ret = p if ret != 0: break return ret
def f(limit): primes = utility.getPrimes(limit) length = 1 ret = 0 for i in xrange(len(primes)): for j in xrange(i + length, len(primes) - 1): s = sum(primes[i:j+1]) if s >= limit: break if s < limit and utility.isPrime(s, primes): length = len(primes[i:j+1]) ret = s return ret
def getAnswer(self): def phi2(n): ret = 0 for i in xrange(1, n): if fractions.gcd(i, n) == 1: ret += 1 return ret def getFactors(n, primes): sqn = sqrt(n) for p in primes: if n % p == 0: yield p if p > sqn: break def phi(n, primes): l = [True for i in xrange(n)] #factors = utility.factorize(n, primes)[0] #for f in factors: for f in getFactors(n, primes): for i in xrange(n / f): l[f * i] = False ret = 0 for i in l: if i: ret +=1 return ret primes = utility.getPrimes(100001) assert phi(9, primes) == 6 assert phi(10, primes) == 4 #print 1.0 * phi(6, primes) / 6 #print 1.0 * phi(6*2, primes) / 6 / 2 #print 1.0 * phi(10, primes) / 10 #print 1.0 * phi(30, primes) / 30 #print 1.0 * phi(210, primes) / 210 #print 1.0 * phi(2310, primes) / 2310 #print 1.0 * phi(30030, primes) / 30030 #print 1.0 * phi(510510, primes) / 510510 ret = 1 for p in primes: if ret * p > 1000000: return ret ret *= p
def getAnswer(self): limit = 27000 primes = utility.getPrimes(limit) def isConcatenatedPrime(p, q, primes): return utility.isPrime(int(p + q), primes) and utility.isPrime(int(q + p), primes) graph = [[] for i in xrange(len(primes))] strPrimes = [str(p) for p in primes] for i in xrange(len(primes)): for j in xrange(i+1, len(primes)): if isConcatenatedPrime(strPrimes[i], strPrimes[j], primes): graph[i].append(j) graph[j].append(i) # we need a n-complete subgraph inside the graph def findCompleteGraphs(graph, n): if n == 3: for i in xrange(len(primes)): for j in graph[i]: if j < i: continue for k in graph[j]: if k < j: continue if k in graph[i]: yield [i, j, k] else: for g in findCompleteGraphs(graph, n-1): for k in graph[g[-1]]: if k < g[-1]: continue isComplete = True for gi in g: if gi not in graph[k]: isComplete = False break if isComplete: yield g + [k] ret = sys.maxint for c in findCompleteGraphs(graph, 5): ret = max(ret, sum(primes[i] for i in c)) assert primes[-1] > ret return ret
def getAnswer(self): primes = utility.getPrimes(100) def isPowerOf(n): for i in xrange(2, n): t = 1 j = 0 while t < n: t *= i j += 1 if t == n: assert j != 1 return [i, j] return [n, 1] def f(arange, brange): sets = [set() for a in arange] ret = 0 # it only happens when a is power of some number for i, a in enumerate(arange): f = isPowerOf(a) if f[1] == 1: for b in brange: sets[i].add(b) else: for b in brange: sets[arange.index(f[0])].add(b * f[1]) for s in sets: ret += len(s) return ret assert f(range(2, 6), range(2, 6)) == 15 return f(range(2, 101), range(2, 101))