Thread: The third root of a number

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057

    The third root of a number

    I was wondering how to calculate the third square root of a number. Or the nth square root of a number. Is there a function for it?

    I know you can calculate the fourth square root like this:
    Code:
    #include <math.h>
    
    sqrt(sqrt(81));
    But that only works for multiples of two.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I don't know if I understand what you mean by "third" and "fourth" square root. If you just mean the third root, 4th root, nth root, then I'd say use pow.
    Code:
    pow(x, 1.0 / n);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    he means, the cubed root, (when he talking about third)

    8 * 8 * 8 = 512 // he want to know how to find the 8

    3 * 3 * 3 = 27 // he want to know how to find the 3


    or if this came up


    5 * 5 * 5 * 5 = 625 // he want to know how to find the 5

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Of course he does. And of course, square roots, cube roots, etc. are really just fractional exponents. Thus:

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main ()
    {
    	printf("%2.2f", pow(125, 1.0 / 3.0));
    	return 0;
    }
    Retuns a clean 5.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    An alternative, which has both advantages and disadvantages over using pow(), is

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main ()
    {
           double x = 125.0;
           int n;
    	printf("%2.2f", exp(log(x)/n));   /* x an n must both be positive */
    	return 0;
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, thanks. I don't know why I never thought of the pow() version . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  3. Really basic string operation
    By bobthebullet990 in forum C Programming
    Replies: 6
    Last Post: 11-28-2005, 05:18 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM