+ 1

how can we write a program without using pow function?

29th Sep 2017, 11:44 AM
samiunsadat
6 Answers
+ 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!!
29th Sep 2017, 12:30 PM
Dapper Mink
Dapper Mink - avatar
+ 3
What program tho?
29th Sep 2017, 12:17 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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.
29th Sep 2017, 12:21 PM
forrest
+ 2
You're welcome!
29th Sep 2017, 12:39 PM
Dapper Mink
Dapper Mink - avatar
+ 1
thnx fr ur advice...but this qsn is found in qsn paper sometimes.
29th Sep 2017, 12:24 PM
samiunsadat
+ 1
thnx...I learned something from that
29th Sep 2017, 12:33 PM
samiunsadat