Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/combinat/partitions.pyx
4068 views
1
"""
2
Number of partitions of integer
3
4
AUTHOR:
5
-- William Stein (2007-07-28): initial version
6
-- Jonathan Bober (2007-07-28): wrote the program partitions_c.cc
7
that does all the actual heavy lifting.
8
"""
9
10
import sys
11
12
cdef extern from "gmp.h":
13
ctypedef void* mpz_t
14
15
cdef extern from "partitions_c.h":
16
int part(mpz_t answer, unsigned int n)
17
int test(bint longtest, bint forever)
18
19
include "../ext/interrupt.pxi"
20
21
from sage.rings.integer cimport Integer
22
23
def number_of_partitions(n):
24
"""
25
Returns the number of partitions of the integer n.
26
27
EXAMPLES:
28
sage: from sage.combinat.partitions import number_of_partitions
29
sage: number_of_partitions(10)
30
42
31
"""
32
n = Integer(n)
33
if n <= 0:
34
return Integer(0)
35
elif n == 1:
36
return Integer(1) # part hangs on n=1 as input.
37
if n >= Integer('4294967296'):
38
raise ValueError, "input must be a nonnegative integer less than 4294967296."
39
cdef unsigned int nn = n
40
41
cdef Integer ans = Integer(0)
42
43
sig_on()
44
part(ans.value, nn)
45
sig_off()
46
47
return ans
48
49
def run_tests(bint longtest=False, bint forever=False):
50
sig_on()
51
error = test(longtest, forever)
52
sig_off()
53
print "Done."
54
if error:
55
return error
56
57