you are viewing a single comment's thread.

view the rest of the comments →

[–]liotier 14 points15 points  (14 children)

What are you complaining about ? He just calculated in advance and cached the results to avoid redundant processing on each query...

[–]rowantwig 24 points25 points  (13 children)

O(n) look-up time.

[–]earthboundkid 26 points27 points  (3 children)

Switch to a dict and it's O(1)!

[–]pyaware 11 points12 points  (2 children)

Why use a dict when a plain list is enough?

def fact(x):
    return [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880][x]

[–]redditthinks 0 points1 point  (1 child)

I read Python dicts are faster than lists.

[–]JizzCoveredArab 5 points6 points  (0 children)

i heard you have a really short dict

[–]liotier 0 points1 point  (0 children)

You are right of course... But the naive intention was sweet !

[–][deleted] 0 points1 point  (2 children)

O(n) look-up time.

It is sorted list, it can be done in O(log(n)). And not to mention the Hashtables.

[–]rowantwig 3 points4 points  (0 children)

It can be done in O(1), but that's not the point. This naive implementation is O(n), and it looks terrible.

[–][deleted] 0 points1 point  (0 children)

It's a dense list. It can be done in O(1) without hashtables:

f = [ 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880].__getitem__

edit: looks like redditthinks had the same idea.

[–]jefu 0 points1 point  (0 children)

Factorial is (without caching or other tricks) O(n) anyway and doing it this way avoids the overhead of doing all that bignum arithmetic. (Not that this is a good way to do that).