def main(): fraction_pairs = [] fractions = [] for d in range(1, 10): for n in range(1, d): n_over_d = n / d if n_over_d >= 1: continue for cancelled in range(1, 10): numerator = join_digits(cancelled, n) denominator = join_digits(cancelled, d) if numerator / denominator == n_over_d: fraction_pairs.append([[numerator, denominator], [n, d]]) fractions.append([numerator, denominator]) numerator = join_digits(cancelled, n) denominator = join_digits(d, cancelled) if numerator / denominator == n_over_d: fraction_pairs.append([[numerator, denominator], [n, d]]) fractions.append([numerator, denominator]) numerator = join_digits(n, cancelled) denominator = join_digits(cancelled, d) if numerator / denominator == n_over_d: fraction_pairs.append([[numerator, denominator], [n, d]]) fractions.append([numerator, denominator]) numerator = join_digits(n, cancelled) denominator = join_digits(d, cancelled) if numerator / denominator == n_over_d: fraction_pairs.append([[numerator, denominator], [n, d]]) fractions.append([numerator, denominator]) result_fraction = [1, 1] for fraction in fractions: result_fraction[0] *= fraction[0] result_fraction[1] *= fraction[1] result_gcd = gcd(result_fraction[0], result_fraction[1]) print(result_fraction[1] / result_gcd)
def calculate_inner_points(self): new_inner_points = 0 # if this is the origin return 0 if self.parent is None: return 0 # if this is the first non-origin point on the polygon, then we have no 3d shape yet # the inner points are just the ones colinear to this first edge if self.parent.x == 0 and self.parent.y == 0: # gcd is a sneaky way to do this - think about it return gcd(abs(self.x), self.y) # y is already positive # any points on y = 0 have already been dealt with in the trivial cases for _y in xrange(1, N + 1): min_x = int(math.ceil(float(_y) / self.y * self.x)) if self.y != 0 else -N max_x = int(math.floor(float(_y) / self.parent.y * self.parent.x)) if self.parent.y != 0 else N + 1 for _x in xrange(min_x, max_x): if _y > float(self.y - self.parent.y) / (self.x - self.parent.x) * (_x - self.x) + self.y: # exclude points in the wedge but outside the triangle continue if _x == self.x and _y == self.y: # exclude the new vertex continue new_inner_points = new_inner_points + 1 # add in parent points return new_inner_points + self.parent.get_inner_points()
def euler_33(): nums = range(10, 100) dens = range(10, 100) fracs = ((n, d) for n, d in itertools.product(nums, dens)) filtered_fracs = list(filter(cancel_filter, fracs)) num = functools.reduce(operator.mul, [x[0] for x in filtered_fracs], 1) den = functools.reduce(operator.mul, [x[1] for x in filtered_fracs], 1) divisor = lib.gcd(num, den) return den // divisor
def D(N): # d/dk((N/k)^k) = 0 -> k=N/2 k = int(N / math.e + 0.5) # Check if N/k is a terminating decimal # Reduce by gcd k //= gcd(N, k) # Reduce by terminating decimal divisors while k % 2 == 0: k //= 2 while k % 5 == 0: k //= 5 return N if k > 1 else -N
def main(limit): rv = 0 for n in count(3): if gcd(n, 10) != 1: continue if primes.isPrime(n): continue if (n - 1) % A(n) != 0: continue rv += n limit -= 1 if limit == 0: return rv
import itertools from lib import gcd def is_equal_fraction(n1, d1, n2, d2): return n1 * d2 == n2 * d1 def rev(l): return list(reversed(l)) def is_curious(n, d): ns = map(int, str(n)) ds = map(int, str(d)) return ns[1] == ds[0] and is_equal_fraction(n, d, ns[0], ds[1]) total_n = 1 total_d = 1 for n in range(10, 100): for d in range(n+1, 100): if is_curious(n, d): total_n *= n total_d *= d factor = gcd(total_n, total_d) print "%d/%d" % (total_n/factor, total_d/factor)
from lib import gcd if __name__ == '__main__': product = 1 for i in xrange(2, 21): product = (product * i) / gcd(product, i) print product
#!/usr/bin/env python3 from lib import gcd Lmax = 1500000 seen = {} for m in range(1,int((Lmax)**.5)): for n in range(1,m): if (m+n)%2 == 1 and gcd(m,n) == 1: a = m**2 - n**2 b = 2*m*n c = m**2 + n**2 s = a+b+c for k in range(s, Lmax+1, s): if k not in seen: seen[k] = set([]) seen[k].add(",".join(map(str,sorted([k/s*a,k/s*b,k/s*c])))) #print(seen) count = 0 for key in seen: if len(seen[key]) == 1: count += 1 print(count)
def reduce_frac((n, d)): g = gcd(n, d) return n / g, d / g
def D(N): k = int(N / math.e + 0.5) k //= gcd(N, k) while k % 2 == 0: k //= 2 while k % 5 == 0: k //= 5 return N if k > 1 else -N
#!/usr/bin/env python3 import lib, math from collections import Counter N = 12_000 prime = lib.prime_sieve(N + 1) count = 0 for d in range(2, N + 1): for n in range(int(d / 3), int(d / 2) + 1): if 2 * n >= d: #n/d >= 1/2 break if 3 * n > d: #n/d > 1/3 if prime[d]: count += 1 elif lib.gcd(n, d) == 1: count += 1 if d % 100 == 0: print('d = ' + str(d), count) print(count)
#!/usr/bin/env python3 import lib from collections import defaultdict L=1_500_000 def odd(n): return n % 2 == 1 triangles = defaultdict(set) for n in range(1, int((L//2)**0.5)): n2 = n**2 for m in range(n+1, int((L//2)**0.5)): # n must be less than m if not(odd(m) and odd(n)) == 1 and lib.gcd(m,n) == 1: # conditions for a primitive pythagorean triple m2 = m**2 k = 1 # Euclid's formula for pythagorean triples a,b,c = m2 - n2, 2*m*n, m2 + n2 l = a+b+c if l > L: break while a+b+c <= L: l = [a,b,c] l.sort() triangles[a+b+c].add(tuple(l)) k += 1 a,b,c = (m2 - n2)*k, 2*m*n*k, (m2 + n2)*k count = 0 for k,v in triangles.items():