+ 1
how can we write a program without using pow function?
6 Respostas
+ 6
Simply make a program which doesn't *need* pow function
Also you can remake the function yourself
If you only want pow for integers you can do it that way:
var customPow = function(x,n){
var r = 1;
for(var i = 0; i < n; i++){
r *= x;
}
return r;
}
But just... why do you wanna avoid using pow?!
Also if you want a pow also working for real values that would be way more... complex basically (lol)
Here are some advices coding it:
x^y = sum from n = 0 to infinity of (y*ln(x))^n/n!
You can approach it with a for i, and about ln(x), it's equal to the infinite sum from n = 1 of (-1)^(n+1)/n*(x-1)^n, only for x values between 0 and 2
However, for x values greater than 2, you can instead calculate -ln(1/x) which equals ln(x) and so 1/x will be between 0 and 2
Hope that helps!!
+ 3
What program tho?
+ 3
you can implement the modular exponentiation algorithm (see Wikipedia), or use the less efficient "multiply by this number n times" method. However, I can't think of a good reason *not* to use pow if it's already included in a library.
+ 2
You're welcome!
+ 1
thnx fr ur advice...but this qsn is found in qsn paper sometimes.
+ 1
thnx...I learned something from that