summaryrefslogtreecommitdiff
path: root/3/6_control/22_loop.c
blob: 73c7710de00bac81a93077621e1b3c2a07857b14 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
// do
//    body
//    while (test);
//
// loop:
//   body
//   t = test;
//   if (t)
//      goto loop;

typedef int64_t fact_t;

long fact_do(long n)
{
    long result = 1;
    do {
        result *= n;
        n = n-1;
    } while (n > 1);
    return result;
}

int tmult_ok(fact_t x, fact_t y)
{
    fact_t p = x*y;
    return !x || p/x == y;
}

fact_t fact_do_int(size_t n)
{
    fact_t result = 1;
    do {
        if (tmult_ok(result, n)) {
            result *= n;
            n = n-1;
        } else {
            printf("would overflow this iter, n=%ld\n", n);
            n = 0;
        }
    } while (n > 1);
    return result;
}

int main(void) {
    size_t f = 20;
    printf("result: %ld, max: %ld, less: %d", fact_do_int(f), INT64_MAX, fact_do_int(f) < INT64_MAX);
    return 0;
}