Response to hijacker of thread:
http://www.daniweb.com/software-development/python/threads/59745
Finding doubling time by generator and logarithm
import itertools
import math
# to use input Python3 style in Python2
try:
input = raw_input
except:
pass
# margin to consider floating point numbers equal
delta = 1E-6
#A program to determine how long it takes for an investment to double
def main():
I=float(input("Enter annualized interest rate as a percent: "))/100.
# with generator
increase = 1+I
time_to_double = next(n for n in itertools.count() if increase**n > 2)
print('Time to double by generator is:', time_to_double)
# exactly by logarithm
print('Years to exactly double by logarithm: %.3f' % math.log(2, increase))
assert increase ** math.log(2, increase) - 2 < delta
main()
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.