本文整理汇总了Python中utils.Utils.sieve方法的典型用法代码示例。如果您正苦于以下问题:Python Utils.sieve方法的具体用法?Python Utils.sieve怎么用?Python Utils.sieve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.Utils
的用法示例。
在下文中一共展示了Utils.sieve方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: p46
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import sieve [as 别名]
def p46():
#getting precalculated primes and squares:
u = Utils()
primes = u.sieve(6000)
squares = [i * i for i in range(1, 101)]
#start searching at 15:
o = 15
while o < 5800:
#is the test number prime?
if u.chop(o, primes) != -1:
#yeap, go to next one:
o += 2
continue
#nope, begin testing:
ind = 0
found = False
while o - primes[ind] > 0:
s = (o - primes[ind]) / 2
#found desired decomposition?
if u.chop(s, squares) != -1:
#yep, break out:
found = True
break
#nope, try next prime:
else:
ind += 1
#found possible candidate:
if not found:
print o
#keep searching:
o += 2
示例2: p35
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import sieve [as 别名]
def p35():
u = Utils()
sieve = u.sieve(10 ** 6)
count = 0
for prime in sieve:
s = str(prime)
l = cyclic_shifts_of(s)
if all_in(l, sieve, u):
count += 1
return count
示例3: p37
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import sieve [as 别名]
def p37():
u = Utils()
primes = u.sieve(10 ** 6)
total = 0
for p in primes:
a = is_right_truncatable(p, u, primes)
b = is_left_truncatable(p, u, primes)
if a and b:
total += p
#we have to ignore 2, 3, 5, and 7, which sum up to 17:
print total - 17
示例4: p127
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import sieve [as 别名]
def p127(max_c, exp):
u = Utils()
primes = u.sieve(max_c)
radicals = rad(int(max_c), primes)
possible_ys = [i for i in range(1, max_c) if radicals[i] <= int(max_c ** exp)]
possible_rads = [radicals[i] for i in possible_ys]
print("len(radicals):", len(radicals))
print("len(possible_ys):", len(possible_ys))
print(possible_ys)
print(possible_rads)
total = 0
for a in possible_ys:
for b in possible_ys:
c = a + b
if a < b and c < max_c and hit(a, b, c, radicals):
print(a,b,c)
total += c
print(total)
示例5: Utils
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import sieve [as 别名]
#Consider the consecutive primes p1 = 19 and p2 = 23. It can be verified that
#1219 is the smallest number such that the last digits are formed by p1 whilst
#also being divisible by p2.
#
#In fact, with the exception of p1 = 3 and p2 = 5, for every pair of consecutive
#primes, p2 > p1, there exist values of n for which the last digits are formed by
#p1 and n is divisible by p2. Let S be the smallest of these values of n.
#
#Find S for every pair of consecutive primes with 5 <= p1 <= 1000000.
from utils import Utils
u = Utils()
a = u.sieve(1100000)
"""
It is assumed that n is a power of 10.
"""
def phi(n):
return (2 * n) / 5
def f1(p1, p2):
m = len(str(p1))
r = 10 ** m
q = phi(r) - 1
s = pow(p2, q, r)
return ((((p1 * p2 * s) / r) % p2) * r) + p1
l = [f1(a[i], a[i + 1]) for i in range(2, len(a) - 1) if a[i] < 10 ** 6]
print sum(l)
示例6: when
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import sieve [as 别名]
#coding: UTF-8
#Let p_n be the nth prime: 2, 3, 5, 7, 11, ..., and let r
#be the remainder when (p_n − 1)^n + (p_n + 1)^n is divided
#by p_n^2.
#
#For example, when n = 3, p_3 = 5, and 4^3 + 6^3 = 280
#≡ 5 mod 25.
#
#The least value of n for which the remainder first exceeds
#10^9 is 7037.
#
#Find the least value of n for which the remainder first
#exceeds 10^10.
from utils import Utils
u = Utils()
p = u.sieve(4 * (10 ** 5))
def p123():
i = 0
a = 0
while a < 10 ** 10 and i < len(p):
i += 1
a = (2 * p[i] * (i + 1)) % (p[i] ** 2)
#sum 2 because i is the index of the number just below
#10 ** 10, and array indices start by 0:
print i + 2
u.exec_time(p123)
示例7: range
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import sieve [as 别名]
#a perfect cube.
#
#For example, when p = 19, 8^3 + 8^2×19 = 12^3.
#
#What is perhaps most surprising is that for each prime with
#this property the value of n is unique, and there are only
#four such primes below one-hundred.
#
#How many primes below one million have this remarkable
#property?
from utils import Utils
cube_list = [x ** 3 for x in range(578)]
u = Utils()
prime_list = u.sieve(10 ** 6)
def p131():
"""
As taken from the forum:
Since x^3 = n^2(n + p), and p is a prime, it turns
out that n must be a cube, as well as n + p, i.e.,
we must have p = a^3 - b^3 for some a, b.
But, for a^3 - b^3 = (a - b)(a^2 + ab + b^2) to be
prime we must have a - b = 1, so p must be a
difference of consecutive cubes.
"""
total = 0
for i in range(len(cube_list) - 1):
p_ = cube_list[i + 1] - cube_list[i]