Beispiel #1
0
 def test_gcd(self):
     # Small integers
     self.assertEqual(common.gcd(10, 45), 5)
     # Medium integers
     self.assertEqual(common.gcd(172383, 209399), 1)
     # Large integers
     self.assertEqual(common.gcd(12938183838281, 91827172737173), 1)
Beispiel #2
0
def solution():
    limit = 10**8
    found = 0

    # Uses Euclid's formula for generating primitive pythagorean triples
    for m in range(2, int((limit/2)**0.5)):
        # Ensure that m and n are of opposite parity
        start = 2 if m % 2 else 1

        for n in range(start, m, 2):
            if gcd(m, n) == 1:
                a, b, c = right_triangle(m, n)

                # Ensure that the tiling would be possible (`c` is the longest
                # side -- i.e., the hypotenuse)
                if c % abs(a - b) != 0:
                    continue

                perimeter = a + b + c

                for k in count(1):
                    if k*perimeter > limit:
                        break

                    found += 1

    return found
Beispiel #3
0
def main():
	MAX = 50
	ans = MAX * MAX * 3
	for x in xrange(1, MAX + 1):
		for y in xrange(1, MAX + 1):
			g = gcd(x, y)
			ans += 2 * min((MAX - x) * g / y, y * g / x)
	print ans
Beispiel #4
0
def run():
    final_denom = 1
    final_numer = 1
    for d in xrange(12, 100):
        if str(d)[1] == "0":
            continue
        for n in xrange(11, d):
            if any(int(nc)*d==int(dc)*n and int(no)==int(do) for nc, no in itertools.permutations(str(n)) for dc, do in itertools.permutations(str(d))):
                final_denom *= d
                final_numer *= n
    return final_denom / gcd(final_denom, final_numer)
Beispiel #5
0
def solution():
    limit = 12000

    num_fracs = 0

    for n in range(4, limit+1):
        for i in range(int(n/3) + 1, int(n/2) + 1):
            if gcd(n, i) == 1:
                num_fracs += 1

    return num_fracs
Beispiel #6
0
    def __init__(self,
                 seed,
                 multiplier=DEFAULT_MULTIPLIER,
                 modulus=DEFAULT_MODULUS):
        super(BrvRandom, self).__init__(seed)
        check_integer(multiplier, 'multiplier should be integer value')
        check_integer(modulus, 'modulus should be integer value')
        if gcd(multiplier, modulus) != 1:
            raise ValueError('multiplier and modulus must be relatively prime')

        self._multiplier = multiplier
        self._modulus = modulus
Beispiel #7
0

def isCuriousFact(a, b):
    sa, sb = str(a), str(b)
    if a == b or '0' in sa + sb:
        return False
    elif len(Set(sa + sb)) != 3:
        return False
    elif sa[0] == sa[1] or sb[0] == sb[1]:
        return False
    else:
        cancelNum = sa[0] if sa[0] in sb else sa[1]
        newSa = sa.replace(cancelNum, "")
        newSb = sb.replace(cancelNum, "")
        if a * int(newSb) != b * int(newSa):
            return False
        return True


if __name__ == "__main__":
    c = []
    for a in range(10, 51):
        for b in range(51, 100):
            if isCuriousFact(a, b):
                c.append((a, b))

    prod = lambda x, y: x * y
    numerator = reduce(prod, [item[0] for item in c])
    denominator = reduce(prod, [item[1] for item in c])
    print denominator / gcd(numerator, denominator)
Beispiel #8
0
def terminating_decimal2(num, denom):
    if denom == 0: return
    denom //= gcd(num, denom)
    while denom % 2 == 0: denom //= 2
    while denom % 5 == 0: denom //= 5
    return denom == 1
Beispiel #9
0
'''

import common

def cancels(n1, d1, n2, d2):
  left = common.fraction(n1, d1) 
  right = common.fraction(n2, d2)
  return left == right and left is not None

common.assertEquals(True, cancels(49,98, 4,8))

n = 1
d = 1
for a in range(10):
  for b in range(10):
    for c in range(1,10):
      if cancels(10*a + c, b + 10*c, a, b):
        print 'found a curious fraction: %d%d/%d%d = %d/%d' %(a,c,c,b,a,b)
        n *= a
        d *= b
          
print 'product of curious fractions: %d/%d' % (n,d)

gcd = common.gcd(n,d)
n /= gcd
d /= gcd

print 'reduced product: %d/%d' % (n,d)

common.submit(d, expected=100)
Beispiel #10
0
def run(n):
    res = 1
    for i in range(1, n):
        res *= (i // common.gcd(res, i))
    return res
Beispiel #11
0
def lcm(a, b):
    """Returns the least common multiple of `a` and `b`"""
    return a*b / gcd(a, b)
Beispiel #12
0
from common import gcd

count = 0
for x in range(1, 10000):
    for y in range(1, x):
        if (x - y) % 2 != 0 and gcd(x, y) == 1:
            t, n, m = x * x + y * y, x * x - y * y, 2 * x * y
            if m >= n and (m - n) % 2 != 0 and gcd(m, n) == 1:
                a, b, c = m * m - n * n, 2 * m * n, m * m + n * n
                if (a * b) % 2 == 0:
                    s = a * b // 2
                    if not (s % 6 == 0 and s % 28 == 0):
                        count += 1
print(count)

  
Beispiel #13
0

def cancels(n1, d1, n2, d2):
    left = common.fraction(n1, d1)
    right = common.fraction(n2, d2)
    return left == right and left is not None


common.assertEquals(True, cancels(49, 98, 4, 8))

n = 1
d = 1
for a in range(10):
    for b in range(10):
        for c in range(1, 10):
            if cancels(10 * a + c, b + 10 * c, a, b):
                print 'found a curious fraction: %d%d/%d%d = %d/%d' % (a, c, c,
                                                                       b, a, b)
                n *= a
                d *= b

print 'product of curious fractions: %d/%d' % (n, d)

gcd = common.gcd(n, d)
n /= gcd
d /= gcd

print 'reduced product: %d/%d' % (n, d)

common.submit(d, expected=100)
Beispiel #14
0
from common import crange, Watch, gcd

Watch.start()
lim, num, denom, cond = 100, 1, 1, lambda x: x % 10 != 0
for a in crange(11, lim - 1, cond):
    for b in crange(a + 1, lim, cond):
        f_a, s_a, f_b, s_b = a // 10, a % 10, b // 10, b % 10
        if s_a == f_b and f_a * b == s_b * a:
            num, denom = num * a, denom * b
print(denom // gcd(num, denom))
Watch.stop()