def pr214(): def phi(n): if isPrimeMR(n): return n - 1 ret = n for x in primes: if x << 1 > n: break if n % x == 0: ret = ret // x * (x - 1) return ret def getChainLen(n): if d[n]: return d[n] t = getChainLen(phi(n)) + 1 d[n] = t return t primes = getPrimes(40000000) d = [0] * 40000000 d[0] = 1 d[1] = 1 ret = 0 pbar = PBar(40000000).start() for x in primes: pbar.update(x) if getChainLen(x) == 25: ret += x pbar.finish() return ret
def pr071(): base = Fraction(3, 7) ret = Fraction(0, 1) pbar = PBar(int(1e6)+1).start() for d in xrange(3, int(1e6) + 1): pbar.update(d) n = int(3.0/7*d) t1 = Fraction(n, d) if (t1 < base) and (t1 > ret): ret = t1 pbar.finish() return ret.numerator
def pr357(n): ret = 3 primes = set(getPrimes(2*n)) pbar = PBar(n).start() for x in xrange(3, n + 1): if not x in primes: pbar.update(x) flag = True for y in xrange(1, int(sqrt(x))+1): if (x % y == 0): if not (y + x / y) in primes: flag = False break if flag: ret += x pbar.finish() return ret
def pr336(n, s): maxA = [] maxN = 0 cnt = 0 pbar = PBar(reduce(lambda x, y: x * y, range(1, n + 1))).start() for p in permutations(range(n)): cnt += 1 pbar.update(cnt) t = rearrange(list(p[:])) if t > maxN: maxA = [''.join(map(lambda x: chr(ord('A')+x), p))] maxN = t elif t == maxN: maxA += [''.join(map(lambda x: chr(ord('A')+x), p))] pbar.finish() maxA.sort() return maxA[s-1]