Sage Demo
Sage is:
- A large free open source mathematics software project that I direct.
- Web-based at http://sagenb.org or you can run it locally.
- Very powerful for number theory, and also combinatorics, graph theory, and numerical analysis (via scipy).
- Uses Python for the main user language.
{{{id=11|
///
}}}
GCD
{{{id=13|
gcd(2010,2007)
///
3
}}}
{{{id=14|
gcd([15,25,20,45])
///
5
}}}
{{{id=10|
gcd(90324809238420398423230948203948, 2903480923840923849082309482)
///
6
}}}
How to make up huge random integers:
{{{id=16|
n = ZZ.random_element(10^10000); m = ZZ.random_element(10^10000)
len(n.digits())
///
10000
}}}
{{{id=15|
gcd(n,m)
///
1
}}}
{{{id=18|
///
}}}
Enumerating Primes
{{{id=6|
prime_range(50)
///
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
}}}
{{{id=5|
prime_range(200)
///
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
}}}
{{{id=4|
@interact
def f(n=(10..2500)):
print prime_range(n)
///
}}}
{{{id=21|
///
}}}
{{{id=3|
///
}}}
Sieving Primes
{{{id=1|
@interact
def f(n=(2..400), pmax=(2..20)):
P = [2]
X = [3,5,..,n]
while True:
if len(X) == 0: break
p = X[0]
if p > pmax:
P.extend(X); break
P.append(p)
X = [a for a in X if a%p]
print ""
for a in [1..n]:
clr = '#a00' if a in P else '#aaf'
print "%s"%(clr, ' '*(3-len(str(a)))+str(a)),
if a%20 == 0:
print
print ""
///
}}}
{{{id=24|
///
}}}
Mersenne Primes
The prime below is the largest known prime. The GIMPS project found it, winning them a \$100,000 prize from the EFF, since it has > ten million digits.
{{{id=26|
time p = 2^43112609 - 1
///
Time: CPU 0.01 s, Wall: 0.01 s
}}}
{{{id=27|
time v = p.digits()
///
Time: CPU 14.40 s, Wall: 14.49 s
}}}
{{{id=28|
len(v)
///
12978189
}}}
Last 10 digits:
{{{id=22|
v[-10:]
///
[3, 9, 6, 2, 0, 7, 4, 6, 1, 3]
}}}
First 10 digits:
{{{id=32|
v[:10]
///
[1, 1, 5, 2, 5, 1, 7, 9, 6, 6]
}}}
Frequency histogram of first $n$ digits:
{{{id=36|
@interact
def f(n=(10,100,..10^5)):
time T = finance.TimeSeries(v[:n])
show(T.plot_histogram())
///
}}}
Counting Primes
{{{id=33|
prime_pi
///
Function that counts the number of primes up to x
}}}
{{{id=40|
prime_pi(10)
///
4
}}}
{{{id=41|
for n in [10,100,1000,10000,100000]:
print n, prime_pi(n)
///
10 4
100 25
1000 168
10000 1229
100000 9592
}}}
{{{id=42|
@interact
def f(n=(10..5000)):
show(plot(prime_pi, 1, n))
///
}}}
{{{id=45|
///
}}}
Wait, that looks like a nice clean smooth curve? What is it "basically" a plot of?
The Riemann Hypothesis is a conjectural answer to this question...
{{{id=44|
///
}}}
{{{id=43|
///
}}}