How to quickly build an integer into a whole degree, i.e. Implement a simplified function Pow
from & lt; CMath & GT;
?
Answer 1, Authority 100%
everything is implemented by built-in means. Google per minute – Fast construction to Step Man . Disassessed detail there. Actually the code itself is:
int binpow (int a, int n) {
INT RES = 1;
While (N)
If (N & amp; 1) {
res * = a;
--n;
}
ELSE {
a * = a;
n & gt; & gt; = 1;
}
RETURN RES;
}
Answer 2, Authority 50%
for x = 2 – shift units on Y bits left.
If the base is more – use decomposition in degrees x 2 t sup> sup>
.
Int format requires four multiplicate pairs (3 31 sup>= 617673396283947)
64-bit format – five.
int power (int x, int y) {
INT POW, RESULT;
if (x == 2) Return 1 & lt; & lt; y;
POW = X;
result = y & amp; 1? POW: 1;
POW * = POW;
Result * = y & amp; 2? POW: 1;
POW * = POW;
Result * = y & amp; 4? POW: 1;
POW * = POW;
result * = y & amp; 8? POW: 1;
POW * = POW;
Return Y & amp; 16? Result * Pow: Result;
}
p.s. Programs without a cycle and with a cycle on PHP (redid there is almost nothing to say):
function power ($ x, $ y) {
if ($ x == 2) Return 1 & lt; & lt; $ y;
$ POW = $ x;
$ result = $ y & amp; 1? $ POW: 1;
$ POW * = $ POW;
$ result * = $ y & amp; 2? $ POW: 1;
$ POW * = $ POW;
$ result * = $ y & amp; 4? $ POW: 1;
$ POW * = $ POW;
$ result * = $ y & amp; 8? $ POW: 1;
$ POW * = $ POW;
Return $ y & amp; 16? $ Result * $ POW: $ Result;
}
FUNCTION BINPOW ($ A, $ n) {
$ Res = ($ n & amp; 1)? $ A: 1;
While ($ n) {
$ a * = $ a;
if ((($ n & gt; & gt; = 1) & amp; 1) {
$ res * = $ a;
}
}
RETURN $ RES;
}
PrintF ("& lt; br & gt; x =% d & amp; emsp; y =% d & amp; emsp; x ** y =% d & amp; emsp; x ** y =% d, $ x = 2, $ y = 15, Power ($ x, $ y), binpow ($ x, $ y));
Printf ("& lt; br & gt; x =% d & amp; emsp; y =% d & amp; emsp; x ** y =% d & amp; emsp; x ** y =% d, $ x = 6, $ y = 4, Power ($ x, $ y), binpow ($ x, $ y));
Printf ("& lt; br & gt; x =% d & amp; emsp; y =% d & amp; emsp; x ** y =% d & amp; emsp; x ** y =% d, $ x = 3, $ y = 10, Power ($ x, $ y), binpow ($ x, $ y));
PrintF ("& lt; br & gt; x =% d & amp; emsp; y =% d & amp; emsp; x ** y =% d & amp; emsp; x ** y =% d, $ x = 5, $ y = 5, Power ($ x, $ y), binpow ($ x, $ y));
Results:
x = 2 y = 15 x ** y = 32768 x ** y = 32768 x = 6 y = 4 x ** y = 1296 x ** y = 1296 x = 3 y = 10 x ** y = 59049 x ** y = 59049 x = 5 y = 5 x ** y = 3125 x ** y = 3125
Answer 3
I think so (checked, works), though without a fractional indicator, but with negative
# include & lt; iostream & gt;
#Include & lt; Cstdlib & gt;
Using Namespace STD;
INT MAIN ()
{
SETLOCALE (0, "");
int a, n;
Double FN_EXPON (INT S, Int F);
COUT & LT; & LT; "Enter the number of degree:" & lt; & lt; Endl; CIN & GT; & GT; a;
COUT & LT; & LT; "Enter the number indicator:" & lt; & lt; Endl; CIN & GT; & GT; n;
COUT & LT; & LT; "Result =" & lt; & lt; fn_expon (a, n) & lt; & lt; Endl;
Return 0;
}
Double FN_Expon (int s, int f) {
int i;
Double Res = 1;
While (s) {
if (! f) Return Res;
For (i = 1; i & lt; = ABS (F); I ++)
res * = s;
IF (F & LT; 0) {
res = 1 / res; RETURN RES;
}
RETURN RES;
}
RETURN RES;
}