Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Finding the Two-Thirds Root of a Number Using Loops

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Finding the Two-Thirds Root of a Number Using Loops

    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 xxx = xxx.xxx

    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,
    tryingtolearn


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Finding the Two-Thirds Root of a Number Using Loops

    In case you aren't aware of it, Java doesn't have an exponentiation operator. People often say "x^2/3" as a shorthand, but using standard Java they mean something like a call to Math.pow(). ^ really means something quite different in Java.

    (Also note that the 2/3 root is 2/3, not 3/2)

    (A) The 2/3 root of a number is the cube root of the square of that number.

    Think about positive numbers as a simplification that might serve as starting point.

    (B) The 2/3 root of zero is zero and the 2/3 root grows as the number whose root is being found grows. What I mean is that big numbers have big 2/3 roots and small numbers have small 2/3 roots.

    This suggests we might find a 2/3 root by playing 20 questions. (or rather, n questions where n is the limit in the for loop you are supposed to use.)

    make a bigGuess
    and a littleGuess
    LOOP:
        is (bigGuess+littleGuess)/2 too big?  If so make bigGuess=(bigGuess+littleGuess)/2
        is (bigGuess+littleGuess)/2 too small?  If so make littleGuess=(bigGuess+littleGuess)/2

    The initial guesses could take advantage of the fact that (C) for numbers bigger than 1 the 2/3 root is less than the number, while for numbers less than 1 the 2/3 root is bigger than the number.

    -----

    Newton and Halley both came up with iterative methods. But the problem of doubling a cube is much older with geometric (Greek) and iterative approximations (Indian) going back to c400BCE.
    Last edited by pbrockway2; July 8th, 2012 at 06:40 PM.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Finding the Two-Thirds Root of a Number Using Loops

    Quote Originally Posted by pbrockway2 View Post
    In case you aren't aware of it, Java doesn't have an exponentiation operator. People often say "x^2/3" as a shorthand, but using standard Java they mean something like a call to Math.pow(). ^ really means something quite different in Java.

    (Also note that the 2/3 root is 2/3, not 3/2)

    (A) The 2/3 root of a number is the cube root of the square of that number.

    Think about positive numbers as a simplification that might serve as starting point.

    (B) The 2/3 root of zero is zero and the 2/3 root grows as the number whose root is being found grows. What I mean is that big numbers have big 2/3 roots and small numbers have small 2/3 roots.

    This suggests we might find a 2/3 root by playing 20 questions. (or rather, n questions where n is the limit in the for loop you are supposed to use.)

    make a bigGuess
    and a littleGuess
    LOOP:
        is (bigGuess+littleGuess)/2 too big?  If so make bigGuess=(bigGuess+littleGuess)/2
        is (bigGuess+littleGuess)/2 too small?  If so make littleGuess=(bigGuess+littleGuess)/2

    The initial guesses could take advantage of the fact that (C) for numbers bigger than 1 the 2/3 root is less than the number, while for numbers less than 1 the 2/3 root is bigger than the number.

    -----

    Newton and Halley both came up with iterative methods. But the problem of doubling a cube is much older with geometric (Greek) and iterative approximations (Indian) going back to c400BCE.
    Thank you for your reply, but I am still having trouble understanding.

    let's say I wanted to find the 2/3 root of 100.

    In the post above it says I should take the square of 100, which is 10, and then find the cube root of 10, which is 3. something.

    So I guess I just don't understand the bigGuess/littleGuess concept.

    Thanks for your patience and help so far!
    tryingtolearn

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Finding the Two-Thirds Root of a Number Using Loops

    let's say I wanted to find the 2/3 root of 100
    The 2/3 root of 100 is a number whose cube is 10000 (100 squared). That's (A). In other words we want to find something, where something*something*something is 10000.

    That something lies between 1 and 10000. That's (C).

    I'll take littleGuess=1 and bigGuess=10000.

    LOOP starts:

    I check (littleGuess+bigGuess)/2 which is 5000.5

    5000.5*5000.5*5000.5 is 125037503750.125 and that's too big (the cube should be 10000). So I revise my guess and make bigGuess=5000.5 and go back to the top of the loop...

    -----

    I check (littleGuess+bigGuess)/2 which is 2500.75

    2500.75*2500.75*2500.75 is 15639066719.171875 and that's still too big. So I make bigGuess=2500.75 and try again...

    -----

    Check (littleGuess+bigGuess)/2 which is 1250.875. The cube is 1957229434.263671875. bigGuess becomes 1250.875
    Check (littleGuess+bigGuess)/2 which is 625.9375. The cube is 245240906.585693359375. bigGuess becomes 625.9375
    Check (littleGuess+bigGuess)/2 which is 313.46875. The cube is 30802272.332489013671875. bigGuess becomes 313.46875
    Check (littleGuess+bigGuess)/2 which is 157.234375. The cube is 3887250.213802337646484375. bigGuess becomes 157.234375
    Check (littleGuess+bigGuess)/2 which is 79.1171875. The cube is 495236.357871532440185546875. bigGuess becomes 79.1171875
    Check (littleGuess+bigGuess)/2 which is 40.05859375. The cube is 64281.662188470363616943359375. bigGuess becomes 40.05859375

    Check (littleGuess+bigGuess)/2 which is 20.529296875. The cube is 8652.113846175372600555419921875. Too small! bigGuess stays at 40.05859375 and littleGuess becomes 20.529296875.

    Check (littleGuess+bigGuess)/2 which is 30.2939453125. The cube is 27801.454087962396442890167236328. (too big) bigGuess becomes 30.2939453125
    Check (littleGuess+bigGuess)/2 which is 25.41162109375. The cube is 16409.566686896956525743007659912. bigGuess becomes 25.41162109375
    Check (littleGuess+bigGuess)/2 which is 22.970458984375. The cube is 12120.178596764206304214894771576.

    -----

    The loop constantly evaluates the cube of (littleGuess+bigGuess)/2 and updates one or other of the guesses based on whether the cube is too high or too low (compared with 10000=100^2).

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Finding the Two-Thirds Root of a Number Using Loops

    Try taking a look at this: optimized pow() approximation

    edit:

    another good link: Fast pow() with adjustable accuracy
    Last edited by helloworld922; July 8th, 2012 at 08:46 PM.

Similar Threads

  1. Finding Chromatic Number w/ Brute Force Algorithm
    By thecrazytaco in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 16th, 2011, 07:35 AM
  2. Finding all Permutations that add up to a number.
    By godsfearme in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 28th, 2010, 03:54 PM
  3. Finding square root
    By Tracy22 in forum The Cafe
    Replies: 1
    Last Post: October 18th, 2010, 06:18 PM
  4. Finding out number of Palindromes in a Sentence
    By rameshiit19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 12th, 2010, 09:44 PM
  5. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM