Need help on Math Calculation with Java

D

Ding Lei

Hello java group,
I need to use Java to compute the following forumla:

p = c / (1 + y) + c / ((1 + y)^2) + c / ((1 + y)^3) + ... c / ((1 + y)^n)

p,c,n are known and we need to calculate y.
Is there anyway to do this with Java, if yes please show up sample code,
Thank you very much!


Yours,
layman
 
D

DrAcKe

Hi!

I think this is a mathematics problem, not a Java problem.
Maybe, if you search in a book of formules and tables, you found
what you want.

By3z, DrAcKe

PS Sorry for my bad english.
 
L

Liz

Ding Lei said:
Hello java group,
I need to use Java to compute the following forumla:

p = c / (1 + y) + c / ((1 + y)^2) + c / ((1 + y)^3) + ... c / ((1 + y)^n)

p,c,n are known and we need to calculate y.
Is there anyway to do this with Java, if yes please show up sample code,
Thank you very much!


Yours,
layman

It's too trivial. Do it yourself. Use Math.pow() to get something to a
power.
 
A

Andy Hill

Ding Lei said:
Hello java group,
I need to use Java to compute the following forumla:

p = c / (1 + y) + c / ((1 + y)^2) + c / ((1 + y)^3) + ... c / ((1 + y)^n)

p,c,n are known and we need to calculate y.
Is there anyway to do this with Java, if yes please show up sample code,
Thank you very much!
Assuming p,c & n aren't known until runtime, you'll probably have to iteratively
solve it. Lots of ways to skin that cat depending on how efficient, accurate,
etc. you want to be. Check out any beginning book (or Google for it) on
Numerical Methods. Should have no trouble implementing it in java, although
I'm sure there are already public-domain jars that implement most of the basic
methods already.
 
R

Roedy Green

It's too trivial. Do it yourself. Use Math.pow() to get something to a
power.

Come on, if it were that trivial you would have provided the solution.

There was a time in your career too when it was not trivial for you
too, either solving for p or y.

I don't think this is a homework problem.
 
L

Liz

Roedy Green said:
Come on, if it were that trivial you would have provided the solution.

And you would have a link to the solution ;-)
(just teasing)
There was a time in your career too when it was not trivial for you
too, either solving for p or y.

But that was before computers were invented.
 
T

Thomas Weidenfeller

Roedy said:
I don't think this is a homework problem.

I think it is. The 'c' coefficient is so suspiciously easy to eliminate,
and I am absolutely sure that the series is a well known one for y being
a number from a certain number set and n being, well, let's say 'large' :).

Just a second, yep, my old Bronstein / Semendjajew has a series listed
right in the first series' table in the first chapter which should be
possible to transform to the one given by the OP. No idea if it would
work out, but I am to lazy to calculated it.

/Thomas
 
T

Tom McGlynn

Andy said:
Assuming p,c & n aren't known until runtime, you'll probably have to iteratively
solve it. Lots of ways to skin that cat depending on how efficient, accurate,
etc. you want to be. Check out any beginning book (or Google for it) on
Numerical Methods. Should have no trouble implementing it in java, although
I'm sure there are already public-domain jars that implement most of the basic
methods already.

Before solving the original equation the original poster should
probably note that the right hand expression
is a geometric series and can be substantially simplified. But I don't
think there is an analytic solution for n>3 though.

Regards,
Tom McGlynn
 
K

kevin

this looks like a compound interest type calculation to me, so i'd
check for that on the net. However,...

if my maths is correct...

c/(1+y) + c/(1+y)^2 + c/(1+y)^3 + .... + c/(1+y)^n

is a geometric series of

initial term a = c/(1+y)

common ratio 1 /(1+y)

number of elements term = n-1

geometric series are solved by the formula (for n terms)

S = a (1-r^n) / (1-r)

so for n-1 terms this ought to give you java code

double initial = c;
double ratio = 1 / (1 + y);
double sum = initial * ( 1 - ( ratio ^ (n-1))) / (1 - ratio);

This would cause an ArithmeticException of course if you feed in y =
-1.

I hope the maths isn't wrong...
 
R

Roedy Green

double sum = initial * ( 1 - ( ratio ^ (n-1))) / (1 - ratio);

That's sort of hash between FORTRAN and Java. ^ in Java is XOR, not
power.

Try:

double sum = initial * ( 1 - ( Math.pow( ratio, n-1 )))
/ ( 1 - ratio );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top