Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/data/math/factorial
blob: bbe5a20e833150ded2f041c87d2914076208d92e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import time

def factorial_recursive(number):
    """ Define a factorial function in recursive flavor """
    result = 1
    if number > 0:
        result = number * factorial_recursive(number - 1)
        print 'factorizing: ', number, ' result: ', result
    return result

def factorial_iterative(number):
    """ Define a factorial function in iterative flavor """
    result = 1
    for i in range(1, number + 1):
        result = result * i
        print 'factorizing: ', i, ' result: ', result
    return result

def calculate(number, type):
    """ Calculate factorial using recursive and iterative methods """
    start = time.time()
    if type == 0:
        type_s = 'recursive'
        factorial_recursive(number)
    else:
        type_s = 'iterative'
        factorial_iterative(number)
    delta = time.time() - start
    print 'Type: ', type_s, ' in: ', 1 / delta

# ask for a number to compute the factorial of
number = input('Please input a number:')
print 'Calculating...'
calculate(number, 0)
calculate(number, 1)