Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created January 14, 2021 02:37
Show Gist options
  • Save ZechCodes/3be96db73274c78c076a35e8ec9177f0 to your computer and use it in GitHub Desktop.
Save ZechCodes/3be96db73274c78c076a35e8ec9177f0 to your computer and use it in GitHub Desktop.
Challenge 165 - Double Factorial

Challenge 165 - Double Factorial

Create a function that takes a number num and returns its double factorial. Mathematically, the formulas for double factorial are as follows.

num !! = num ( num - 2)( num - 4)( num - 6) ... (4)(2)

If num == 0 or num == -1, then num !! == 1 by convention.

Examples

double_factorial(0) ➞ 1

double_factorial(2) ➞ 2

double_factorial(9) ➞ 945

double_factorial(14) ➞ 645120

Notes

  • Assume all input values are greater than or equal to -1.
  • Double factorial is not the same as factorial * 2.
import unittest
def double_factorial(num: int) -> int:
return 0 # Put your code here!!!
class Test(unittest.TestCase):
def test_1(self):
self.assertEqual(double_factorial(-1), 1)
def test_2(self):
self.assertEqual(double_factorial(0), 1)
def test_3(self):
self.assertEqual(double_factorial(1), 1)
def test_4(self):
self.assertEqual(double_factorial(2), 2)
def test_5(self):
self.assertEqual(double_factorial(7), 105)
def test_6(self):
self.assertEqual(double_factorial(9), 945)
def test_7(self):
self.assertEqual(double_factorial(14), 645120)
def test_8(self):
self.assertEqual(double_factorial(22), 81749606400)
def test_9(self):
self.assertEqual(double_factorial(25), 7905853580625)
def test_10(self):
self.assertEqual(double_factorial(27), 213458046676875)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment