def totient_comparisons(max_n): #x is our modulus false_pos = 0 false_neg = 0 for x in range(3, max_n): self_squares = [] #factoring to find invertible numbers primes = factorint(x) #checking all the numbers where gcd(x,y)=1 for y in range(1, x): #checks that no prime divisors of our modulus (x) divide our number y #then checks if y is its own inverse if all(y % p != 0 for p in primes.keys()) and (y * y) % x == 1: self_squares.append(y) #if we have some interesting solutions, verify the relationship between #totient(n)%4 and the existence of solutions (it's necessary, not sufficient) if (len(self_squares) > 2) != (sp.totient(x) % 4 == 0): if len(self_squares) > 2: false_neg += 1 else: false_pos += 1 if len(self_squares) > 2: print("Solution totient: {}".format( sp.factorint(sp.totient(max(self_squares))))) #looking at the factorizations of solutions print("N totient: {}".format(sp.factorint(sp.totient(x)))) print("False positive solns: {} False negative solns: {}".format( false_pos, false_neg))
def get_reps(n, coprime=True): # note that each representative represents n-1 other entries # so in total there are phi(2**n - 1) l = sympy.totient(2**n - 1) / n reps = [1] p = 2 while p < 2**n - 1: if coprime: m = p = sympy.nextprime(p) else: p += 2 if p % 2 == 0: p -= 1 if coprime and gcd(p, 2**n - 1) != 1: continue i = 1 for i in range(1, n): if (p * 2**i) % (2**n - 1) in reps: break if coprime: m = m if (m < (p * 2**i) % (2**n - 1)) else ((p * 2**i) % (2**n - 1)) if (p * 2**i) % (2**n - 1) in reps: continue if not coprime: m = p reps.append(m) if len(reps) == l and coprime: break return sorted(reps)
def order(n, elt): if elt in coPrime(n): for i in sorted(divisors(totient(n))): if elt ** i % n == 1: return i else: return "Element not in set"
def magic_gcd(m, n): ans = 0 for k in xrange(1, n+1): if m % k == 0 and n % k == 0: print k ans += totient(k) return ans
def totient_chain_length(n): global cache if n == 1: return 1 if n not in cache: cache[n] = totient_chain_length(totient(n)) return 1 + cache[n]
def euler69(): ret = 0 max_calc = 0 for n in xrange(2, 1000001): t = n * 1.0 / totient(n) if max(max_calc, t) == t: max_calc = t ret = n return ret
def R(d): """ The number of resilient fractions can be given as the Euler totient of the given number n as we are looking for the number of coprime integers less than n. So R(d) = totient(d) / (d-1) """ return Fraction(totient(d), d - 1)
def min_totient_ratio(arr): ret, min_ratio = 0, 1000 for n in arr: t = totient(n) if is_permutation(n, t): ratio = n * 1.0 / t if ratio < min_ratio: min_ratio = ratio ret = n return min_ratio, ret
def grapher_tau(n): import matplotlib.pyplot as plt X=[] Y=[] for i in range(1,n+1): X.append(i) Y.append(tau(i)+sp.totient(i)) if(i%500==0): print(i," segment "," covered") plt.plot(X,Y)
def G(N): s1, s2 = 0, 0 for d in range(1, math.floor(math.sqrt(N)) + 1): s1 += (d * H(math.floor(N / d))) % 998244353 #print(d, s1) print("1st Sum Completed...") for c in range(1, math.floor(math.sqrt(N)) + 1): s2 += (sympy.totient(c) * F(math.floor(N / c))) % 998244353 #print(c, s2) print("2nd Sum Completed...") s3 = (F(math.floor(math.sqrt(N))) * H(math.floor(math.sqrt(N)))) % 998244353 return (s1 + s2 - s3) % 998244353
def calc_pn(n): n1 = len(n) phi_n = sympy.totient(n1) for i in range(1, phi_n + 1): myk = (2**i) % n1 if myk == 1: ord_2 = i a = ord_2 / 2 myk2 = (2**a) % n1 if myk2 == n1 - 1: pn = n1 * (2**a - 1) else: pn = 2**a - 1 return pn
from sympy import totient, mod_inverse # n = 0, 1, 2, 3 result = 1 + 3 + 7 + 61 p = 2**8 q = 7**8 modulo = p * q phi_chain = [q] k = q while k > 1: k = totient(k) phi_chain.append(k) # A(n, n) + 3 = 0 (mod 2^8) for n > 3 u = p * mod_inverse(p, q) # n = 4 A(4, 4) = 2^2^2^65536 - 3 w = 65536 for i in 2, 1, 0: w = pow(2, w, phi_chain[i]) result = (result + w * u - 3) % modulo # n = 5, 6 A(5, 5) % modulo = A(6, 6) % modulo w = 2 for phi in reversed(phi_chain): w = pow(2, w, phi) result = (result + 2 * (w * u - 3)) % modulo print(result)
for p in PRIMES: if p > halfN: break div, mod = divmod(n, p) if mod == 0: factors.append(p) prevDiv = div div, mod = divmod(div, p) while mod == 0: prevDiv = div div, mod = divmod(div, p) factors += factorsOf(prevDiv) return set(factors) def nOnPhiN(n): # p = factorint(n).keys() p = factorsOf(n) total = 1 for fact in p: total *= (fact - 1) / fact return 1 / total possible = sorted([(nOnPhiN(n), n) for n in trange(2, top)], key=lambda a: (a[0], top - a[1])) for pair in tqdm(possible): ratio, n = pair if is_perm(n, totient(n)): print(n) break
p = 12039102490128509125925019010000012423515617235219127649182470182570195018265927223 q = 1039300813886545966418005631983853921163721828798787466771912919828750891 assert (p - 1) % q == 0 g = 10729072579307052184848302322451332192456229619044181105063011741516558110216720725 x = int.from_bytes(b"Hi! I am Vadim Davydov from ITMO University", 'big') G = pow(g, x, p) Gp = int.to_bytes(G, ceil(log2(G) / 8), 'big') a = sha512(Gp).digest() ap = int.from_bytes(a, 'big') t = int(totient(p)) aa = pow(ap, ap, t) h = aa % p print("p =", p) print("q =", q) print("g =", g) print("x =", x) print("G =", G) print("G'=", Gp) print("a =", a) print("a'=", ap) print("t =", t) print("aa=", aa)
# Number of test cases size = 250 # Cardinality of the set {m : phi(m) <= N} C_N = np.zeros(size - 1) # Values of N to be tested Ns = [i for i in range(1, size)] # Set of all values m s.t. phi(m) <= N, we do not need to recalcuate these values # if phi(m) <= N then, phi(m) <= N + 1 and so on ms = {1} for N in range(1, size): # Checks all values up to our analytic upper bound for m in range(1, 2 * N**2): # Skips values of m that have already been calculated if m in ms: continue elif sp.totient(m) <= N: ms.add(m) C_N[N - 1] += len(ms) # Stores pairs N and C_N when C_N > 2N (our trent line) pairs = [] for i in range(len(Ns)): if C_N[i] > 2 * Ns[i]: pairs.append((Ns[i], C_N[i])) # Plots data and lines plt.figure(figsize=(30, 15)) plt.plot([0, size], [0, size], 'k-') plt.plot([0, size], [0, 2 * size], 'g-') plt.plot([0, size], [0, 3 * size], 'k-') plt.bar(Ns, C_N) plt.show()
from numpy import argmax from sympy import totient from tqdm import trange print(argmax([n / totient(n) for n in trange(1, 1_000_000)]) + 1)
#!/usr/bin/python3 # -*- coding: utf-8 -*- # https://projecteuler.net/problem=70 from sympy import totient LIMIT = 10**7 oran = 42 for n in range(2,LIMIT): to = totient(n) for p in set(str(n)): if(str(n).count(p)!=str(to).count(p)): break else: if(set(str(n))==set(str(to))): if(len(set(str(n)))==len(set(str(to)))): if(n/to<oran): oran = n/to enkucuk = n print(enkucuk)
def euler72(): ret = 0 for d in xrange(2, 1000001): ret += totient(d) return ret
# -*- coding: utf-8 -*- """ Created on Sat Mar 05 19:13:49 2016 @author: Lucas """ import fractions import lucaslib import sympy def phi(n): result = 0 for i in xrange(1,n+1): if fractions.gcd(n,i) == 1: result += 1 return result maxval = 2 for x in xrange(2,1000000): if float(x)/sympy.totient(x) > maxval: maxval = float(x)/sympy.totient(x) print x print float(x)/sympy.totient(x) print
def number_reduced_proper_fractions(max_denominator): return sum(totient(d) for d in tqdm(range(2, max_denominator + 1)))
def test_latex_functions(): assert latex(exp(x)) == "e^{x}" assert latex(exp(1) + exp(2)) == "e + e^{2}" f = Function('f') assert latex(f(x)) == '\\operatorname{f}{\\left (x \\right )}' beta = Function('beta') assert latex(beta(x)) == r"\beta{\left (x \right )}" assert latex(sin(x)) == r"\sin{\left (x \right )}" assert latex(sin(x), fold_func_brackets=True) == r"\sin {x}" assert latex(sin(2*x**2), fold_func_brackets=True) == \ r"\sin {2 x^{2}}" assert latex(sin(x**2), fold_func_brackets=True) == \ r"\sin {x^{2}}" assert latex(asin(x)**2) == r"\operatorname{asin}^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="full") == \ r"\arcsin^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="power") == \ r"\sin^{-1}{\left (x \right )}^{2}" assert latex(asin(x**2), inv_trig_style="power", fold_func_brackets=True) == \ r"\sin^{-1} {x^{2}}" assert latex(factorial(k)) == r"k!" assert latex(factorial(-k)) == r"\left(- k\right)!" assert latex(subfactorial(k)) == r"!k" assert latex(subfactorial(-k)) == r"!\left(- k\right)" assert latex(factorial2(k)) == r"k!!" assert latex(factorial2(-k)) == r"\left(- k\right)!!" assert latex(binomial(2, k)) == r"{\binom{2}{k}}" assert latex( FallingFactorial(3, k)) == r"{\left(3\right)}_{\left(k\right)}" assert latex(RisingFactorial(3, k)) == r"{\left(3\right)}^{\left(k\right)}" assert latex(floor(x)) == r"\lfloor{x}\rfloor" assert latex(ceiling(x)) == r"\lceil{x}\rceil" assert latex(Min(x, 2, x**3)) == r"\min\left(2, x, x^{3}\right)" assert latex(Min(x, y)**2) == r"\min\left(x, y\right)^{2}" assert latex(Max(x, 2, x**3)) == r"\max\left(2, x, x^{3}\right)" assert latex(Max(x, y)**2) == r"\max\left(x, y\right)^{2}" assert latex(Abs(x)) == r"\left\lvert{x}\right\rvert" assert latex(re(x)) == r"\Re{x}" assert latex(re(x + y)) == r"\Re{x} + \Re{y}" assert latex(im(x)) == r"\Im{x}" assert latex(conjugate(x)) == r"\overline{x}" assert latex(gamma(x)) == r"\Gamma\left(x\right)" assert latex(Order(x)) == r"\mathcal{O}\left(x\right)" assert latex(lowergamma(x, y)) == r'\gamma\left(x, y\right)' assert latex(uppergamma(x, y)) == r'\Gamma\left(x, y\right)' assert latex(cot(x)) == r'\cot{\left (x \right )}' assert latex(coth(x)) == r'\coth{\left (x \right )}' assert latex(re(x)) == r'\Re{x}' assert latex(im(x)) == r'\Im{x}' assert latex(root(x, y)) == r'x^{\frac{1}{y}}' assert latex(arg(x)) == r'\arg{\left (x \right )}' assert latex(zeta(x)) == r'\zeta\left(x\right)' assert latex(zeta(x)) == r"\zeta\left(x\right)" assert latex(zeta(x)**2) == r"\zeta^{2}\left(x\right)" assert latex(zeta(x, y)) == r"\zeta\left(x, y\right)" assert latex(zeta(x, y)**2) == r"\zeta^{2}\left(x, y\right)" assert latex(dirichlet_eta(x)) == r"\eta\left(x\right)" assert latex(dirichlet_eta(x)**2) == r"\eta^{2}\left(x\right)" assert latex(polylog(x, y)) == r"\operatorname{Li}_{x}\left(y\right)" assert latex( polylog(x, y)**2) == r"\operatorname{Li}_{x}^{2}\left(y\right)" assert latex(lerchphi(x, y, n)) == r"\Phi\left(x, y, n\right)" assert latex(lerchphi(x, y, n)**2) == r"\Phi^{2}\left(x, y, n\right)" assert latex(elliptic_k(z)) == r"K\left(z\right)" assert latex(elliptic_k(z)**2) == r"K^{2}\left(z\right)" assert latex(elliptic_f(x, y)) == r"F\left(x\middle| y\right)" assert latex(elliptic_f(x, y)**2) == r"F^{2}\left(x\middle| y\right)" assert latex(elliptic_e(x, y)) == r"E\left(x\middle| y\right)" assert latex(elliptic_e(x, y)**2) == r"E^{2}\left(x\middle| y\right)" assert latex(elliptic_e(z)) == r"E\left(z\right)" assert latex(elliptic_e(z)**2) == r"E^{2}\left(z\right)" assert latex(elliptic_pi(x, y, z)) == r"\Pi\left(x; y\middle| z\right)" assert latex(elliptic_pi(x, y, z)**2) == \ r"\Pi^{2}\left(x; y\middle| z\right)" assert latex(elliptic_pi(x, y)) == r"\Pi\left(x\middle| y\right)" assert latex(elliptic_pi(x, y)**2) == r"\Pi^{2}\left(x\middle| y\right)" assert latex(Ei(x)) == r'\operatorname{Ei}{\left (x \right )}' assert latex(Ei(x)**2) == r'\operatorname{Ei}^{2}{\left (x \right )}' assert latex(expint(x, y)**2) == r'\operatorname{E}_{x}^{2}\left(y\right)' assert latex(Shi(x)**2) == r'\operatorname{Shi}^{2}{\left (x \right )}' assert latex(Si(x)**2) == r'\operatorname{Si}^{2}{\left (x \right )}' assert latex(Ci(x)**2) == r'\operatorname{Ci}^{2}{\left (x \right )}' assert latex(Chi(x)**2) == r'\operatorname{Chi}^{2}{\left (x \right )}' assert latex( jacobi(n, a, b, x)) == r'P_{n}^{\left(a,b\right)}\left(x\right)' assert latex(jacobi(n, a, b, x)**2) == r'\left(P_{n}^{\left(a,b\right)}\left(x\right)\right)^{2}' assert latex( gegenbauer(n, a, x)) == r'C_{n}^{\left(a\right)}\left(x\right)' assert latex(gegenbauer(n, a, x)**2) == r'\left(C_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(chebyshevt(n, x)) == r'T_{n}\left(x\right)' assert latex( chebyshevt(n, x)**2) == r'\left(T_{n}\left(x\right)\right)^{2}' assert latex(chebyshevu(n, x)) == r'U_{n}\left(x\right)' assert latex( chebyshevu(n, x)**2) == r'\left(U_{n}\left(x\right)\right)^{2}' assert latex(legendre(n, x)) == r'P_{n}\left(x\right)' assert latex(legendre(n, x)**2) == r'\left(P_{n}\left(x\right)\right)^{2}' assert latex( assoc_legendre(n, a, x)) == r'P_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_legendre(n, a, x)**2) == r'\left(P_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(laguerre(n, x)) == r'L_{n}\left(x\right)' assert latex(laguerre(n, x)**2) == r'\left(L_{n}\left(x\right)\right)^{2}' assert latex( assoc_laguerre(n, a, x)) == r'L_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_laguerre(n, a, x)**2) == r'\left(L_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(hermite(n, x)) == r'H_{n}\left(x\right)' assert latex(hermite(n, x)**2) == r'\left(H_{n}\left(x\right)\right)^{2}' # Test latex printing of function names with "_" assert latex( polar_lift(0)) == r"\operatorname{polar\_lift}{\left (0 \right )}" assert latex(polar_lift( 0)**3) == r"\operatorname{polar\_lift}^{3}{\left (0 \right )}" assert latex(totient(n)) == r'\phi\left( n \right)'
#!/usr/bin/env python from sympy import primefactors, mod_inverse, totient c = 10247955272908064997771727568918647737311526262165262458875076213296879253353684001266750329 e = 65537 n = 14393188157100504374851319373504728765762087532393762203410288659620743314524046223483350199 phi = totient(n) d = mod_inverse(e, phi) flag = pow(c, d, n) print(flag.to_bytes(flag.bit_length() // 8 + 1, 'big').decode())
def phi(n): tot = sympy.totient(n) print("phi(", n, ") =", tot) return tot
def NumOfDecimals(n): if n == 1 or n % 2 == 0 or n % 5 == 0: return (0, 0) phid = divisors(totient(n)) for m in phid: if (10**m - 1) % n == 0: return (n, m)
def test_latex_functions(): assert latex(exp(x)) == "e^{x}" assert latex(exp(1) + exp(2)) == "e + e^{2}" f = Function('f') assert latex(f(x)) == r'f{\left (x \right )}' assert latex(f) == r'f' g = Function('g') assert latex(g(x, y)) == r'g{\left (x,y \right )}' assert latex(g) == r'g' h = Function('h') assert latex(h(x, y, z)) == r'h{\left (x,y,z \right )}' assert latex(h) == r'h' Li = Function('Li') assert latex(Li) == r'\operatorname{Li}' assert latex(Li(x)) == r'\operatorname{Li}{\left (x \right )}' beta = Function('beta') # not to be confused with the beta function assert latex(beta(x)) == r"\beta{\left (x \right )}" assert latex(beta) == r"\beta" assert latex(sin(x)) == r"\sin{\left (x \right )}" assert latex(sin(x), fold_func_brackets=True) == r"\sin {x}" assert latex(sin(2*x**2), fold_func_brackets=True) == \ r"\sin {2 x^{2}}" assert latex(sin(x**2), fold_func_brackets=True) == \ r"\sin {x^{2}}" assert latex(asin(x)**2) == r"\operatorname{asin}^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="full") == \ r"\arcsin^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="power") == \ r"\sin^{-1}{\left (x \right )}^{2}" assert latex(asin(x**2), inv_trig_style="power", fold_func_brackets=True) == \ r"\sin^{-1} {x^{2}}" assert latex(factorial(k)) == r"k!" assert latex(factorial(-k)) == r"\left(- k\right)!" assert latex(subfactorial(k)) == r"!k" assert latex(subfactorial(-k)) == r"!\left(- k\right)" assert latex(factorial2(k)) == r"k!!" assert latex(factorial2(-k)) == r"\left(- k\right)!!" assert latex(binomial(2, k)) == r"{\binom{2}{k}}" assert latex(FallingFactorial(3, k)) == r"{\left(3\right)}_{\left(k\right)}" assert latex(RisingFactorial(3, k)) == r"{\left(3\right)}^{\left(k\right)}" assert latex(floor(x)) == r"\lfloor{x}\rfloor" assert latex(ceiling(x)) == r"\lceil{x}\rceil" assert latex(Min(x, 2, x**3)) == r"\min\left(2, x, x^{3}\right)" assert latex(Min(x, y)**2) == r"\min\left(x, y\right)^{2}" assert latex(Max(x, 2, x**3)) == r"\max\left(2, x, x^{3}\right)" assert latex(Max(x, y)**2) == r"\max\left(x, y\right)^{2}" assert latex(Abs(x)) == r"\left\lvert{x}\right\rvert" assert latex(re(x)) == r"\Re{x}" assert latex(re(x + y)) == r"\Re{x} + \Re{y}" assert latex(im(x)) == r"\Im{x}" assert latex(conjugate(x)) == r"\overline{x}" assert latex(gamma(x)) == r"\Gamma{\left(x \right)}" w = Wild('w') assert latex(gamma(w)) == r"\Gamma{\left(w \right)}" assert latex(Order(x)) == r"\mathcal{O}\left(x\right)" assert latex(Order(x, x)) == r"\mathcal{O}\left(x\right)" assert latex(Order(x, x, 0)) == r"\mathcal{O}\left(x\right)" assert latex(Order(x, x, oo)) == r"\mathcal{O}\left(x; x\rightarrow\infty\right)" assert latex( Order(x, x, y) ) == r"\mathcal{O}\left(x; \begin{pmatrix}x, & y\end{pmatrix}\rightarrow0\right)" assert latex( Order(x, x, y, 0) ) == r"\mathcal{O}\left(x; \begin{pmatrix}x, & y\end{pmatrix}\rightarrow0\right)" assert latex( Order(x, x, y, oo) ) == r"\mathcal{O}\left(x; \begin{pmatrix}x, & y\end{pmatrix}\rightarrow\infty\right)" assert latex(lowergamma(x, y)) == r'\gamma\left(x, y\right)' assert latex(uppergamma(x, y)) == r'\Gamma\left(x, y\right)' assert latex(cot(x)) == r'\cot{\left (x \right )}' assert latex(coth(x)) == r'\coth{\left (x \right )}' assert latex(re(x)) == r'\Re{x}' assert latex(im(x)) == r'\Im{x}' assert latex(root(x, y)) == r'x^{\frac{1}{y}}' assert latex(arg(x)) == r'\arg{\left (x \right )}' assert latex(zeta(x)) == r'\zeta\left(x\right)' assert latex(zeta(x)) == r"\zeta\left(x\right)" assert latex(zeta(x)**2) == r"\zeta^{2}\left(x\right)" assert latex(zeta(x, y)) == r"\zeta\left(x, y\right)" assert latex(zeta(x, y)**2) == r"\zeta^{2}\left(x, y\right)" assert latex(dirichlet_eta(x)) == r"\eta\left(x\right)" assert latex(dirichlet_eta(x)**2) == r"\eta^{2}\left(x\right)" assert latex(polylog(x, y)) == r"\operatorname{Li}_{x}\left(y\right)" assert latex(polylog(x, y)**2) == r"\operatorname{Li}_{x}^{2}\left(y\right)" assert latex(lerchphi(x, y, n)) == r"\Phi\left(x, y, n\right)" assert latex(lerchphi(x, y, n)**2) == r"\Phi^{2}\left(x, y, n\right)" assert latex(elliptic_k(z)) == r"K\left(z\right)" assert latex(elliptic_k(z)**2) == r"K^{2}\left(z\right)" assert latex(elliptic_f(x, y)) == r"F\left(x\middle| y\right)" assert latex(elliptic_f(x, y)**2) == r"F^{2}\left(x\middle| y\right)" assert latex(elliptic_e(x, y)) == r"E\left(x\middle| y\right)" assert latex(elliptic_e(x, y)**2) == r"E^{2}\left(x\middle| y\right)" assert latex(elliptic_e(z)) == r"E\left(z\right)" assert latex(elliptic_e(z)**2) == r"E^{2}\left(z\right)" assert latex(elliptic_pi(x, y, z)) == r"\Pi\left(x; y\middle| z\right)" assert latex(elliptic_pi(x, y, z)**2) == \ r"\Pi^{2}\left(x; y\middle| z\right)" assert latex(elliptic_pi(x, y)) == r"\Pi\left(x\middle| y\right)" assert latex(elliptic_pi(x, y)**2) == r"\Pi^{2}\left(x\middle| y\right)" assert latex(Ei(x)) == r'\operatorname{Ei}{\left (x \right )}' assert latex(Ei(x)**2) == r'\operatorname{Ei}^{2}{\left (x \right )}' assert latex(expint(x, y)**2) == r'\operatorname{E}_{x}^{2}\left(y\right)' assert latex(Shi(x)**2) == r'\operatorname{Shi}^{2}{\left (x \right )}' assert latex(Si(x)**2) == r'\operatorname{Si}^{2}{\left (x \right )}' assert latex(Ci(x)**2) == r'\operatorname{Ci}^{2}{\left (x \right )}' assert latex(Chi(x)**2) == r'\operatorname{Chi}^{2}{\left (x \right )}' assert latex(Chi(x)) == r'\operatorname{Chi}{\left (x \right )}' assert latex(jacobi(n, a, b, x)) == r'P_{n}^{\left(a,b\right)}\left(x\right)' assert latex(jacobi( n, a, b, x)**2) == r'\left(P_{n}^{\left(a,b\right)}\left(x\right)\right)^{2}' assert latex(gegenbauer(n, a, x)) == r'C_{n}^{\left(a\right)}\left(x\right)' assert latex(gegenbauer( n, a, x)**2) == r'\left(C_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(chebyshevt(n, x)) == r'T_{n}\left(x\right)' assert latex(chebyshevt(n, x)**2) == r'\left(T_{n}\left(x\right)\right)^{2}' assert latex(chebyshevu(n, x)) == r'U_{n}\left(x\right)' assert latex(chebyshevu(n, x)**2) == r'\left(U_{n}\left(x\right)\right)^{2}' assert latex(legendre(n, x)) == r'P_{n}\left(x\right)' assert latex(legendre(n, x)**2) == r'\left(P_{n}\left(x\right)\right)^{2}' assert latex(assoc_legendre(n, a, x)) == r'P_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_legendre( n, a, x)**2) == r'\left(P_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(laguerre(n, x)) == r'L_{n}\left(x\right)' assert latex(laguerre(n, x)**2) == r'\left(L_{n}\left(x\right)\right)^{2}' assert latex(assoc_laguerre(n, a, x)) == r'L_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_laguerre( n, a, x)**2) == r'\left(L_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(hermite(n, x)) == r'H_{n}\left(x\right)' assert latex(hermite(n, x)**2) == r'\left(H_{n}\left(x\right)\right)^{2}' theta = Symbol("theta", real=True) phi = Symbol("phi", real=True) assert latex(Ynm(n, m, theta, phi)) == r'Y_{n}^{m}\left(\theta,\phi\right)' assert latex( Ynm(n, m, theta, phi)**3) == r'\left(Y_{n}^{m}\left(\theta,\phi\right)\right)^{3}' assert latex(Znm(n, m, theta, phi)) == r'Z_{n}^{m}\left(\theta,\phi\right)' assert latex( Znm(n, m, theta, phi)**3) == r'\left(Z_{n}^{m}\left(\theta,\phi\right)\right)^{3}' # Test latex printing of function names with "_" assert latex( polar_lift(0)) == r"\operatorname{polar\_lift}{\left (0 \right )}" assert latex(polar_lift(0)** 3) == r"\operatorname{polar\_lift}^{3}{\left (0 \right )}" assert latex(totient(n)) == r'\phi\left( n \right)' # some unknown function name should get rendered with \operatorname fjlkd = Function('fjlkd') assert latex(fjlkd(x)) == r'\operatorname{fjlkd}{\left (x \right )}' # even when it is referred to without an argument assert latex(fjlkd) == r'\operatorname{fjlkd}'
def R(n): return totient(n)/(n-1)
# -*- coding: utf-8 -*- """ Created on Thu Sep 15 21:01:57 2016 @author: Lucas """ minsofar = 1.0008 import lucaslib import sympy for i in xrange(5000001, 10**7, 2): if sorted(str(sympy.totient(i))) == sorted(str(i)): if i / float(sympy.totient(i)) < minsofar: print i if lucaslib.isprime(i): print "Prime!" print i / float(sympy.totient(i)) print minsofar = i / float(sympy.totient(i))
def max_totient_ratio(N): return max((n for n in range(2, N + 1)), key=lambda n: n / totient(n))
def cum_phi_count(x): total = 0 for i in trange(2, x + 1): total += totient(i) return total
from sympy import totient maxn = 6 maxq = 3 for i in range(2, 1000000): q = i / totient(i) if q > maxq: maxq = q maxn = i print("{} / {} = {}".format(maxn, totient(maxn), maxq))
def min_permutated_totient_ratio(N): return min((n for n in tqdm(range(2, N + 1)) if sorted(str(n)) == sorted(str(totient(n)))), key=lambda n: n / totient(n))
def test_F9(): assert totient(1776) == 576
def count_totients_div_4(max_n): count = 0 for x in range(2, max_n): if sp.totient(x) % 4 == 0: count += 1 print("Totients divis by 4 up to {}: {}".format(max_n, count))
def test_latex_functions(): assert latex(exp(x)) == "e^{x}" assert latex(exp(1) + exp(2)) == "e + e^{2}" f = Function('f') assert latex(f(x)) == r'f{\left (x \right )}' assert latex(f) == r'f' g = Function('g') assert latex(g(x, y)) == r'g{\left (x,y \right )}' assert latex(g) == r'g' h = Function('h') assert latex(h(x, y, z)) == r'h{\left (x,y,z \right )}' assert latex(h) == r'h' Li = Function('Li') assert latex(Li) == r'\operatorname{Li}' assert latex(Li(x)) == r'\operatorname{Li}{\left (x \right )}' beta = Function('beta') # not to be confused with the beta function assert latex(beta(x)) == r"\beta{\left (x \right )}" assert latex(beta) == r"\beta" assert latex(sin(x)) == r"\sin{\left (x \right )}" assert latex(sin(x), fold_func_brackets=True) == r"\sin {x}" assert latex(sin(2*x**2), fold_func_brackets=True) == \ r"\sin {2 x^{2}}" assert latex(sin(x**2), fold_func_brackets=True) == \ r"\sin {x^{2}}" assert latex(asin(x)**2) == r"\operatorname{asin}^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="full") == \ r"\arcsin^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="power") == \ r"\sin^{-1}{\left (x \right )}^{2}" assert latex(asin(x**2), inv_trig_style="power", fold_func_brackets=True) == \ r"\sin^{-1} {x^{2}}" assert latex(factorial(k)) == r"k!" assert latex(factorial(-k)) == r"\left(- k\right)!" assert latex(subfactorial(k)) == r"!k" assert latex(subfactorial(-k)) == r"!\left(- k\right)" assert latex(factorial2(k)) == r"k!!" assert latex(factorial2(-k)) == r"\left(- k\right)!!" assert latex(binomial(2, k)) == r"{\binom{2}{k}}" assert latex( FallingFactorial(3, k)) == r"{\left(3\right)}_{\left(k\right)}" assert latex(RisingFactorial(3, k)) == r"{\left(3\right)}^{\left(k\right)}" assert latex(floor(x)) == r"\lfloor{x}\rfloor" assert latex(ceiling(x)) == r"\lceil{x}\rceil" assert latex(Min(x, 2, x**3)) == r"\min\left(2, x, x^{3}\right)" assert latex(Min(x, y)**2) == r"\min\left(x, y\right)^{2}" assert latex(Max(x, 2, x**3)) == r"\max\left(2, x, x^{3}\right)" assert latex(Max(x, y)**2) == r"\max\left(x, y\right)^{2}" assert latex(Abs(x)) == r"\left\lvert{x}\right\rvert" assert latex(re(x)) == r"\Re{x}" assert latex(re(x + y)) == r"\Re{x} + \Re{y}" assert latex(im(x)) == r"\Im{x}" assert latex(conjugate(x)) == r"\overline{x}" assert latex(gamma(x)) == r"\Gamma\left(x\right)" assert latex(Order(x)) == r"\mathcal{O}\left(x\right)" assert latex(lowergamma(x, y)) == r'\gamma\left(x, y\right)' assert latex(uppergamma(x, y)) == r'\Gamma\left(x, y\right)' assert latex(cot(x)) == r'\cot{\left (x \right )}' assert latex(coth(x)) == r'\coth{\left (x \right )}' assert latex(re(x)) == r'\Re{x}' assert latex(im(x)) == r'\Im{x}' assert latex(root(x, y)) == r'x^{\frac{1}{y}}' assert latex(arg(x)) == r'\arg{\left (x \right )}' assert latex(zeta(x)) == r'\zeta\left(x\right)' assert latex(zeta(x)) == r"\zeta\left(x\right)" assert latex(zeta(x)**2) == r"\zeta^{2}\left(x\right)" assert latex(zeta(x, y)) == r"\zeta\left(x, y\right)" assert latex(zeta(x, y)**2) == r"\zeta^{2}\left(x, y\right)" assert latex(dirichlet_eta(x)) == r"\eta\left(x\right)" assert latex(dirichlet_eta(x)**2) == r"\eta^{2}\left(x\right)" assert latex(polylog(x, y)) == r"\operatorname{Li}_{x}\left(y\right)" assert latex( polylog(x, y)**2) == r"\operatorname{Li}_{x}^{2}\left(y\right)" assert latex(lerchphi(x, y, n)) == r"\Phi\left(x, y, n\right)" assert latex(lerchphi(x, y, n)**2) == r"\Phi^{2}\left(x, y, n\right)" assert latex(elliptic_k(z)) == r"K\left(z\right)" assert latex(elliptic_k(z)**2) == r"K^{2}\left(z\right)" assert latex(elliptic_f(x, y)) == r"F\left(x\middle| y\right)" assert latex(elliptic_f(x, y)**2) == r"F^{2}\left(x\middle| y\right)" assert latex(elliptic_e(x, y)) == r"E\left(x\middle| y\right)" assert latex(elliptic_e(x, y)**2) == r"E^{2}\left(x\middle| y\right)" assert latex(elliptic_e(z)) == r"E\left(z\right)" assert latex(elliptic_e(z)**2) == r"E^{2}\left(z\right)" assert latex(elliptic_pi(x, y, z)) == r"\Pi\left(x; y\middle| z\right)" assert latex(elliptic_pi(x, y, z)**2) == \ r"\Pi^{2}\left(x; y\middle| z\right)" assert latex(elliptic_pi(x, y)) == r"\Pi\left(x\middle| y\right)" assert latex(elliptic_pi(x, y)**2) == r"\Pi^{2}\left(x\middle| y\right)" assert latex(Ei(x)) == r'\operatorname{Ei}{\left (x \right )}' assert latex(Ei(x)**2) == r'\operatorname{Ei}^{2}{\left (x \right )}' assert latex(expint(x, y)**2) == r'\operatorname{E}_{x}^{2}\left(y\right)' assert latex(Shi(x)**2) == r'\operatorname{Shi}^{2}{\left (x \right )}' assert latex(Si(x)**2) == r'\operatorname{Si}^{2}{\left (x \right )}' assert latex(Ci(x)**2) == r'\operatorname{Ci}^{2}{\left (x \right )}' assert latex(Chi(x)**2) == r'\operatorname{Chi}^{2}{\left (x \right )}', latex(Chi(x)**2) assert latex( jacobi(n, a, b, x)) == r'P_{n}^{\left(a,b\right)}\left(x\right)' assert latex(jacobi(n, a, b, x)**2) == r'\left(P_{n}^{\left(a,b\right)}\left(x\right)\right)^{2}' assert latex( gegenbauer(n, a, x)) == r'C_{n}^{\left(a\right)}\left(x\right)' assert latex(gegenbauer(n, a, x)**2) == r'\left(C_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(chebyshevt(n, x)) == r'T_{n}\left(x\right)' assert latex( chebyshevt(n, x)**2) == r'\left(T_{n}\left(x\right)\right)^{2}' assert latex(chebyshevu(n, x)) == r'U_{n}\left(x\right)' assert latex( chebyshevu(n, x)**2) == r'\left(U_{n}\left(x\right)\right)^{2}' assert latex(legendre(n, x)) == r'P_{n}\left(x\right)' assert latex(legendre(n, x)**2) == r'\left(P_{n}\left(x\right)\right)^{2}' assert latex( assoc_legendre(n, a, x)) == r'P_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_legendre(n, a, x)**2) == r'\left(P_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(laguerre(n, x)) == r'L_{n}\left(x\right)' assert latex(laguerre(n, x)**2) == r'\left(L_{n}\left(x\right)\right)^{2}' assert latex( assoc_laguerre(n, a, x)) == r'L_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_laguerre(n, a, x)**2) == r'\left(L_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(hermite(n, x)) == r'H_{n}\left(x\right)' assert latex(hermite(n, x)**2) == r'\left(H_{n}\left(x\right)\right)^{2}' theta = Symbol("theta", real=True) phi = Symbol("phi", real=True) assert latex(Ynm(n,m,theta,phi)) == r'Y_{n}^{m}\left(\theta,\phi\right)' assert latex(Ynm(n, m, theta, phi)**3) == r'\left(Y_{n}^{m}\left(\theta,\phi\right)\right)^{3}' assert latex(Znm(n,m,theta,phi)) == r'Z_{n}^{m}\left(\theta,\phi\right)' assert latex(Znm(n, m, theta, phi)**3) == r'\left(Z_{n}^{m}\left(\theta,\phi\right)\right)^{3}' # Test latex printing of function names with "_" assert latex( polar_lift(0)) == r"\operatorname{polar\_lift}{\left (0 \right )}" assert latex(polar_lift( 0)**3) == r"\operatorname{polar\_lift}^{3}{\left (0 \right )}" assert latex(totient(n)) == r'\phi\left( n \right)' # some unknown function name should get rendered with \operatorname fjlkd = Function('fjlkd') assert latex(fjlkd(x)) == r'\operatorname{fjlkd}{\left (x \right )}' # even when it is referred to without an argument assert latex(fjlkd) == r'\operatorname{fjlkd}'
# -*- coding: utf-8 -*- """ Created on Sun Sep 25 11:24:32 2016 @author: Lucas """ import lucaslib import math import sympy answer = 0 for i in xrange(2, 1000001): answer += sympy.totient(i) print answer
#!/usr/bin/python3 # -*- coding: utf-8 -*- # https://projecteuler.net/problem=70 from sympy import totient LIMIT = 10**7 oran = 42 for n in range(2, LIMIT): to = totient(n) for p in set(str(n)): if (str(n).count(p) != str(to).count(p)): break else: if (set(str(n)) == set(str(to))): if (len(set(str(n))) == len(set(str(to)))): if (n / to < oran): oran = n / to enkucuk = n print(enkucuk)
__author__ = 'Prateek' from coPrime import coPrime from sympy import totient from elementOrder import order def generators(n): list = [] for elt in coPrime(n): if order(n, elt) == totient(n): list.append(elt) return list if __author__ == 'Prateek': n = 13 print "Number of generators for",13," are",totient(totient(n)) print generators(n)
def generators(n): list = [] for elt in coPrime(n): if order(n, elt) == totient(n): list.append(elt) return list
import sympy from sympy import totient from sympy import reduced_totient import time start = time.time() N = 2019 #показатель a = 2019 #основание z = 10**2019 tot = totient(z) i = 0 aa = a f = open('2019.txt', 'w') for i in range(N - 1): r = aa % tot f. write("r = "+str(r) + "\n") print("r = "+str(r)) aa = pow(a,r,z) f. write("a = "+str(aa) + "\n") print("a = " + str(aa)) f. write("Ответ: "+str(aa) + "\n") print(aa) print(- start + time.time()) f. write("Время: "+str(- start + time.time()) + "\n")
def phi(n): """Calculates the value of phi(n), the number of positive integers less than n and relatively prime to n.""" return sympy.totient(n)