Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Program To Find 2/3 Root Of Number X Using Only Loops? Help Please :/


  • Please log in to reply
1 reply to this topic

#1 tryingtolearn


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:Java
  • Learning:Java

Posted 08 July 2012 - 01:55 PM

Hello everyone!

I am trying to create a program that can display the two thirds root of a number that I provide.

I have yet to find any clear explanation of my problem online.

The format of the answer should be like this:

The two thirds root of ** = **.**

A two thirds root of x should be equal to x^3/2, correct?

Well I am trying to have the program solve for that x^3/2 without using Math.pow, and instead using a loop of any kind.

Thank you for any help,
  • 0

#2 RhetoricalRuvim


    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 08 July 2012 - 03:49 PM

I don't know why you would, as even C or assembly usually don't, but if you're trying to learn then it should actually be pretty easy, especially given that 3/2 is a rational number.

First of all, to calculate x^(3/2), let's take the fact that x^(a/b) = (x^a)^(1/b). This way the program just needs to find x^a, and then finding the b root of x should be as easy as implementing Newton's Method of Approximation.

We'll need to find the intersection of f(t)=t^b and g(t)=X . The reason for this is that X is what we're trying to find the b root of and t^b is a variable to the b power (we're trying to find t; t^b = X --> t = X^(1/b)). To find the intersection, we manipulate the equation to form the following:
t^b - X = 0

We can then use Newton's Method of Approximation to find what t must be equal to, given that t is the variable and all other symbols are constant.

Newton's method is like this:
x= x - f(x) / f'(x)

The way we follow that is put in a random value (a guess) for x and evaluate the right side of the equation. Then we take that result and put it in for x. We repeat the operation, using the new x for the right side of the equation and then plug that result in for x. We keep going until we get close enough to the actual answer (the rate of change will significantly decrease as the x gets close to the actual answer).

That can be thought of as:
float now_x= 8; // Just a guess value.
float previous_x= 100;
do {
previous_x = now_x
now_x = now_x - f(now_x) / f'(now_x)
} while (... until now_x is close enough to previous_x ...);

Where f(t) is, in our case, is (t^b - X) , and f'(t) is the derivative of f(t). The derivative with respect to t of (t^b - X) is equal to (b * t^(b - 1)) .

What you'll need to do is write a function or a procedure that will do those calculations (think of a value (a guess) for now_x, loop until now_x close enough to previous_x, and in the loop {set previous_x to now_x and then calculate the new value for now_x}) to find the square root (b = 2) of X. Again, to get X you just need to cube x (easy enough to do).
  • 1

Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download