Ejemplo n.º 1
0
def cyclic_numbers(N):
    '''A generator of numbers who are divisors of their right-rotation in [10,10**N).'''
    for n, a in ((n, a) for n in xrange(2, N + 1) for a in xrange(1, 10)):
        U, V = 10 ** (n - 1) - a, 10 * a - 1
        d = gcd(U, V)
        u, v = U / d, V / d
        c_min, c_max = int(ceil(max(1. / v, 10.**(n - 2) / u))), min(9 / v, (10 ** (n - 1) - 1) / u)
        for c in xrange(c_min, c_max + 1): yield c * (10 * u + v)
Ejemplo n.º 2
0
def num_triples_brute_force(N):
    count = 0
    for m in xrange(2, N):
        for n in (n for n in xrange(m % 2 + 1, m, 2) if gcd(m, n) == 1):
            A, B, C = m * m - n * n, 2 * m * n, m * m + n * n
            for k in takewhile(lambda k: (A + B + C) * k <= N, count(1)):
                a, b, c = k * A, k * B, k * C
                if c % (b - a) == 0:
                    count += 1
    return count
Ejemplo n.º 3
0
def num_frac(A, B, D):
    a, b = A + 1e-15, B - 1e-15
#     for d in xrange(2, D + 1):
#         high = it.dropwhile(lambda n: gcd(d, n) != 1 and n > 0, xrange(int(b * d), -1, -1)).next()
#         low = it.dropwhile(lambda n: gcd(d, n) != 1 and n < d, xrange(int(ceil(a * d)), d + 1)).next()
#         print 'd', d, '%.2f < %d/%d=%.2f to %d/%d=%.2f < %.2f, %d' % \
#         (a, low, d, float(low) / d, high, d, float(high) / d, b, max(high - low + 1, 0))
#         
    return sum(len(filter(lambda n: gcd(d, n) == 1, xrange(int(ceil(a * d)), int(b * d) + 1)))
               for d in xrange(2, D + 1))
Ejemplo n.º 4
0
def num_perfect(L):
    '''Enumerate primitive triples whose hypotenuse is a perfect square r^2 (i.e., r is the hypotenuse
    the primitive triplet (m,n,r), where m,n parametrize the original triples. Look for those that
    don''t yield an area divisible by 84.'''
    s, L2 = 0, L ** 0.5
    for N in (N for N in xrange(1, int((L2 / 2) ** 0.5) + 1) if N % 4 != 0):
        N2 = N * N
        for M in (M for M in xrange(N + 1, int(L2 - N2) + 1, 2) if M % 4 != 0 and gcd(M, N) == 1):
            m, n = M * M - N2, 2 * M * N
            if ((m * m - n * n) * m * n % 84 != 0): s += 1
    return s
Ejemplo n.º 5
0
def pairs2(N, add_both=True):
    pairs = list()
    for u in xrange(int(N ** 0.5) + 1):
        for v in xrange(1, u):
            if (gcd(u, v) != 1): continue;
            if ((u - v) % 3 == 0): continue;
            p = 2 * u * v + v * v
            r = u * u - v * v
            if p + r > N: break
            #print 'u', u, 'v', v
            for k in xrange(1, int(N / float(r + p)) + 1):
                #print '\t', 'k', k, (k * p, k * r)
                pairs.append((k * p, k * r))
                if add_both: pairs.append((k * r, k * p))
    return pairs
Ejemplo n.º 6
0
def pairs(N):
    '''Generate the list S of pairs (x,y) s.t. x**2+y**2+x*y is a perfect square and x+y <= N.'''
    S = []
    for u in xrange(1, int(N ** 0.5) + 1):
        u2 = u * u
        for v in (v for v in xrange(1, u) if (u - v) % 3 != 0 and gcd(u, v) == 1):
            v2 = v * v
            p, r = 2 * u * v + v2, u2 - v2
            pr = p + r
            if pr > N: break
            #print 'u', u, 'v', v
            for k in xrange(1, int(N / float(pr)) + 1):
                #print '\t', 'k', k, (k * p, k * r)
                S.append((k * p, k * r))
    return S
Ejemplo n.º 7
0
def num_triples(N):
    count, do = 0, {-1: True, 1: True}
    for y, x in islice(pell_solutions(2, -1), 1, None):
        cont = False
        for t in (t for t in [-1, 1] if do[t]):
            g = gcd(x + t, y - x)
            if ((y + t) / g) % 2:
                s = 2 * (x + t) * (y + t) / (g * g)
                if s <= N:
                    count += N / s
                    cont = True
                else:
                    do[t] = False
        if not cont:
            break
    return count
Ejemplo n.º 8
0
def sum_divisors_complex_formula(n):
    '''Return the sum of complex Gaussian divisors with positive real part of all k, k=1..n.
    Using a summation formula. Runtime complexity: O(n).'''
    s = 0L
    for a in xrange(1, int((n - 1) ** 0.5) + 1):
        a2, sa = a * a, 0L
        # print 'a', a, 'b_max', int((n - a2) ** 0.5)
        for b in (b for b in xrange(1, int((n - a2) ** 0.5) + 1) if gcd(a, b) == 1):
            N = n / (a2 + b * b)
            # print '\t', (a, b), 'N', N
            for g1 in xrange(1, N + 1):
                K = n / ((a2 + b * b) * g1)
                sa += (K * g1 + K * (K + 1) / 2)
                # print '\t\t', (a, b), 'g1', g1, 'K', K, 'term', a * (K * g1 + K * (K + 1) / 2)
        s += a * sa
    return s
Ejemplo n.º 9
0
def sum_divisors_complex(n):
    '''Return the sum of complex Gaussian divisors with positive real part of all k, k=1..n.'''
    d = np.zeros((n + 1,), dtype=long)

    # Complex factors a+bi with a > 0; use symmetry b/-b
    # print 'a_max', int((n - 1) ** 0.5)
    for a in xrange(1, int((n - 1) ** 0.5) + 1):
        a2 = a * a
        # print 'a', a, 'b_max', int((n - 1 - a2) ** 0.5)
        for b in (b for b in xrange(1, int((n - a2) ** 0.5) + 1) if gcd(a, b) == 1):
            p0 = a2 + b * b
            for g in xrange(1, n / p0 + 1):
                p = p0 * g
                d[p] += p
                d[2 * p::p] += 2 * p
                # print '\t', (a, b), 'p', p, 'd', d
    # print np.arange(1, n + 1)
    # print d[1:]
    return sum(d)
Ejemplo n.º 10
0
def D(N):
    k = int(N / e)
    k1 = k + 1
    k = k if f(N, k) > f(N, k + 1) else k1
    return (-N) if is_term(k / gcd(N, k)) else N
Ejemplo n.º 11
0
'''
============================================================
http://projecteuler.net/problem=130

A number consisting entirely of ones is called a repunit. We shall define R(k) to be a repunit of length k; for example, R(6) = 111111.

Given that n is a positive integer and GCD(n, 10) = 1, it can be shown that there always exists a value, k, for which R(k) is divisible by n, and let A(n) be the least such value of k; for example, A(7) = 6 and A(41) = 5.

You are given that for all primes, p  5, that p  1 is divisible by A(p). For example, when p = 41, A(41) = 5, and 40 is divisible by 5.

However, there are rare composite values for which this is also true; the first five examples being 91, 259, 451, 481, and 703.

Find the sum of the first twenty-five composite values of n for which
GCD(n, 10) = 1 and n  1 is divisible by A(n).
============================================================
'''
import itertools as it
from problem129 import A
from problem005 import gcd
from euler.problem035 import is_prime

if __name__ == "__main__":
    print sum(n for n, _ in it.islice(it.ifilter(lambda (n, a): (n - 1) % a == 0, ((n, A(n)) for n in it.count(1) if gcd(n, 10) == 1 and not is_prime(n))), 25))
Ejemplo n.º 12
0
def normalize(x, y):
    '''Reduce the fraction x/y to its normal, irreducible form.'''
    g = gcd(x, y)
    return x / g, y / g
Ejemplo n.º 13
0
# Complex divisors
# -------------------------------
def g_sum(n, p):
    """Return the inner-most sum of complex Gaussian divisors with positive real part of all k, k=1..n.
    Using a summation formula. Runtime complexity: O(n^(1/2))."""
    # Optimal split to balance local and smoth work
    q, s = int((n / p) ** 0.5), 0L
    # Local part
    for g in xrange(1L, n / (p * q) + 1):
        K = n / (p * g)
        s += K * g + K * (K + 1) / 2
    # Smooth part
    for k in xrange(1L, q):
        r1, r2 = n / (p * (k + 1)), n / (p * k)
        G = (r1 + r2 + 1) * (r2 - r1) / 2
        s += k * G + k * (k + 1) * (r2 - r1) / 2
    return s


"""Return the sum of complex Gaussian divisors with positive real part of all k, k=1..n.
Using a summation formula. Runtime complexity: O(n)."""
sum_divisors_complex = lambda n: sum(
    a * sum(g_sum(n, a * a + b * b) for b in xrange(1, int((n - a * a) ** 0.5) + 1) if gcd(a, b) == 1)
    for a in xrange(1, int((n - 1) ** 0.5) + 1)
)

sum_divisors_gaussian = lambda n: sum_divisors_rational(n) + sum_divisors_complex(n)

if __name__ == "__main__":
    print sum_divisors_gaussian(10L ** 8)
Ejemplo n.º 14
0
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for d  8 in ascending order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that 2/5 is the fraction immediately to the left of 3/7.

By listing the set of reduced proper fractions for d  1,000,000 in ascending order of size, find the numerator of the fraction immediately to the left of 3/7.
============================================================
'''
from problem005 import gcd

def max_frac((nt, dt), D):  # D>=3
    rt, (n_max, d_max), r_max = float(nt) / dt, (0, 1), 0.
    for d in xrange(4, D):
        if d == dt: continue
        m = d * r_max
        for n in xrange(int(rt * d), 0, -1):
            if n < m: break
            if gcd(d, n) == 1:
                n_max, d_max, r_max = n, d, float(n) / d
                break
    return n_max, d_max
                
if __name__ == "__main__":
#    import doctest
#    doctest.testmod()
    print max_frac((3, 7), 1000000)
Ejemplo n.º 15
0
def min_a_with_A_ge(N):
    '''Return the minimum n for which A(N)>=N.'''
    logN = log(N) / log(3)
    if logN < int(logN) + 1e-12: return N  # N is a power of 3
    p_low = 3 ** (int(logN))
    p_high = 3 * p_low
    p = primes('lt', p_high)
    p = p[p > p_low]
    
    lim = lambda P: it.dropwhile(lambda (n, a): a != n - 1, ((n, A(n)) for n in P)).next()[0]
    # Find largest prime 3^[log3(N)] < p_min <= N such that A(n)=n-1
    try: p_min = lim(reversed(p[p <= N]))
    except StopIteration: p_min = p_high
    # Find smallest prime N <= p_max < 3^([log3(N)]+1) such that A(n)=n-1
    try: p_max = lim(p[p >= N])
    except StopIteration: p_max = p_high
    
    # Search between p_min and p_max 
    return it.dropwhile(lambda (_, a): a < N, ((n, A(n)) for n in xrange(p_min, p_max + 1) if gcd(n, 10) == 1)).next()[0]
Ejemplo n.º 16
0
def sum_divisors_complex_formula_fast(n):
    s = 0L
    print 'a_max', int((n - 1) ** 0.5)
    for a in xrange(1, int((n - 1) ** 0.5) + 1):
        a2 = a * a
        if (a % 100) == 0: print 'a', a, 'b_max', int((n - a2) ** 0.5)
        s += a * sum(g_sum(n, a * a + b * b) for b in xrange(1, int((n - a * a) ** 0.5) + 1) if gcd(a, b) == 1)
    return s