コード例 #1
0
ファイル: problem127.py プロジェクト: pombredanne/euler
def abc_hit(N):
    """Yield all abc-hits with c < N."""
    r = rad(N)
    for a in xrange(1, (N - 1) / 2 + 1):
        if a % 100 == 0:
            print "a", a
        for b in xrange(a + 1, N - a):
            c = a + b
            rab = r[a] * r[b]
            if rab < c and rab * r[c] < c and gcd(a, b) == 1:
                yield a, b, c
コード例 #2
0
ファイル: problem075.py プロジェクト: orenlivne/euler
def is_unique_perimeter(s):
    if s % 100000 == 0: print s
    s2, count = s / 2, 0
    for m in ifilter(lambda m: s2 % m == 0, xrange(2, int(ceil(s2 ** 0.5)))):
        sm = s2 / m  # s/(2m), need to find odd factor k of
        while sm % 2 == 0: sm /= 2  # Reduce search space of odd factors kby removing all 2-factors
        for k in xrange(m + 1 + (m % 2), min(2 * m, sm + 1), 2):
            if sm % k == 0 and gcd(k, m) == 1:
                d, n = s2 / (k * m), k - m
                m2, n2 = m * m, n * n
                a, b = d * (m2 - n2), 2 * d * m * n
                if a > b: a, b = b, a
                count += 1
                if count == 1: a1, b1 = a, b
                elif count == 2 and a == a1 and b == b1: return False
    return count == 1
コード例 #3
0
ファイル: problem091.py プロジェクト: pombredanne/euler
#                             count += 1
#     print 'f[%d,%d,%d] = %d' % (m, n, i, count)
#     return count
#
# g = lambda k, S: sum(f(k, k - 1, i, S) for i in xrange(k)) + sum(f(k, k, i, S) for i in xrange(k + 1))
#
# def num_triangles(N):
#     S, G = squares_dict(N), 0
#     for n in xrange(1, N + 1):
#         gn = g(n, S)
#         G += gn
#         print 'n', n, 'g[n]', gn, 'S[n]', n * n + G
#         yield n * n + G
#
# if __name__ == "__main__":
#     N = 2
#     print [(a * a + b * b, (a, b)) for a, b in it.product(xrange(2 * N + 1), xrange(2 * N + 1))]
#     S = squares_dict(N)
#     print f(2, 2, 2, S)
#     print list(num_triangles(2))  # [-1]

from euler.problem005 import gcd

num_triangles = lambda n: 3 * n * n + 2 * sum(
    min(y * f / x, (n - x) * f / y)
    for x, y, f in ((x, y, gcd(x, y)) for x in xrange(1, n + 1) for y in xrange(1, n + 1))
)

if __name__ == "__main__":
    print num_triangles(50)
コード例 #4
0
ファイル: problem141.py プロジェクト: orenlivne/euler
def prog_sq2(N):
    '''Return the set of progressive perfect squares <= N.'''
    result = set([])
    for a in xrange(2, int((N - 1) ** (1. / 3.)) + 1):
        a3 = a ** 3
        for b in (b for b in xrange(1, min(a, int(0.5 * ((a3 * a3 + 4 * N) ** 0.5 - a3)) + 1)) if gcd(a, b) == 1):
            ab, b2 = a3 * b, b * b
            for k in xrange(1, int((b ** 4 + 4 * N * ab) ** 0.5 - b2) / (2 * ab) + 1):
                n = k * (k * ab + b2)
                if int(n ** 0.5) ** 2 == n:
                    print 'n', '=', int(n ** 0.5), '^ 2'
                    result.add(n)
            
    return result
コード例 #5
0
ファイル: problem433.py プロジェクト: orenlivne/euler
T = lambda n: sum(gcd_steps(n, m) for m in xrange(n))

tau_restricted = lambda n, r: sum(k for d, k in (gcd_and_steps(m, n) for m in xrange(r)) if d == 1)  # @UnusedVariable
tau = lambda n: tau_restricted(n, n)


if __name__ == "__main__":
    N = 100
    print S(N), 2 * sum(T(n) for n in xrange(1, N + 1)) + N * (N + 1) / 2

    print gcd_steps(47, 13)
    print gcd_steps(8, 13)
    
    d = 24
    p = 3
    print tau(p * d), sum(gcd_steps(i + k * d, p * d) for i in xrange(d) for k in xrange(p) if gcd(i, d) == 1)
    
    for i in (i for i in xrange(d) if gcd(d, i) == 1):
        print 'i %3d' % i,
        for k in xrange(p):
            print (i + k * d, p * d), gcd_steps(i + k * d, p * d),
#            if k == 0:
#                print (i, p * d),
#            elif k == (p - 1) / 2:
#                if i > d / 2:
#                    print (d * p - i - k * d, p * d), 1,
#                else:
#                    print (i + k * d, p * d),
#            elif k > p / 2:
#                print (d * p - i - k * d, p * d), 1,
#            else: