def search_down(): global prod global prod_row global prod_col global prod_direction global prod_lst for j in range(20): for i in range(16): lst = [mtrx[i + x][j] for x in range(4)] m = mul(lst) if m > prod: prod = m prod_row = i prod_col = j prod_direction = "down" prod_lst = lst
def search_up_right(): global prod global prod_row global prod_col global prod_direction global prod_lst for i in range(16): for j in range(3, 20): lst = [mtrx[i + x][j - x] for x in range(4)] m = mul(lst) if m > prod: prod = m prod_row = i prod_col = j prod_direction = "up-right" prod_lst = lst
return d_totient[n] except KeyError: ret = n if is_prime(n): ret = n - 1 else: fact = prime_factors(n) for p in fact: ret = ret*(p - 1)/p k = 1 while True: d_totient[n**k] = (n**(k - 1))*ret k += 1 if n**k >= 10**6: break return d_totient[n] if __name__ == '__main__': # read the wiki: http://en.wikipedia.org/wiki/Euler%27s_totient_function # in particular the chapter 'Growth of the function' lst_primes = [x for x in range(2, 50) if is_prime(x)] i = 0 while True: n = mul(lst_primes[:i]) if n > 10**6: n = mul(lst_primes[:(i - 1)]) break i += 1 print n
d_gcd[(n, r)] = gcd(r, n - r*(n/r)) return d_gcd[(n, r)] def simplify((a, b)): d = gcd(a, b) return (a/d, b/d) def is_curious((a, b)): possibilities = [(hdc(a), hdc(b)), (hdc(a), ldc(b)), (ldc(a), hdc(b)), (ldc(a), ldc(b))] equalities = [(ldc(a), ldc(b)), (ldc(a), hdc(b)), (hdc(a), ldc(b)), (hdc(a), hdc(b))] for i, frac in enumerate(possibilities): if equalities[i][0] == equalities[i][1] and fractions_equal((a, b), frac): print "%d/%d = %d/%d" % (a, b, frac[0], frac[1]) return True return False def find_fractions(): d = {} for a in range(10, 100): for b in range(a + 1, 100): if is_curious((a, b)): try: tmp = d[simplify((a, b))] except KeyError: d[simplify((a, b))] = 1 return d if __name__ == '__main__': f = find_fractions() print simplify((mul([x for x, y in f.keys()]), mul([y for x, y in f.keys()])))