# 初等数论¶

## 取模幂¶

sage: R = Integers(97)
sage: a = R(51)
sage: a^2006
12


sage: 51.powermod(99203843984,97)
96


## 离散日志¶

sage: r = Integers(125)
sage: b = r.multiplicative_generator()^3
sage: a = b^17
sage: a.log(b)
17


sage: FF = FiniteField(16,"a")
sage: a = FF.gen()
sage: c = a^7
sage: c.log(a)
7


## 素数¶

sage: 2^(2^12)+1 in Primes()
False
sage: 11 in Primes()
True


sage: next_prime(2005)
2011


sage: pari(10).primepi()
4


sage: primes_first_n(5)
[2, 3, 5, 7, 11]
sage: list(primes(1, 10))
[2, 3, 5, 7]


## 约数¶

Sage使用 divisors(n) 的除数列表 nnumber_of_divisors(n) 的除数 nsigma(n,k) 对于 k -除数的次幂 n （所以 number_of_divisors(n)sigma(n,0) 相同）。

sage: divisors(28); sum(divisors(28)); 2*28
[1, 2, 4, 7, 14, 28]
56
56
sage: sigma(28,0); sigma(28,1); sigma(28,2)
6
56
1050


## 二次剩余¶

sage: Q = quadratic_residues(23); Q
[0, 1, 2, 3, 4, 6, 8, 9, 12, 13, 16, 18]
sage: N = [x for x in range(22) if kronecker(x,23)==-1]; N
[5, 7, 10, 11, 14, 15, 17, 19, 20, 21]


Q是mod 23的二次剩余集，N是非剩余集。

sage: [x for x in range(22) if kronecker(x,23)==1]
[1, 2, 3, 4, 6, 8, 9, 12, 13, 16, 18]
sage: [x for x in range(22) if kronecker(x,23)==-1]
[5, 7, 10, 11, 14, 15, 17, 19, 20, 21]