Compare commits
No commits in common. "bfac2b9433f6c3f777b18ce4eff0a5313505b963" and "d6fad1511c20ced0e3c6acc71a28bb71046f2c92" have entirely different histories.
bfac2b9433
...
d6fad1511c
@ -1,20 +1,12 @@
|
|||||||
def factorial(n: int, start: int = 1) -> int:
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
def factorial(n: int) -> int:
|
||||||
"""
|
"""
|
||||||
n! = 1 * 2 * 3 * 4 * ... * n
|
n! = 1 * 2 * 3 * 4 * ... * n
|
||||||
1, 1, 2, 6, 24, 120, 720, ...
|
1, 1, 2, 6, 24, 120, 720, ...
|
||||||
|
|
||||||
If you're looking for efficiency with start == 1, just use math.factorial(n)
|
|
||||||
which takes just 25% of the compute time on average, but this is the fastest
|
|
||||||
pure python implementation I could come up with and it allows for partial
|
|
||||||
multiplications, like 5 * 6 * 7 * 8 * .... * 17
|
|
||||||
"""
|
"""
|
||||||
if start == n:
|
return math.factorial(n)
|
||||||
return n
|
|
||||||
if n - start == 1:
|
|
||||||
return n * start
|
|
||||||
|
|
||||||
middle = start + (n - start) // 2
|
|
||||||
return factorial(middle, start) * factorial(n, middle + 1)
|
|
||||||
|
|
||||||
|
|
||||||
def fibonacci(n: int) -> int:
|
def fibonacci(n: int) -> int:
|
||||||
@ -46,10 +38,3 @@ def pentagonal(n: int) -> int:
|
|||||||
0, 1, 5, 12, 22, 35, ...
|
0, 1, 5, 12, 22, 35, ...
|
||||||
"""
|
"""
|
||||||
return ((3 * n * n) - n) // 2
|
return ((3 * n * n) - n) // 2
|
||||||
|
|
||||||
|
|
||||||
def hexagonal(n: int) -> int:
|
|
||||||
if n == 1:
|
|
||||||
return 1
|
|
||||||
|
|
||||||
return n * 2 + (n - 1) * 2 + (n - 2) * 2 + hexagonal(n - 1)
|
|
||||||
|
|||||||
@ -20,9 +20,7 @@ class StopWatch:
|
|||||||
self.stopped = perf_counter_ns()
|
self.stopped = perf_counter_ns()
|
||||||
self.total_elapsed += self.elapsed()
|
self.total_elapsed += self.elapsed()
|
||||||
|
|
||||||
def reset(self):
|
reset = start
|
||||||
self.total_elapsed = 0
|
|
||||||
self.start()
|
|
||||||
|
|
||||||
def elapsed(self) -> int:
|
def elapsed(self) -> int:
|
||||||
if self.stopped is None:
|
if self.stopped is None:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user