no way to compare when less than two revisions
تفاوتها
تفاوت دو نسخهٔ متفاوت از صفحه را مشاهده میکنید.
— | courseware:python_programming:resources:code:9 [2022/07/19 17:51] (فعلی) – ایجاد شد - ویرایش خارجی 127.0.0.1 | ||
---|---|---|---|
خط 1: | خط 1: | ||
+ | ===== محاسبهٔ فاکتوریل ===== | ||
+ | <code python line-numbers=" | ||
+ | import math | ||
+ | |||
+ | |||
+ | def factorial_for(x): | ||
+ | result = 1 | ||
+ | for i in range(2, x + 1): | ||
+ | result *= i | ||
+ | return result | ||
+ | |||
+ | |||
+ | def factorial_while(x): | ||
+ | result = 1 | ||
+ | while x > 1: | ||
+ | result *= x | ||
+ | x -= 1 | ||
+ | return result | ||
+ | |||
+ | |||
+ | def factorial_recursive(x): | ||
+ | if x == 0: | ||
+ | return 1 | ||
+ | else: | ||
+ | return x * factorial_recursive(x - 1) | ||
+ | |||
+ | |||
+ | math.factorial(5) | ||
+ | </ |