本文整理汇总了Python中sympy.ntheory.totient函数的典型用法代码示例。如果您正苦于以下问题:Python totient函数的具体用法?Python totient怎么用?Python totient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了totient函数的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: pohlig_helman
def pohlig_helman(Y, g, p):
print("Solving 0x%s = 0x%s^x (mod 0x%s)" % (hex(Y), hex(g), hex(p)))
#print(factorint(p))
t1 = time.clock()
n = totient(p)
t2 = time.clock()
q = factorint(n)
t3 = time.clock()
print("totient(%i) = %i" % (p, n))
print("factor(%i) = %s" % (n, str(q)))
print("totient time: " + str(t2-t1))
print("factor time: " + str(t3-t2))
t4 = time.clock()
x_is = list()
mods = list()
for p_i in q:
e = q[p_i]
x_i = solve_xi(Y, g, p, p_i, e, n)
print("x = %i mod %i" % (x_i, pow(p_i, e)))
x_is.append(x_i)
mods.append(pow(p_i, e))
x = chinese_remainder(mods, x_is)
t5 = time.clock()
print("x = %i" % x)
print("ph time: " + str(t5-t4))
return x
开发者ID:Henkru,项目名称:anglerek-keyfinder,代码行数:34,代码来源:anglerek-keyfinder.py
示例2: test_totient
def test_totient():
assert [totient(k) for k in range(1, 12)] == [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10]
assert totient(5005) == 2880
assert totient(5006) == 2502
assert totient(5009) == 5008
assert totient(2 ** 100) == 2 ** 99
m = Symbol("m", integer=True)
assert totient(m)
assert totient(m).subs(m, 3 ** 10) == 3 ** 10 - 3 ** 9
assert summation(totient(m), (m, 1, 11)) == 42
n = Symbol("n", integer=True, positive=True)
assert totient(n).is_integer
开发者ID:whimsy-Pan,项目名称:sympy,代码行数:14,代码来源:test_factor_.py
示例3: test
def test(totients,n):
print "Starting Test"
a = totients
b = correctTotients(n)
if a==b:
print "Pass!"
for i in range(len(a)):
if a[i]!=b[i]:
print i,totient(i),a[i],b[i]
print "Fail!"
break
开发者ID:jkschin,项目名称:project_euler,代码行数:11,代码来源:351.py
示例4: test_generate
def test_generate():
assert nextprime(-4) == 2
assert nextprime(2) == 3
assert nextprime(5) == 7
assert nextprime(12) == 13
assert nextprime(90) == 97
assert nextprime(10**40) == (10**40 + 121)
assert prevprime(3) == 2
assert prevprime(7) == 5
assert prevprime(13) == 11
assert prevprime(97) == 89
assert prevprime(10**40) == (10**40 - 17)
assert list(primerange(2, 7)) == [2, 3, 5]
assert list(primerange(2, 10)) == [2, 3, 5, 7]
assert list(primerange(1050, 1100)) == [1051, 1061,
1063, 1069, 1087, 1091, 1093, 1097]
s = Sieve()
for i in range(30, 2350, 376):
for j in range(2, 5096, 1139):
A = list(s.primerange(i, i + j))
B = list(primerange(i, i + j))
assert A == B
s = Sieve()
assert s[10] == 29
assert nextprime(2, 2) == 5
raises(ValueError, lambda: totient(0))
raises(ValueError, lambda: reduced_totient(0))
raises(ValueError, lambda: primorial(0))
assert mr(1, [2]) is False
func = lambda i: (i**2 + 1) % 51
assert next(cycle_length(func, 4)) == (6, 2)
assert list(cycle_length(func, 4, values=True)) == \
[17, 35, 2, 5, 26, 14, 44, 50, 2, 5, 26, 14]
assert next(cycle_length(func, 4, nmax=5)) == (5, None)
assert list(cycle_length(func, 4, nmax=5, values=True)) == \
[17, 35, 2, 5, 26]
开发者ID:JamesdeLisle,项目名称:sympy,代码行数:42,代码来源:test_generate.py
示例5: rsa_private_key
def rsa_private_key(p, q, e):
r"""
The RSA *private key* is the pair `(n,d)`, where `n`
is a product of two primes and `d` is the inverse of
`e` (mod `\phi(n)`).
Examples
========
>>> from sympy.crypto.crypto import rsa_private_key
>>> p, q, e = 3, 5, 7
>>> rsa_private_key(p, q, e)
(15, 7)
"""
n = p*q
phi = totient(n)
if isprime(p) and isprime(q) and gcd(e, phi) == 1:
return n, pow(e, phi - 1, phi)
return False
开发者ID:AALEKH,项目名称:sympy,代码行数:20,代码来源:crypto.py
示例6: rsa_public_key
def rsa_public_key(p, q, e):
r"""
The RSA *public key* is the pair `(n,e)`, where `n`
is a product of two primes and `e` is relatively
prime (coprime) to the Euler totient `\phi(n)`.
Examples
========
>>> from sympy.crypto.crypto import rsa_public_key
>>> p, q, e = 3, 5, 7
>>> n, e = rsa_public_key(p, q, e)
>>> n
15
>>> e
7
"""
n = p*q
phi = totient(n)
if isprime(p) and isprime(q) and gcd(e, phi) == 1:
return n, e
return False
开发者ID:Upabjojr,项目名称:sympy,代码行数:23,代码来源:crypto.py
示例7: test_residue
def test_residue():
assert n_order(2, 13) == 12
assert [n_order(a, 7) for a in range(1, 7)] == \
[1, 3, 6, 3, 6, 2]
assert n_order(5, 17) == 16
assert n_order(17, 11) == n_order(6, 11)
assert n_order(101, 119) == 6
assert n_order(11, (10**50 + 151)**2) == 10000000000000000000000000000000000000000000000030100000000000000000000000000000000000000000000022650
raises(ValueError, lambda: n_order(6, 9))
assert is_primitive_root(2, 7) is False
assert is_primitive_root(3, 8) is False
assert is_primitive_root(11, 14) is False
assert is_primitive_root(12, 17) == is_primitive_root(29, 17)
raises(ValueError, lambda: is_primitive_root(3, 6))
assert [primitive_root(i) for i in range(2, 31)] == [1, 2, 3, 2, 5, 3, \
None, 2, 3, 2, None, 2, 3, None, None, 3, 5, 2, None, None, 7, 5, \
None, 2, 7, 2, None, 2, None]
for p in primerange(3, 100):
it = _primitive_root_prime_iter(p)
assert len(list(it)) == totient(totient(p))
assert primitive_root(97) == 5
assert primitive_root(97**2) == 5
assert primitive_root(40487) == 5
# note that primitive_root(40487) + 40487 = 40492 is a primitive root
# of 40487**2, but it is not the smallest
assert primitive_root(40487**2) == 10
assert primitive_root(82) == 7
p = 10**50 + 151
assert primitive_root(p) == 11
assert primitive_root(2*p) == 11
assert primitive_root(p**2) == 11
raises(ValueError, lambda: primitive_root(-3))
assert is_quad_residue(3, 7) is False
assert is_quad_residue(10, 13) is True
assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139)
assert is_quad_residue(207, 251) is True
assert is_quad_residue(0, 1) is True
assert is_quad_residue(1, 1) is True
assert is_quad_residue(0, 2) == is_quad_residue(1, 2) is True
assert is_quad_residue(1, 4) is True
assert is_quad_residue(2, 27) is False
assert is_quad_residue(13122380800, 13604889600) is True
assert [j for j in range(14) if is_quad_residue(j, 14)] == \
[0, 1, 2, 4, 7, 8, 9, 11]
raises(ValueError, lambda: is_quad_residue(1.1, 2))
raises(ValueError, lambda: is_quad_residue(2, 0))
assert quadratic_residues(12) == [0, 1, 4, 9]
assert quadratic_residues(13) == [0, 1, 3, 4, 9, 10, 12]
assert [len(quadratic_residues(i)) for i in range(1, 20)] == \
[1, 2, 2, 2, 3, 4, 4, 3, 4, 6, 6, 4, 7, 8, 6, 4, 9, 8, 10]
assert list(sqrt_mod_iter(6, 2)) == [0]
assert sqrt_mod(3, 13) == 4
assert sqrt_mod(3, -13) == 4
assert sqrt_mod(6, 23) == 11
assert sqrt_mod(345, 690) == 345
for p in range(3, 100):
d = defaultdict(list)
for i in range(p):
d[pow(i, 2, p)].append(i)
for i in range(1, p):
it = sqrt_mod_iter(i, p)
v = sqrt_mod(i, p, True)
if v:
v = sorted(v)
assert d[i] == v
else:
assert not d[i]
assert sqrt_mod(9, 27, True) == [3, 6, 12, 15, 21, 24]
assert sqrt_mod(9, 81, True) == [3, 24, 30, 51, 57, 78]
assert sqrt_mod(9, 3**5, True) == [3, 78, 84, 159, 165, 240]
assert sqrt_mod(81, 3**4, True) == [0, 9, 18, 27, 36, 45, 54, 63, 72]
assert sqrt_mod(81, 3**5, True) == [9, 18, 36, 45, 63, 72, 90, 99, 117,\
126, 144, 153, 171, 180, 198, 207, 225, 234]
assert sqrt_mod(81, 3**6, True) == [9, 72, 90, 153, 171, 234, 252, 315,\
333, 396, 414, 477, 495, 558, 576, 639, 657, 720]
assert sqrt_mod(81, 3**7, True) == [9, 234, 252, 477, 495, 720, 738, 963,\
981, 1206, 1224, 1449, 1467, 1692, 1710, 1935, 1953, 2178]
for a, p in [(26214400, 32768000000), (26214400, 16384000000),
(262144, 1048576), (87169610025, 163443018796875),
(22315420166400, 167365651248000000)]:
assert pow(sqrt_mod(a, p), 2, p) == a
n = 70
a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+2)
it = sqrt_mod_iter(a, p)
for i in range(10):
assert pow(next(it), 2, p) == a
a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+3)
it = sqrt_mod_iter(a, p)
for i in range(2):
#.........这里部分代码省略.........
开发者ID:LuckyStrikes1090,项目名称:sympy,代码行数:101,代码来源:test_ntheory.py
示例8: test_totient
def test_totient():
assert [totient(k) for k in range(1, 12)] == [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10]
assert totient(5005) == 2880
assert totient(5006) == 2502
assert totient(5009) == 5008
开发者ID:hitej,项目名称:meta-core,代码行数:5,代码来源:test_ntheory.py
示例9: test_totient
def test_totient():
assert [totient(k) for k in range(1, 12)] == \
[1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10]
assert totient(5005) == 2880
assert totient(5006) == 2502
assert totient(5009) == 5008
assert totient(2**100) == 2**99
raises(ValueError, lambda: totient(30.1))
raises(ValueError, lambda: totient(20.001))
m = Symbol("m", integer=True)
assert totient(m)
assert totient(m).subs(m, 3**10) == 3**10 - 3**9
assert summation(totient(m), (m, 1, 11)) == 42
n = Symbol("n", integer=True, positive=True)
assert totient(n).is_integer
x=Symbol("x", integer=False)
raises(ValueError, lambda: totient(x))
y=Symbol("y", positive=False)
raises(ValueError, lambda: totient(y))
z=Symbol("z", positive=True, integer=True)
raises(ValueError, lambda: totient(2**(-z)))
开发者ID:KonstantinTogoi,项目名称:sympy,代码行数:27,代码来源:test_factor_.py
示例10: test_residue
def test_residue():
assert n_order(2, 13) == 12
assert [n_order(a, 7) for a in range(1, 7)] == \
[1, 3, 6, 3, 6, 2]
assert n_order(5, 17) == 16
assert n_order(17, 11) == n_order(6, 11)
assert n_order(101, 119) == 6
assert n_order(11, (10**50 + 151)**2) == 10000000000000000000000000000000000000000000000030100000000000000000000000000000000000000000000022650
raises(ValueError, lambda: n_order(6, 9))
assert is_primitive_root(2, 7) is False
assert is_primitive_root(3, 8) is False
assert is_primitive_root(11, 14) is False
assert is_primitive_root(12, 17) == is_primitive_root(29, 17)
raises(ValueError, lambda: is_primitive_root(3, 6))
assert [primitive_root(i) for i in range(2, 31)] == [1, 2, 3, 2, 5, 3, \
None, 2, 3, 2, None, 2, 3, None, None, 3, 5, 2, None, None, 7, 5, \
None, 2, 7, 2, None, 2, None]
for p in primerange(3, 100):
it = _primitive_root_prime_iter(p)
assert len(list(it)) == totient(totient(p))
assert primitive_root(97) == 5
assert primitive_root(97**2) == 5
assert primitive_root(40487) == 5
# note that primitive_root(40487) + 40487 = 40492 is a primitive root
# of 40487**2, but it is not the smallest
assert primitive_root(40487**2) == 10
assert primitive_root(82) == 7
p = 10**50 + 151
assert primitive_root(p) == 11
assert primitive_root(2*p) == 11
assert primitive_root(p**2) == 11
raises(ValueError, lambda: primitive_root(-3))
assert is_quad_residue(3, 7) is False
assert is_quad_residue(10, 13) is True
assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139)
assert is_quad_residue(207, 251) is True
assert is_quad_residue(0, 1) is True
assert is_quad_residue(1, 1) is True
assert is_quad_residue(0, 2) == is_quad_residue(1, 2) is True
assert is_quad_residue(1, 4) is True
assert is_quad_residue(2, 27) is False
assert is_quad_residue(13122380800, 13604889600) is True
assert [j for j in range(14) if is_quad_residue(j, 14)] == \
[0, 1, 2, 4, 7, 8, 9, 11]
raises(ValueError, lambda: is_quad_residue(1.1, 2))
raises(ValueError, lambda: is_quad_residue(2, 0))
assert quadratic_residues(S.One) == [0]
assert quadratic_residues(1) == [0]
assert quadratic_residues(12) == [0, 1, 4, 9]
assert quadratic_residues(12) == [0, 1, 4, 9]
assert quadratic_residues(13) == [0, 1, 3, 4, 9, 10, 12]
assert [len(quadratic_residues(i)) for i in range(1, 20)] == \
[1, 2, 2, 2, 3, 4, 4, 3, 4, 6, 6, 4, 7, 8, 6, 4, 9, 8, 10]
assert list(sqrt_mod_iter(6, 2)) == [0]
assert sqrt_mod(3, 13) == 4
assert sqrt_mod(3, -13) == 4
assert sqrt_mod(6, 23) == 11
assert sqrt_mod(345, 690) == 345
for p in range(3, 100):
d = defaultdict(list)
for i in range(p):
d[pow(i, 2, p)].append(i)
for i in range(1, p):
it = sqrt_mod_iter(i, p)
v = sqrt_mod(i, p, True)
if v:
v = sorted(v)
assert d[i] == v
else:
assert not d[i]
assert sqrt_mod(9, 27, True) == [3, 6, 12, 15, 21, 24]
assert sqrt_mod(9, 81, True) == [3, 24, 30, 51, 57, 78]
assert sqrt_mod(9, 3**5, True) == [3, 78, 84, 159, 165, 240]
assert sqrt_mod(81, 3**4, True) == [0, 9, 18, 27, 36, 45, 54, 63, 72]
assert sqrt_mod(81, 3**5, True) == [9, 18, 36, 45, 63, 72, 90, 99, 117,\
126, 144, 153, 171, 180, 198, 207, 225, 234]
assert sqrt_mod(81, 3**6, True) == [9, 72, 90, 153, 171, 234, 252, 315,\
333, 396, 414, 477, 495, 558, 576, 639, 657, 720]
assert sqrt_mod(81, 3**7, True) == [9, 234, 252, 477, 495, 720, 738, 963,\
981, 1206, 1224, 1449, 1467, 1692, 1710, 1935, 1953, 2178]
for a, p in [(26214400, 32768000000), (26214400, 16384000000),
(262144, 1048576), (87169610025, 163443018796875),
(22315420166400, 167365651248000000)]:
assert pow(sqrt_mod(a, p), 2, p) == a
n = 70
a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+2)
it = sqrt_mod_iter(a, p)
for i in range(10):
assert pow(next(it), 2, p) == a
#.........这里部分代码省略.........
开发者ID:asmeurer,项目名称:sympy,代码行数:101,代码来源:test_residue.py
示例11: test_generate
def test_generate():
from sympy.ntheory.generate import sieve
sieve._reset()
assert nextprime(-4) == 2
assert nextprime(2) == 3
assert nextprime(5) == 7
assert nextprime(12) == 13
assert prevprime(3) == 2
assert prevprime(7) == 5
assert prevprime(13) == 11
assert prevprime(19) == 17
assert prevprime(20) == 19
sieve.extend_to_no(9)
assert sieve._list[-1] == 23
assert sieve._list[-1] < 31
assert 31 in sieve
assert nextprime(90) == 97
assert nextprime(10**40) == (10**40 + 121)
assert prevprime(97) == 89
assert prevprime(10**40) == (10**40 - 17)
assert list(sieve.primerange(10, 1)) == []
assert list(sieve.primerange(5, 9)) == [5, 7]
sieve._reset(prime=True)
assert list(sieve.primerange(2, 12)) == [2, 3, 5, 7, 11]
assert list(sieve.totientrange(5, 15)) == [4, 2, 6, 4, 6, 4, 10, 4, 12, 6]
sieve._reset(totient=True)
assert list(sieve.totientrange(3, 13)) == [2, 2, 4, 2, 6, 4, 6, 4, 10, 4]
assert list(sieve.totientrange(900, 1000)) == [totient(x) for x in range(900, 1000)]
assert list(sieve.totientrange(0, 1)) == []
assert list(sieve.totientrange(1, 2)) == [1]
assert list(sieve.mobiusrange(5, 15)) == [-1, 1, -1, 0, 0, 1, -1, 0, -1, 1]
sieve._reset(mobius=True)
assert list(sieve.mobiusrange(3, 13)) == [-1, 0, -1, 1, -1, 0, 0, 1, -1, 0]
assert list(sieve.mobiusrange(1050, 1100)) == [mobius(x) for x in range(1050, 1100)]
assert list(sieve.mobiusrange(0, 1)) == []
assert list(sieve.mobiusrange(1, 2)) == [1]
assert list(primerange(10, 1)) == []
assert list(primerange(2, 7)) == [2, 3, 5]
assert list(primerange(2, 10)) == [2, 3, 5, 7]
assert list(primerange(1050, 1100)) == [1051, 1061,
1063, 1069, 1087, 1091, 1093, 1097]
s = Sieve()
for i in range(30, 2350, 376):
for j in range(2, 5096, 1139):
A = list(s.primerange(i, i + j))
B = list(primerange(i, i + j))
assert A == B
s = Sieve()
assert s[10] == 29
assert nextprime(2, 2) == 5
raises(ValueError, lambda: totient(0))
raises(ValueError, lambda: reduced_totient(0))
raises(ValueError, lambda: primorial(0))
assert mr(1, [2]) is False
func = lambda i: (i**2 + 1) % 51
assert next(cycle_length(func, 4)) == (6, 2)
assert list(cycle_length(func, 4, values=True)) == \
[17, 35, 2, 5, 26, 14, 44, 50, 2, 5, 26, 14]
assert next(cycle_length(func, 4, nmax=5)) == (5, None)
assert list(cycle_length(func, 4, nmax=5, values=True)) == \
[17, 35, 2, 5, 26]
sieve.extend(3000)
assert nextprime(2968) == 2969
assert prevprime(2930) == 2927
raises(ValueError, lambda: prevprime(1))
开发者ID:KonstantinTogoi,项目名称:sympy,代码行数:78,代码来源:test_generate.py
示例12: phi2
__author__ = 'Prateek'
from sympy.ntheory import factorint
from sympy.ntheory import totient
def phi2(n):
value = n
for prime, exp in factorint(n).items():
value = value * (1 - prime ** -1)
return int(value)
if __author__ == 'Prateek':
print phi2(666)
print totient(666)
开发者ID:prateekgulati,项目名称:numberTheory,代码行数:15,代码来源:phy2.py
示例13: sumtot
def sumtot(j):
result = 0
for k in range(n//(j+1)+1, n//j +1):
#result = modf(result+totient(k))
result = result+totient(k)
return result
开发者ID:desaxce,项目名称:euler,代码行数:6,代码来源:pb465.py
示例14: print
# if j%100==0:
# print("Processing j = ", j)
# PSI.append(totientsum(n//j))
# j+=1
#print("Done with totient sums")
#with open('sumstot.txt', 'wb') as f:
#pickle.dump(PSI, f)
with open('sumstot.txt', 'rb') as f:
PSI = pickle.load(f)
#print("Computing L")
L = 1
for j in range(2, int(sqrt(n))+1):
L = mod(L*mod(int(pow(n//j+1, totient(j), 1000000007))))
#print("L - Part 1 complete")
for j in range(1, int(sqrt(n))+1): # sometimes up to sqrt(n), sometimes just below
L = mod(L*mod(int(pow(j+1, PSI[j-1]-PSI[j], 1000000007))))
#L = mod(L*mod(int(pow(j+1, sumtot(j)))))
L = mod(L*(n+1))
#print("L - Part 2 complete")
#print("Done with L")
L8 = mod(int(pow(L, 8)))
L4 = mod(int(pow(L, 4)))
#print("Computing T")
T = 0
for j in range(2, int(sqrt(n))+1):
T = mod(T+(2*(n//j)*L4 - (n//j)*(n//j))*totient(j))
开发者ID:desaxce,项目名称:euler,代码行数:31,代码来源:pb465.py
示例15: correctTotients
def correctTotients(n):
totients = [0]*(n+1)
for i in range(1,len(totients)):
totients[i]=totient(i)
return totients
开发者ID:jkschin,项目名称:project_euler,代码行数:5,代码来源:351.py
示例16: print
from sympy.ntheory import totient
limit = 1000001
solution = 0
print(sum(totient(n) for n in range(2, limit)))
开发者ID:jvujcic,项目名称:ProjectEuler,代码行数:6,代码来源:072.py
注:本文中的sympy.ntheory.totient函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论