@soon
2545 / 1310 / 81
Регистрация: 09.05.2011
Сообщений: 3,086
|
07.11.2011, 16:01
|
|
Можно так попробовать.
C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| #include <stdio.h>
#include <math.h>
float Pow(float num, int degree)
{
if(degree == 0)
return 1;
if(degree == 1)
return num;
return Pow((num * num), (degree / 2)) * ((degree % 2 == 0) ? (1) : (num));
}
int main(void)
{
int num;
int i;
float P, p;
scanf("%d", &num);
printf("\tmy pow\t pow from math.h\toctal\thex\n") ;
for(i = 0; i < num; ++i)
{
P = Pow((float)num, i);
p = pow((float)num, i);
printf("%d^%d = %6.1f\t\t%6.1lf\t\t%o\t%x\n", num, i, P, p, (int)p, (int)p);
}
return 0;
} |
|
Код
soon@coming:~/src$ ./main
5
my pow pow from math.h octal hex
5^0 = 1.0 1.0 1 1
5^1 = 5.0 5.0 5 5
5^2 = 25.0 25.0 31 19
5^3 = 125.0 125.0 175 7d
5^4 = 625.0 625.0 1161 271
Код
5 ^ 5 = 5 * 5 * 5 * 5 * 5 - 4 operations
Pow(5, 5) = Pow(5 * 5, 2) * 5 = 25 * 25 * 5 - 3 operations
При больших числах будут косяки с красивым выводом в консоль
0
|