Last change
on this file was
115,
checked in by rosiere, 15 years ago
|
1) Write queue with mealy
2) Network : fix bug
3) leak memory
|
-
Property svn:keywords set to
Id
|
File size:
1.9 KB
|
Rev | Line | |
---|
[102] | 1 | #include "func_factoriel.h" |
---|
| 2 | #include "func_math.h" |
---|
| 3 | #include "func_io.h" |
---|
| 4 | |
---|
| 5 | //-----[ Factoriel ]------------------------------------------------------- |
---|
| 6 | |
---|
[115] | 7 | #define mul(x,y) mul_soft(x,y) |
---|
| 8 | |
---|
[102] | 9 | unsigned int factoriel_recursif (unsigned int x) |
---|
| 10 | { |
---|
| 11 | if ( x <= 1) |
---|
| 12 | return 1; |
---|
[108] | 13 | |
---|
[115] | 14 | unsigned int res = mul(x , factoriel_recursif (x-1)); |
---|
[102] | 15 | |
---|
[108] | 16 | return res; |
---|
[102] | 17 | } |
---|
| 18 | |
---|
| 19 | unsigned int factoriel_iteratif (unsigned int x) |
---|
| 20 | { |
---|
| 21 | unsigned int res= 1; |
---|
| 22 | |
---|
| 23 | while (x > 1) |
---|
| 24 | { |
---|
[115] | 25 | res = mul(res,x); |
---|
[102] | 26 | x --; |
---|
| 27 | } |
---|
| 28 | return res; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | //------------------------------------------------------------------------- |
---|
| 32 | //-----[ Test ]------------------------------------------------------------ |
---|
| 33 | //------------------------------------------------------------------------- |
---|
| 34 | |
---|
| 35 | void test_factoriel_iteratif (int x) |
---|
| 36 | { |
---|
[108] | 37 | int x_min = 0; |
---|
[102] | 38 | int x_max = 12; |
---|
| 39 | int wait [x_max+1]; |
---|
| 40 | |
---|
| 41 | wait[0] = 1; // 1 |
---|
| 42 | wait[1] = 1; // 1 |
---|
| 43 | wait[2] = 2; // 2 |
---|
| 44 | wait[3] = 6; // 6 |
---|
| 45 | wait[4] = 24; // 18 |
---|
| 46 | wait[5] = 120; // 78 |
---|
| 47 | wait[6] = 720; // 2d0 |
---|
| 48 | wait[7] = 5040; // 13b0 |
---|
| 49 | wait[8] = 40320; // 9d80 |
---|
| 50 | wait[9] = 362880; // 58980 |
---|
| 51 | wait[10] = 3628800; // 375f00 |
---|
| 52 | wait[11] = 39916800; // 2611500 |
---|
| 53 | wait[12] = 479001600; // 1c8cfc00 |
---|
| 54 | |
---|
[108] | 55 | for (int i = x_min; i <= ((x<x_max)?x:x_max); i ++) |
---|
[102] | 56 | if (factoriel_iteratif (i) != wait[i]) quit(i+1); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | void test_factoriel_recursif (int x) |
---|
| 60 | { |
---|
[108] | 61 | int x_min = 0; |
---|
[102] | 62 | int x_max = 12; |
---|
| 63 | int wait [x_max+1]; |
---|
| 64 | |
---|
| 65 | wait[0] = 1; // 1 |
---|
| 66 | wait[1] = 1; // 1 |
---|
| 67 | wait[2] = 2; // 2 |
---|
| 68 | wait[3] = 6; // 6 |
---|
| 69 | wait[4] = 24; // 18 |
---|
| 70 | wait[5] = 120; // 78 |
---|
| 71 | wait[6] = 720; // 2d0 |
---|
| 72 | wait[7] = 5040; // 13b0 |
---|
| 73 | wait[8] = 40320; // 9d80 |
---|
| 74 | wait[9] = 362880; // 58980 |
---|
| 75 | wait[10] = 3628800; // 375f00 |
---|
| 76 | wait[11] = 39916800; // 2611500 |
---|
| 77 | wait[12] = 479001600; // 1c8cfc00 |
---|
| 78 | |
---|
[108] | 79 | for (int i = x_min; i <= ((x<x_max)?x:x_max); i ++) |
---|
[102] | 80 | if (factoriel_recursif (i) != wait[i]) quit(i+1); |
---|
| 81 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.