# Calculating the factorial of a number # is a good way to practice recursion. Below # are examples of both recursive and iterative # approaches to calculating a factorial. Of course # python has its own builtin math module that # can also take care of factorials. import math # A recursive function to calculate the # factorial of N def recursiveFactorial(n): if n == 1: return n return n * recursiveFactorial(n - 1) # Iterative calculation of the factorial # of N def iterativeFactorial(n): result = 1 while n > 1: result *= n n = n-1 return result # Builtin python math.factorial of N def pythonLib(n): return math.factorial(n) print("Iterative factorial of 5: ", iterativeFactorial(5)) print("Recursive factorial of 5: ", recursiveFactorial(5)) print("math.factorial of 5: ", pythonLib(5)) print("Iterative factorial of 10: ", iterativeFactorial(10)) print("Recursive factorial of 10: ", recursiveFactorial(10)) print("math.factorial of 10: ", pythonLib(10)) print("Iterative factorial of 15: ", iterativeFactorial(15)) print("Recursive factorial of 15: ", recursiveFactorial(15)) print("math.factorial of 15: ", pythonLib(15)) # my results: # Iterative factorial of 5: 120 # Recursive factorial of 5: 120 # math.factorial of 5: 120 # Iterative factorial of 10: 3628800 # Recursive factorial of 10: 3628800 # math.factorial of 10: 3628800 # Iterative factorial of 15: 1307674368000 # Recursive factorial of 15: 1307674368000 # math.factorial of 15: 1307674368000
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts
Thursday, March 3, 2011
Python - Compute the factorial of N with recursion (among others)
Monday, April 12, 2010
Python - Finding GCD with Euclids Algorithm
# Implementing the Euclidean Algorithm # is simple in python. # # Euclid's algorithm is used to efficiently # find two numbers greatest common divisor (GCD) # Division based Euclidean algorithm def gcd(a,b): while b != 0: temp = b b = a%b a = temp return a # Recursive based Euclidean algorithm def recursiveGCD(a,b): if b==0: return a else: return recursiveGCD(b, a%b) # just to prove they both work the same: def findGCD(a,b): divisionStyle = gcd(a,b) recursiveStyle = recursiveGCD(a,b) if divisionStyle != recursiveStyle: return "Fail...GCDs differ" else: return divisionStyle print findGCD(1071, 462) print findGCD(5, 10) print findGCD(5214, 24324) # my output: # 21 # 5 # 6
Thursday, February 25, 2010
Python - the final countdown
# There is more than one way to accomplish a task. # I needed a method to countdown from a positive # number down to 0. Here are three ways to solve # the issue. Of course there are many more possible # solutions. # # I'd like to title this post "The Final Count Down" # Listen to this tune whilst perusing the script: # www.youtube.com/watch?v=tt_ro2aerQg # Using recursion always makes the girls swoon. def recursiveCountdown(number): print number if number > 0: recursiveCountdown(number-1) # Old school while loop. This solution gets points # for being the most obvious. def whileLoopCountdown(number): while number > -1: print number number -= 1 # Using python's xrange function is the most concise. def xrangeCountdown(number): for i in xrange(number, -1, -1): print i print "It's the final countdown" number = input("Enter number: ") print "Recursive..." recursiveCountdown(number) print "While Loop..." whileLoopCountdown(number) print "xrange..." xrangeCountdown(number) # my output: # #It's the final countdown #Enter number: 5 #Recursive... #5 #4 #3 #2 #1 #0 #While Loop... #5 #4 #3 #2 #1 #0 #xrange... #5 #4 #3 #2 #1 #0
Subscribe to:
Posts (Atom)