#!/usr/bin/python3
# =================================================================
# In mathematics, the double factorial of a number n, denoted
# by n‼, is the product of all the positive integers up to n
# that have the same parity (odd or even) as n. (Wikipedia)
# =================================================================
import math
import user_interface as ui
# -----------------------------------------------------------------
# ---- double factorial
# -----------------------------------------------------------------
def double_factorial(n:int) -> int:
if n <= 0:
return 1
else:
return n * double_factorial(n-2)
# -----------------------------------------------------------------
# ---- ask the user for a non-negative integer
# -----------------------------------------------------------------
def ask_the_user():
while True:
# ---- ask the user for a non-negative integer
print()
s = ui.get_user_input('Enter a non-negative integer: ')
if not s: break
tf,i = ui.is_integer(s)
if not tf:
print('bad input - try again')
continue
if i < 0:
print('bad input - try again')
continue
# ---- display integer's factorial
print()
print(f'{i}! is {math.factorial(i)}')
# ---- display integer's double factorial
print(f'{i}!! is {double_factorial(i)}')
return
# -----------------------------------------------------------------
# ---- main
# -----------------------------------------------------------------
# ---- basic test data
# 5! is 120
# 23! is 25852016738884976640000
#print()
#print(math.factorial(0))
#print(math.factorial(1))
#print(math.factorial(5))
#print(math.factorial(23))
ask_the_user()