Sagebrush · Examples · Notebooks

Sage

Sage mode: ^ is a power, division is exact; harmonic numbers, factoring and primes.

notebook · Sage · intro · by Sagebrush · MIT license

basicsnumber theory

Download (ZIP)Source on GitHub

Preview

# Sage mode: ^ is a power and integer division is exact
2^100, 2/3 + 1/6, (2/3)^-2
(1267650600228229401496703205376, 5/6, 9/4)
# Harmonic numbers, exactly
H = sum(1/n for n in range(1, 21))
H, n(H)
(55835135/15519504, 3.59773965714368)
factor(2^64 - 1), factor(2^67 - 1)
(3 * 5 * 17 * 257 * 641 * 65537 * 6700417, 193707721 * 761838257287)
p = next_prime(10^30)
p, is_prime(p), is_prime(p + 2)
(1000000000000000000000000000057, True, False)
divisors(360), euler_phi(360), sigma(28), crt(2, 3, 5, 7)
([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 18, 20, 24, 30, 36, 40, 45, 60, 72, 90, 120, 180, 360], 96, 56, 17)
# The Mersenne exponents below 1000, by Lucas-Lehmer
def lucas_lehmer(p):
    s, M = 4, 2^p - 1
    for _ in range(p - 2):
        s = (s^2 - 2) % M
    return s == 0

[p for p in prime_range(3, 1000) if lucas_lehmer(p)]
[3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607]

Files

Open the ZIP's notebook in Sagebrush (File, Open) to run and change it.