Skip to content

Instantly share code, notes, and snippets.

@yoniLavi
Last active June 3, 2023 17:46
Show Gist options
  • Select an option

  • Save yoniLavi/ebc70eb8a32ea7010868bce46cb4481e to your computer and use it in GitHub Desktop.

Select an option

Save yoniLavi/ebc70eb8a32ea7010868bce46cb4481e to your computer and use it in GitHub Desktop.
def factorial_row(n, length):
"""Create 1d array whose product is the factorial of n, padded to length"""
return np.pad(np.arange(1, n + 1), (0, length - n), 'constant', constant_values=1)
def sum_factorial(nums):
"""Sum the factorials of the numbers in nums."""
factorial_matrix = np.vstack([factorial_row(n, max(nums)) for n in nums])
return factorial_matrix.prod(axis=1).sum()
sum_factorial([2, 3]) # Returns 8 (2! + 3!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment