hey guyz... im trying to convert this formula into java code, im just having difficulty understanding it... ok it goes like this:

Given loan amount (denoted by a), monthly interest rate as a decimal number (denoted by r). and monthly payment (denoted by p), the loan duration (in months) can be determined by the formula

Duration = ceil ( -log(1 - ar / p) / log(1 + r) )

where ceil(x) is the smallest integer which is bigger or equal to x.

im not to sure about the ceil part but would the rest of the formula look like this:

int a = 0 //Loan amount
double r = 0 //Interest rate
double p = 0 //Monthly Payment
int duration = 0 //Loan duration
ceil = ????

duration = ????((-log(1-((a*r)/p)))/(log(1+r))

please feel free to forward any suggestions...

you might want to take a look at the Math class. Ceil is one of it's functions, of that I'm sure. I'm quite sure it 'll also help you solving the rest of the puzzle.

How about something like: double duration = Math.ceil(-Math.log(1 - a * r / p) / Math.log(1 + r)) Though I would like to point out here that the log use here is to the natural base (e) so you might want to convert it so that it is to base …

All 8 Replies

you might want to take a look at the Math class. Ceil is one of it's functions, of that I'm sure. I'm quite sure it 'll also help you solving the rest of the puzzle.

How about something like: double duration = Math.ceil(-Math.log(1 - a * r / p) / Math.log(1 + r)) Though I would like to point out here that the log use here is to the natural base (e) so you might want to convert it so that it is to base 10 using the formula: log A(base 10) = log A (base [I]e) / [/I]log 10 (base [I]e[/I])

commented: Great examples +6

thanx guys.. i have come up with the following code:

double duration = (((-Math.log)*(1-((a*r)/p)))/((Math.log)*(1+r));
System.out.println("It will take " + Math.ceil(duration) + " to pay the loan");

sos.. so what ur saying is the answer the formula will give me won't be correct ?

Your formula appears to be incorrect. Your log function is not taking any arguments, when it should be talking one. See the one I posted in my previous post.

The log function in the Math class gives us log to the base e, that is natural log. For normal calculations, we use log to the base 10. Hence we have to do some adjustments specified by me in my previous post.

ahh okay i understand what your saying... so type in the base formula before the System.out.println to convert it back to base 10?

what if i were to you use:

double duration = Math.ceil(-Math.log10(1 - a * r / p) / Math.log10(1 + r))

that would give me the correct answer, wouldn't it?

> double duration = Math.ceil(-Math.log10(1 - a * r / p) / Math.log10(1 + r))

Yes, provided you write your own function "log10" based on what I had posted before.

thank you veri much for the help... everythin has been solved ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.