Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2008
    Location
    Vista, CA
    Posts
    59
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Finding the root of a number

    Here's a function I made for finding the root of most whole numbers.
    Code:
    function root(Num,Root) {
        var out = Root + "\u221A" + Num + " Irrational";
        for(i=Num;i>=0;i--) {
            if(Math.pow(i,Root)==Num) {out = i;}
        }
        return out;
    }
    Num is the number you're finding the root of, and Root is the root you're finding. So root(4,2) would return 2. If the root is irrational then the function will return "Root√Num Irrational" (e.g. root(4,3) would return "3√4 Irrational").

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Well, you could always do:
    Code:
    function root(num, root)
    {
      return Math.pow(num, 1/root);
    }
    Works for irrational numbers, too.

    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #3
    New Coder
    Join Date
    Jun 2008
    Location
    Vista, CA
    Posts
    59
    Thanks
    3
    Thanked 0 Times in 0 Posts
    oh... I guess my codes useless then.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •