def compute(): # Consider an arbitrary fraction n/d: # Let n = 10 * n1 + n0 be the numerator. # Let d = 10 * d1 + d0 be the denominator. # As stated in the problem, we need 10 <= n < d < 100. # We must disregard trivial simplifications where n0 = d0 = 0. # # Now, a simplification with n0 = d0 is impossible because: # n1 / d1 = n / d = (10*n1 + n0) / (10*d1 + n0). # n1 * (10*d1 + n0) = d1 * (10*n1 + n0). # 10*n1*d1 + n1*n0 = 10*d1*n1 + d1*n0. # n1*n0 = d1*n0. # n1 = d1. # This implies n = d, which contradicts the fact that n < d. # Similarly, we cannot have a simplification with n1 = d1 for the same reason. # # Therefore we only need to consider the cases where n0 = d1 or n1 = d0. # In the first case, check that n1/d0 = n/d; # in the second case, check that n0/d1 = n/d. numer = 1 denom = 1 for d in range(10, 100): for n in range(10, d): n0 = n % 10 n1 = n // 10 d0 = d % 10 d1 = d // 10 if (n1 == d0 and n0 * d == n * d1) or (n0 == d1 and n1 * d == n * d0): numer *= n denom *= d return str(denom // eulerlib.gcd(numer, denom))
def is_prime(n, k): """ Test if a number n is prime k-times. :param n: The prime number to be tested. :param k: The number of tests. :return: """ if n <= 1 or n == 4: return False if n <= 3: return True if eulerlib.is_even(n): return False while k > 0: # Take random int in [2, n-2] a = random.randint(2, n-1) # Check if a and n are co-prime. if eulerlib.gcd(n, a) != 1: return False # Fermat's little theorem if modpow(a, n-1, n) != 1: return False k -= 1 return True
def digit_cancelling_fractions(): lowest_nums = [] lowest_denoms = [] for den in range(10, 100): if den % 10 == 0: continue for num in range(10, den): num_digits = eulerlib.digits(num) den_digits = eulerlib.digits(den) if num % 10 == 0: continue digits_intersection = list(set(num_digits) & set(den_digits)) if len(digits_intersection) > 0: num_digits.remove(digits_intersection[0]) den_digits.remove(digits_intersection[0]) num_lct = eulerlib.digits_to_int(num_digits) den_lct = eulerlib.digits_to_int(den_digits) cancelled_fraction = num_lct / den_lct fraction = num / den if fraction == cancelled_fraction: lowest_nums.append(num_lct) lowest_denoms.append(den_lct) print('{} / {} = {} / {}'.format(num, den, num_lct, den_lct)) num_prod = eulerlib.product(lowest_nums) den_prod = eulerlib.product(lowest_denoms) gcd = eulerlib.gcd(num_prod, den_prod) return den_prod / gcd
def count_all_unconcealed(prime): result = [] for e in range(prime - 1): if eulerlib.gcd(e, prime - 1) == 1: result.append(count_unconcealed(prime, e)) else: result.append(10**20) # Sentinel return result
def solve(): qtys = [0] * (bound+1) for m in xrange(2, int(ceil((sqrt(1+2*bound) - 1)/2))+1): for n in xrange(2 - (m+1)%2, m, 2): if gcd(m,n) != 1: continue perim = 2*m*(m+n) if perim > bound: break for x in xrange(perim, bound+1, perim): qtys[x] += 1 return sum(1 for q in qtys if q == 1)
def solve(): for a in range(1, 10): for b in range(1, 10): ab = a*10 + b for c in range(1, 10): checkFrac(ab,b,c, a,c) # ab/bc = a/c checkFrac(ab,c,b, a,c) # ab/cb = a/c checkFrac(ab,a,c, b,c) # ab/ac = b/c checkFrac(ab,c,a, b,c) # ab/ca = b/c (nums, denoms) = zip(*fractions) num = product(nums) denom = product(denoms) return denom // gcd(num, denom)
def find(n: int): p = 1 q = 1 for a in range(1, n): for b in range(1, n): for c in range(1, n): if f(a, b, c): p *= (10 * a + b) q *= (10 * b + c) elif g(a, b, c): p *= (10 * a + b) q *= (10 * c + b) r = lib.gcd(p, q) p //= r q //= r return (p, q)
def getPnum(edgeMAX): res = {} m = 2 flag = True while flag: for n in range(m - 1, 0, -2): if m * m + n * n <= edgeMAX: if gcd(m, n) == 1: if m * m + n * n in res: res[m * m + n * n].append((2 * m * n, m * m - n * n)) else: res[m * m + n * n] = [(2 * m * n, m * m - n * n)] else: flag = False break m += 1 return res
def getPnumAll(edgeMAX): res = {} m = 2 flag = True while flag: for n in range(m - 1, 0, -2): if m * m + n * n <= edgeMAX: if gcd(m, n) == 1: t = edgeMAX // (m * m + n * n) for i in range(1, t + 1): if (m * m + n * n) * i in res: res[(m * m + n * n) * i].append( (2 * m * n * i, (m * m - n * n) * i)) else: res[(m * m + n * n) * i] = [(2 * m * n * i, (m * m - n * n) * i)] else: flag = False break m += 1 return res
def simplify(self): c = eulerlib.gcd(self.num, self.den) return Fraction(self.num // c, self.den // c)
def compute(): ans = 1 for i in range(1, 21): ans *= i // eulerlib.gcd(i, ans) return str(ans)
from eulerlib import cmpf, gcd N = 12000 f1 = (1,3) f2 = (1,2) c = 0 for d in xrange(1,N+1): for n in xrange(d/3,d/2+1): if gcd(n,d) == 1: f = (n,d) if cmpf(f,f1) > 0 and cmpf(f,f2) < 0: c += 1 print c
def totient(n): result = 0 for k in xrange(1, n + 1): if gcd(k, n) == 1: result += 1 return result