Skip to content

Instantly share code, notes, and snippets.

@ershad
Created September 28, 2011 07:33
Show Gist options
  • Save ershad/1247244 to your computer and use it in GitHub Desktop.
Save ershad/1247244 to your computer and use it in GitHub Desktop.
A script to solve a problem in algo textbook :D
# Python script to solve the problem 1-1 in 'Introduction to Algorithms - Cormen, Leiserson, Rivest, Stein (3rd edition)'
# Ershad K <ershad92@gmail.com>
# License: GPL Version 3
from math import *
import sys
def f_(n):
return n**3
# Define the desired f(n) here and return the value.
print "Processing..."
sec = 10**6 #microseconds --> seconds
maxrange = [sec ,sec*60 , sec*60*60, sec*60*60*24, sec*60*60*24*30, sec*60*60*24*30*12, sec*60*60*24*30*12*100]
cmaxrange = ['1 second' ,'1 minute','1 hour' , '1 day', '1 month', '1 year', '1 century' ]
maxi = 2**800 # A large range
i = 1
for j in range(len(maxrange)):
g = 0
while i < maxi:
oldg = g
g = f_(i)
if g > maxrange[j] :
print "n = %2d [f(n) = %-20.2f Time = %-10s]" % (i-1,oldg,cmaxrange[j])
break
i = i + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment