Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Tuesday, May 24, 2011

Python - Determine Primality of a number

 
# Determining primality by using a simple trial division test. 
# 
# This approach tests whether n is a multiple of an integer i 
# between 2 and √n.  If it is a multiple of any of these integers 
# then it is not a prime. 
 
import math
import time
 
def isPrime(n):
    for i in range(2, int(math.sqrt(n))):
        if n % i == 0:
            return False
    return True
 
def withTimer(n):
    start = time.time()
    prime = isPrime(n)
    elapsed = time.time() - start
    print("{0}   {1}    time:{2}").format(prime, n, elapsed)
 
# Primality of 2 to 13 digit known primes 
withTimer(13)
withTimer(715827883)
withTimer(2932031007403)
 
# test non primes 
withTimer(52)
withTimer(5820384023)
withTimer(2059384726303)
 
# my output: 
# True   13    time:0.0 
# True   715827883    time:0.00300002098083 
# True   2932031007403    time:0.521000146866 
# False   52    time:0.0 
# False   5820384023    time:0.000999927520752 
# False   2059384726303    time:0.029000043869 
 
 
 

Thursday, March 3, 2011

Python - Compute the factorial of N with recursion (among others)

# 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 
 
 

Tuesday, September 15, 2009

Python - create static methods in a class

#create static methods in a python class

import math

class MyStaticClass:
def getSqrt(num):
return math.sqrt(num)
def getHalf(num):
return num/2
# labels the methods as static
getSqrt = staticmethod(getSqrt)
getHalf = staticmethod(getHalf)

MyStaticClass.getSqrt(144)
12.0
MyStaticClass.getHalf(20)
10