Title / Description
Code a=100000.0 r=0.06 p=10000.0 n=Math.log((-p)/(a*r-p),1+r) #formula for n, directly p n time2=( (1.0+r)**n * (a*r-p) +p)/r p time2 a=100000.0 r=0.07754689530012998 #p=10000.0 n=15.725208543887753 #formula for n, directly n=20 p=(a*r*(1.0+r)**n)/(((1.0+r)**n)-1) puts p #file 5c.rb def f(r) am=100000 # amount p = 10000 #payment/ year n=20 #years # function for calculation p subtracted by itself to get a root #for which we have root r such that f(r)=0 # where r is ratio >>>> (am*r*(1.0+r)**n)/(((1.0+r)**n)-1)-p end #used simplified Newton method for rootfinding def newton_simple(init, epsilon) x = init d = f(x)/((f(x+f(x))-f(x))/f(x)) while(d.abs > epsilon ) do x = x-d d = f(x)/((f(x+f(x))-f(x))/f(x)) end return x end p newton_simple(0.6, 0.0000000000000001)
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code