-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorial_rec.c
41 lines (36 loc) · 887 Bytes
/
factorial_rec.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*@ inductive factorial_ind(integer i, integer f) {
case nil:
factorial_ind(0, 1);
case step_inc:
\forall integer i, f;
factorial_ind(i - 1, f) ==> factorial_ind(i, f * i);
}
lemma factorial_ind_20:
factorial_ind(20, 2432902008176640000);
lemma increasing:
\forall integer i1, i2, f1, f2;
i2 >= i1 && factorial_ind(i1, f1) && factorial_ind(i2, f2) ==> f2 >= f1;
*/
#define SPEC_ULONG_MAX 18446744073709551615UL
/*@ requires i <= 20;
decreases i;
assigns \nothing;
ensures factorial_ind(i, \result);
*/
unsigned long factorial_rec(unsigned i)
{
if (i == 0) {
return 1;
} else {
return factorial_rec(i - 1) * i;
}
}
#ifdef OUT_OF_TASK
#include <stdio.h>
int main(void)
{
printf("res: %lu\n", factorial_rec(10));
printf("res: %lu\n", factorial_rec(20));
return 0;
}
#endif