当前位置: 首页>>代码示例>>Python>>正文


Python functions.sieve函数代码示例

本文整理汇总了Python中python.functions.sieve函数的典型用法代码示例。如果您正苦于以下问题:Python sieve函数的具体用法?Python sieve怎么用?Python sieve使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了sieve函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

def main(verbose=False):
    prime_max = 10 ** 5
    PRIMES = sieve(prime_max)
    found = []
    n = 2
    while len(found) < 25:
        n += 1
        if n > prime_max:
            prime_max *= 10
            PRIMES = sieve(prime_max)

        if n % 2 == 0 or n % 5 == 0 or n in PRIMES:
            continue

        basis = n
        if n % 3 == 0:
            basis = 9 * n

        if (n - 1) % order_mod_n(10, basis) == 0:
            found.append(n)
    if verbose:
        return ('%s.\nAs a check, the first five values are calculated to be '
                '%s, as stated.' % (sum(found),
                                    ', '.join(str(num) for num in found[:5])))
    else:
        return sum(found)
开发者ID:dhermes,项目名称:project-euler,代码行数:26,代码来源:no130.py

示例2: main

def main(verbose=False):
    PRIMES = sieve(10 ** 5)
    running_sum = 0
    for prime in PRIMES:
        if not prime_divides_repunit_power10(prime):
            running_sum += prime
    return running_sum
开发者ID:dhermes,项目名称:project-euler,代码行数:7,代码来源:no133.py

示例3: main

def main(verbose=False):
    prime_factors_hash = {}

    MINIMUM_SOLUTIONS = 4*(10**6)
    # P^k < 10**7 (10 mil)
    powers = [power_up_to_digits(prime, 7)
              for prime in [3, 5, 7]]
    products = [reduce(operator.mul, triple) for
                triple in list(i_product(*powers))]
    products = [product for product in sorted(products)
                if product > 2*MINIMUM_SOLUTIONS][:20]

    PRIMES = sieve(100)

    max_prod = 10**21
    res = []
    for product in products:
        factors = prime_factors(product, unique=False, hash_=prime_factors_hash)
        factors = [(factor - 1)/2 for factor in factors][::-1]
        curr_prod = 1
        for i, exp in enumerate(factors):
            curr_prod = curr_prod*(PRIMES[i]**exp)

        if curr_prod < max_prod:
            max_prod = curr_prod

    return max_prod
开发者ID:mireq,项目名称:project-euler,代码行数:27,代码来源:no110.py

示例4: main

def main(verbose=False):
    # layer/primes
    #     2/3
    #  1581/835
    #  3536/1677
    #  5000/2249
    # 13121/5248, winning layer

    # ratio >= .1 iff 10*(primes/total) >= 1 iff 10*primes >= total
    # iff 10*primes >= 4*index - 3
    FAILURE_POINT = 10 ** 9
    PRIMES = sieve(int(sqrt(FAILURE_POINT)) + 1)

    layer = 2
    num_primes = 3
    while 10 * num_primes >= 4 * layer - 3:
        layer += 1
        candidates = [(2 * layer - 1) ** 2 - 2 * (layer - 1) * i
                      for i in range(1, 4)]
        if candidates[-1] >= FAILURE_POINT:
            raise ValueError("Sieve was not big enough, restart function")
        for candidate in candidates:
            if is_prime(candidate, primes=PRIMES, failure_point=FAILURE_POINT):
                num_primes += 1
    side_length = 2 * layer - 1  # 2*(layer - 1) + 1
    return side_length
开发者ID:dhermes,项目名称:project-euler,代码行数:26,代码来源:no058.py

示例5: main

def main(verbose=False):
    PRIMES = sieve(10 ** 7)
    running_sum = 0
    for prime in PRIMES:
        if prime not in [2, 5]:
            running_sum += inverse_mod_n(10, prime)
    return running_sum
开发者ID:dhermes,项目名称:project-euler,代码行数:7,代码来源:no274.py

示例6: main

def main(verbose=False):
    primes = sieve(100)

    MAX_n = 10 ** 16

    product_factor_pairs = [(1, 0)]
    product_hash = {0: [1]}
    for num_factors in range(1, 13 + 1):
        product_hash[num_factors] = []

    # (1,0) becomes
    # (1,0), (p, 1) becomes
    # (1,0), (p, 1), (q,1), (q*p, 2) etc.
    for prime in primes:
        to_add = []
        for product, num_factors in product_factor_pairs:
            if prime * product < MAX_n:
                to_add.append((prime * product, num_factors + 1))
                product_hash[num_factors + 1].append(prime * product)
        product_factor_pairs += to_add

    result = 0
    sign = -1
    for num_factors in range(4, 13 + 1):
        sign = -sign
        PIE_factor = sign * choose(num_factors - 1, 3)
        current_sum = 0
        for product in product_hash[num_factors]:
            current_sum += MAX_n / product  # integer division
        result += PIE_factor * current_sum

    return result
开发者ID:dhermes,项目名称:project-euler,代码行数:32,代码来源:no268.py

示例7: all_circular

def all_circular(n):
    # the number of digits limits the size of all permutations
    digs = len(str(n))
    possible_primes = [2, 5] + \
        [prime for prime in sieve(10**digs - 1)
         if contains_only_digits(prime, [1, 3, 7, 9])]
    return [prime for prime in possible_primes if prime <= n
             and all_circular_perm_in(prime, possible_primes)]
开发者ID:mireq,项目名称:project-euler,代码行数:8,代码来源:no035.py

示例8: main

def main(verbose=False):
    # By the prime number theorem, pi(x) =~ x/ln(x)
    # pi(x) >= 10001 when x >= 10001 ln(x)
    # To be safe, we'll double it and solve
    # x = 20002 ln(x)

    # We are left with approximately 248490
    primes = sieve(248490)
    return primes[10001 - 1]
开发者ID:dhermes,项目名称:project-euler,代码行数:9,代码来源:no007.py

示例9: main

def main(verbose=False):
    # sieve(6000) will do it (answer is 5777)
    curr = 9
    primes = sieve(5777)
    while is_possible(curr, primes):
        curr += 2
        while curr in primes:
            curr += 2
    return curr
开发者ID:dhermes,项目名称:project-euler,代码行数:9,代码来源:no046.py

示例10: main

def main(verbose=False):
    problem_max = 10 ** 6
    count = 0
    PRIMES = sieve(problem_max)
    max_L = int(round((sqrt(12 * problem_max - 3) - 3) / 6))
    for L in xrange(1, max_L + 1):
        difference = (L + 1) ** 3 - L ** 3
        if difference in PRIMES:
            count += 1
    return count
开发者ID:dhermes,项目名称:project-euler,代码行数:10,代码来源:no131.py

示例11: prime_sequence_exists

def prime_sequence_exists(length, digits, primes=[]):
    """
    Returns True if a sequence of length consecutive primes which sums
    to less than 10**digits also sums to a prime number
    """
    cap = 10**digits
    if primes == []:
        primes = sieve(cap)
    sums = [sum(seq) for seq in all_prime_seqs(length, digits, primes)]
    return (set(sums).intersection(primes) != set())
开发者ID:dhermes,项目名称:project-euler,代码行数:10,代码来源:no050.py

示例12: main

def main(verbose=False):
    problem_max = 10 ** 6
    PRIMES = sieve(problem_max)
    # ignore zero index
    ratios = [(1, index) for index in range(problem_max + 1)]
    for prime in PRIMES:
        ratios[prime::prime] = [((ratio * prime * 1.0) / (prime - 1), index)
                                for ratio, index in ratios[prime::prime]]
    ratios.sort(key=lambda pair: pair[0], reverse=True)
    return ratios[0][1]
开发者ID:dhermes,项目名称:project-euler,代码行数:10,代码来源:no069.py

示例13: matches

def matches(problem_max):
    PRIMES = sieve(problem_max)
    phi_list = range(problem_max + 1)  # ignore zero index
    for prime in PRIMES:
        phi_list[prime::prime] = [(val / prime) * (prime - 1) for val in
                                  phi_list[prime::prime]]
    cands = [(val, phi_list[val]) for val in range(2, problem_max + 1)
             if same_digits(val, phi_list[val])]
    cands.sort(key=lambda cand: (cand[0] * 1.0) / cand[1])
    return cands[0][0]
开发者ID:dhermes,项目名称:project-euler,代码行数:10,代码来源:no070.py

示例14: main

def main(verbose=False):
    PRIMES = sieve(10 ** 6)
    prime_index = 3  # p0=2, p1=3, and p2=5 are false positives
    matches = []
    while len(matches) < 40:
        prime = PRIMES[prime_index]
        if prime_divides_repunit_power10(prime, 9):
            matches.append(prime)
        prime_index += 1

    return sum(matches)
开发者ID:dhermes,项目名称:project-euler,代码行数:11,代码来源:no132.py

示例15: main

def main(verbose=False):
    # Since p_n > 70710, to be safe let's multiply by 10
    PRIMES = sieve(707100)
    # The odd primes are the even indices here
    prime_index = 1
    product = 2*(1*2) # p_1 = 2
    while product < 10**10:
        prime_index += 2
        prime = PRIMES[prime_index-1]
        product = 2*(prime_index*prime)
    return prime_index
开发者ID:mireq,项目名称:project-euler,代码行数:11,代码来源:no123.py


注:本文中的python.functions.sieve函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。