Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Math problems

Greenhorn
Posts: 5
I'm trying to write a program to calculate for any positive value of x. I have to use the expression: square root of(7x^4 + 5x^3 + 3x^2 + x). I have it up till the part where I have to incorporate the sqrt() and pow() methods in order to get the answer. Any advice? Thanks...

Sheriff
Posts: 11343
Welcome to JavaRanch!

What have you tried so far? You might start small to make sure you're using the methods correctly -- for example, just print out the square root of 9, or try raising 5 to the 2nd power.

Katrina Cobb
Greenhorn
Posts: 5
I feel like an idiot for not thinking of that
...thanks!

Ranch Hand
Posts: 1970
You do not need, and should not use, pow() for this expression. All the powers are integers, so you can just use ordinary multiplication, which is much faster than pow() and more accurate, too.

Also, you have a polynomial there, so you can refactor, to make the number of operations smaller.

For instance: -

x^4 + 2 x^3 + 3 x^2 + x + 4

is refactorable to

4 + x ( 1 + x ( 3 + x ( 2 + x ) ) )

Or something like that. The general point is that, when you have to implement a mathematical formula in a computer program, you should first take a good look at the formula itself, to see if it can be simplified and to see how it is best implemented on a computer.
[ September 25, 2006: Message edited by: Peter Chase ]

 Hey, sticks and stones baby. And maybe a wee mention of my stuff: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS