-
Notifications
You must be signed in to change notification settings - Fork 1
/
025-factorize.dfy
50 lines (48 loc) · 1.1 KB
/
025-factorize.dfy
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
function prod(s: seq<int>) : int {
if |s| == 0 then 1 else prod(s[..|s| - 1]) * s[|s| - 1]
}
method factorize(n: nat) returns (factors: seq<nat>)
// pre-conditions-start
requires n > 0
// pre-conditions-end
// post-conditions-start
ensures prod(factors) == n
// post-conditions-end
{
// impl-start
factors := [];
ghost var taken := 1;
var cur := n;
var i := 2;
while i * i <= cur
// invariants-start
invariant prod(factors) == taken
invariant taken * cur == n
invariant cur >= 1
// invariants-end
{
ghost var pre := cur;
ghost var temp := 1;
while cur % i == 0
// invariants-start
invariant cur >= 1
invariant temp * cur == pre
invariant prod(factors) == taken * temp
// invariants-end
decreases cur - 1
{
factors := factors + [i];
cur := cur / i;
temp := temp * i;
assert 2 <= i && 2 * cur <= i * cur; // assert-line
}
taken := taken * temp;
i := i + 1;
}
if cur > 1 {
factors := factors + [cur];
taken := taken * cur;
}
assert taken == n; // assert-line
// impl-end
}