View Full Version : Finding the root of a number

12-06-2008, 04:15 AM
Here's a function I made for finding the root of most whole numbers.

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").

12-06-2008, 04:52 PM
Well, you could always do:

function root(num, root)
return Math.pow(num, 1/root);
}Works for irrational numbers, too.


12-06-2008, 06:53 PM
oh... I guess my codes useless then.