@@ -15,6 +15,14 @@ def factorial(x, start=1):
1515
1616 This differs from the built-in `math.factorial` in that it allows a `start`
1717 argument. If one is given, the function returns `(x!)/(start!)`.
18+
19+ Examples:
20+
21+ >>> factorial(5)
22+ 120
23+ >>> factorial(5, 3)
24+ 60
25+
1826 '''
1927 from python_toolbox import misc_tools
2028 return misc_tools .general_product (range (start , x + 1 ), start = 1 )
@@ -27,6 +35,14 @@ def inverse_factorial(number, round_up=True):
2735 If `number` isn't a factorial of an integer, the result will be rounded. By
2836 default it'll be rounded up, but you can specify `round_up=False` to have
2937 it be rounded down.
38+
39+ Examples:
40+
41+ >>> inverse_factorial(100)
42+ 5
43+ >>> inverse_factorial(100, round_up=False)
44+ 4
45+
3046 '''
3147 assert number >= 0
3248 if number == 0 :
@@ -47,11 +63,16 @@ def inverse_factorial(number, round_up=True):
4763
4864def from_factoradic (factoradic_number ):
4965 '''
50- Get the integer that the factorial of would be `number`.
66+ Convert a factoradic representation to the number it's representing.
67+
68+ Read about factoradic numbers here:
69+ https://en.wikipedia.org/wiki/Factorial_number_system
70+
71+ Example:
72+
73+ >>> from_factoradic((4, 0, 2, 0, 0))
74+ 100
5175
52- If `number` isn't a factorial of an integer, the result will be rounded. By
53- default it'll be rounded up, but you can specify `round_up=False` to have
54- it be rounded down.
5576 '''
5677 from python_toolbox import sequence_tools
5778 assert isinstance (factoradic_number , collections .Iterable )
@@ -65,6 +86,24 @@ def from_factoradic(factoradic_number):
6586
6687
6788def to_factoradic (number , n_digits_pad = None ):
89+ '''
90+ Convert a number to factoradic representation (in a tuple.)
91+
92+ Read about factoradic numbers here:
93+ https://en.wikipedia.org/wiki/Factorial_number_system
94+
95+ Example:
96+
97+ >>> to_factoradic(100)
98+ (4, 0, 2, 0, 0)
99+
100+
101+ Use `n_digits_pad` if you want to have the result padded with zeroes:
102+
103+ >>> to_factoradic(100, n_digits_pad=7)
104+ (0, 0, 4, 0, 2, 0, 0)
105+
106+ '''
68107 assert isinstance (number , int )
69108 assert number >= 0
70109 n_digits = inverse_factorial (number , round_up = False ) + 1
0 commit comments