使用Python計算某個數的階乘及打印1!-2!+3!-4!...+50!的結果
計算某個數的階乘
number = 5 # 計算3的階乘
re = 1 # 存儲最後算出來的結果,因為最初的數是1,1*任何數都是1
i = 1 # 臨時存儲當前階乘計算到哪個數了
while i <= number:
re *= i
i += 1
print(re)
打印1!-2!+3!-4!...+50!的結果
sum = 0
number = 1
while number <= 50:
result = 1 # 存儲最後計算的結果
i = 1
while i <= number:
result *= i
i += 1
# print(result)
if number % 2 ==0:
sum -= result
else:
sum += result
number += 1
print(sum)