<jupyter_start><jupyter_text><jupyter_code><jupyter_output><empty_output><jupyter_text>sympy.ntheory に脇道https://docs.sympy.org/latest/modules/ntheory.htmlmodule-sympy.ntheory.modular<jupyter_code># エラトステネスの篩
from sympy import sieve
sieve._reset()
display(25 in sieve) # 25 は素数表 sieve にあるか => False
display(sieve) # 単純な素数表ではないのだ!!!!
display(type(sieve))
display(type(sieve._list))
display(sieve._list)
display(sieve[3])
display([i for i in sieve._list])
# same as 30 in sieve without return
from sympy import sieve
sieve._reset()
sieve.extend(30)
display(sieve[3]==5)
display(sieve._list)
# nth prime
from sympy import sieve, prime
sieve._reset()
n=20
sieve.extend_to_no(n)
display(sieve[n])
display(prime(n))
display(sieve._list)
# isprime
from sympy import sieve,prime,isprime
sieve._reset()
display(isprime(1299709))
display(sieve._list)
display(sieve[10])
display(sieve._list)
# mobiusrange(a,b) # b is outside of range
# 下の例では 7,8,9,10,11,12,13,14,15,16,17 についてメビウス関数の値を示す
# メビウス関数は素因数分解して中に平方を含んでいれば 0 => 8,9,12,16
# 因子が奇数個の場合は -1 => 7,11,13,17
# 因子が偶数個の場合は 1 => 10,14,15
# 従って素数とは関係ない
from sympy import sieve
print([i for i in sieve.mobiusrange(7, 18)])

# primerange
from sympy import sieve
print([i for i in sieve.primerange(7, 18)])
# primerange と同じものを書いてみる
from sympy import sieve
n=30
sieve.extend_to_no(n)
display([i for i in sieve._list if (i >= 7) & (i < 18)])
# search 与えられた値の上下の素数を tuple で返す
from sympy import sieve
n = 8
display(sieve.search(n))
a, b = sieve.search(n)
display(sieve[a], sieve[b])<jupyter_output><empty_output><jupyter_text>totientオイラーのトーシェント関数とは、正の整数 $n$ に対して、 $n$ と互いに素である 1 以上 $n$ 以下の自然数の個数 $\varphi (n)$ を与える数論的関数 $\varphi$ である<jupyter_code># totientrange range について totient 数を返す
# 素数の場合 n-1 個になる
from sympy import sieve
print([i for i in sieve.totientrange(7, 18)])
# nth prime n番目の素数
from sympy import prime
display(prime(10))
display(prime(1))
display(prime(100000))

#primepi n を含み n より小さい素数の数 primepi(10) => 2,3,5,7 => 4 個
from sympy import *
n = 10
primepi(n)
# 対数積分 logarithmic integral function オイラーの対数積分
from sympy import *
n = 10
print(li(n))
li(n).evalf()
# nextprime
from sympy import nextprime
[(i,nextprime(i)) for i in range (10,15)]
# prevprime
from sympy import prevprime
[(i,prevprime(i)) for i in range (10,15)]
# primerange, sieve
from sympy import primerange, sieve
print([i for i in primerange(1,30)])
print([i for i in sieve.primerange(1,30)])
print(list(sieve.primerange(1,30)))

# randprime, isprime
from sympy import randprime, isprime
print(randprime(1,30))
print(isprime(randprime(1,30)))
from sympy.ntheory.generate import primorial, primerange
from sympy import factorint, Mul, primefactors, sqrt
print(primorial(4)) # the first 4 primes are 2, 3, 5, 7
print(primorial(4, nth=False)) # primes <= 4 are 2 and 3
print(primorial(1))
print(primorial(1, nth=False))
print(sqrt(101).evalf())
print(primorial(sqrt(101), nth=False))
print(factorint(primorial(4) + 1))
print(factorint(210))
print(factorint(primorial(4) - 1))
p = list(primerange(10, 20)) # generate all primes in a given range
print(p)
print(sorted(set(primefactors(Mul(*p) + 1)).difference(set(p))))

from sympy.ntheory.generate import cycle_length
func = lambda i: (i**2 + 1) % 51
print(next(cycle_length(func, 4)))
n = cycle_length(func, 4, values=True)
print(list(ni for ni in n))
print(next(cycle_length(func,4,nmax=4)))
print([ni for ni in cycle_length(func,4,nmax=4,values=True)])
from sympy import composite
print(composite(36))
print(composite(1))
print(composite(17737))

from sympy import compositepi
print(compositepi(25))
print(compositepi(1))
print(compositepi(1000))
from sympy.ntheory.factor_  import smoothness
print(smoothness(2**7*3**2))
print(smoothness(2**4*13))
print(smoothness(2))
from sympy.ntheory.factor_  import smoothness_p
print(smoothness_p(10431, m=1))
print(smoothness_p(10431))
print(smoothness_p(10431,power=1))
print(smoothness_p(21477639576571, visual=1))
print(factorint(17*9))
print(smoothness_p(factorint(17*9)))
print(smoothness_p(smoothness_p(factorint(17*9))))
from sympy import trailing
print(trailing(128))
print(trailing(63))

from sympy.ntheory import multiplicity
from sympy.core.numbers import Rational as R
print([multiplicity(5, n) for n in [8, 5, 25, 125, 250]])
print(multiplicity(3, R(1, 9)))<jupyter_output>[0, 1, 2, 3, 3]
-2
{'fraction_non_alphanumeric': 0.14332171893147502, 'fraction_numerical': 0.05876887340301974, 'mean_word_length': 7.344961240310077, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '201624', 'license': 'MIT', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1840, 'n_words': 405, 'path': 'sympy_ntheory.ipynb', 'repo_name': 'kalz2q/myjupyternotebooks', 'size': '4305'}