-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a factorial function #46391
Comments
Add a factorial method. Everybody understands what it means before print n.factorial() Maybe raise ValueError if n is negative. |
It seems like most of the methods on integers are for two-argument I have to say a factorial function is something I've found myself |
I don't think it would be appropriate to add this as a method of int; Perhaps a math.factorial function could be considered? Historically, David, any chance you could come up with a patch implementing |
Except that hypot is not a one-liner, if you want to get edge cases |
Ah, true; thanks for pointing that out. Should there be some upper limit on the argument math.factorial would |
I'd say not. Any such limit would be artificial, and an arbitrary |
Since the domain and range are ints/longs, this makes more sense as a IIRC, Tim wanted the math module to maintain that theme and not become |
Fair enough. Raymond, do you have any thoughts on where a gcd |
gcd() is probably fine where it is. People learn about GCD and LCM |
The other issue here is that I see factorial as being the thin end of So it seems to me that either there should be a good single place for |
I wouldn't worry about that so much -- our job is to provide good |
Mr. Dickinson thank you for doing this. I do not know how to help with def factorial(n, _known=[1]):
assert isinstance(n, int), "Need an integer. This isn't a gamma"
assert n >= 0, "Sorry, can't factorilize a negative"
assert n < 1000, "No way! That's too large"
try:
return _known[n]
except IndexError:
pass
for i in range(len(_known), n+1):
_known.append(_known[-1] * i)
return _known[n] When the assertions are turned-off, this runs pretty fast. |
I like the idea of having some integer math functions like this in the http://bugs.python.org/issue457066 has some discussion of xgcd and |
Would it be implemented in C? How about using Luschny's Prime Swing |
IMHO, The best place to put functions such as xgcd, factorial, etc, Not only would it keep the standard math module clean, it would also |
The trouble is that this comes at a time when people are trying to trim Perhaps the solution is a high-quality third party 'imath' module? |
Yeah, I like the idea of a third party module and letting the popularity This would also make it easier to see what kind of functionality people |
The factorial function is most likely to be used in context with other |
FWIW, I don't agree with the reasoning on the rejection. Hundreds of The OP requested a simple, widely understood integer method with no This is a re-invented function and it would be ashamed to not offer it Eventhough this is re-invented often, it is not often re-invented well To compare, I checked the somewhat clean SmallTalk Integer API and found Re-opening for further discussion. If someone still feels that it is a |
Raymond: Can you come into the core sprint and discuss it with the table |
Wish I could be at the sprint. I'm back in Los Angeles. My little post I thought it was a reasonable request and would hate to see it killed |
Rather than factorial how about a product function, so factorial(n) = |
Problems with product(): It is dreadfully inefficient compared to a |
I'm not opposed to adding factorial somewhere, and it doesn't seem There are other integer -> integer functions that I'd consider just And supposing that gcd were added some day, shouldn't it be in the As to implementation, I'd probably avoid PrimeSwing on the basis I can volunteer to try to implement this sometime before the 2.6/3.0 |
I prefer factorial as a method (like Ruby and Smalltalk). Given the Compared to numbits() and isqrt(), a factorial() method is more basic FWIW, if separate RFEs were opened for numbits() and isqrt(), I would |
The fact that other languages have factorial does not in itself impress I don't really like the idea of making factorial a method for the same |
It's a simplified version, but why not something like this: import operator
def factorial(num):
return reduce(operator.mul, range(1, num+1)) A product() function could also be done similarly. |
Or slightly better: from operator import mul
def factorial(num):
return reduce(mul, range(2, num+1), 1) |
Contrary to what I said above, I'm not going to find time for this A simple implementation would be fine---the algorithm could be tweaked |
I've got in from here. |
Added math.factorial() in r64050. |
It looks like there's a refcounting bug in the code: if the call to Python 2.6a3+ (trunk:64341M, Jun 17 2008, 13:19:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import factorial
[36374 refs]
>>> factorial(10**9)
^CFatal Python error:
/Users/dickinsm/python_source/trunk/Modules/mathmodule.c:562 object at
0x81f63c has negative ref count -1
Abort trap |
That was fixed by Raymond in 64365. |
I think the unit test is wrong, or at least not as is intended: You put some numbers in values and shuffle them. But you don't use them |
maix: good point. Fixed in revisions r81126 through r81129. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: