Skip to content

Commit f3bebf7

Browse files
dlucanudlucanu
dlucanu
authored and
dlucanu
committed
added in examples/used-in-papers gc examples
1 parent 3aa3189 commit f3bebf7

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
gcd(a, b, out x, out y)
2+
@requires a : int && b : int && x : int && y : int
3+
@ensures \result : int
4+
@ensures x * a + y * b == \result
5+
{
6+
x = 1; y = 0; x1 = 0; y1 = 1; a1 = a; b1 = b;
7+
while (b1 > 0)
8+
@invariant x * a + y * b == a1
9+
@invariant x1 * a + y1 * b == b1
10+
{
11+
q = a1 / b1;
12+
tmp = x; x = x1; x1 = tmp - q * x1;
13+
tmp = y; y = y1; y1 = tmp - q * y1;
14+
tmp = a1; a1 = b1; b1 = tmp - q * b1;
15+
}
16+
return a1;
17+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
gcd(a, b, out x, out y) {
2+
x = 1; y = 0; x1 = 0; y1 = 1; a1 = a; b1 = b;
3+
while (b1 > 0) {
4+
q = a1 / b1;
5+
tmp = x; x = x1; x1 = tmp - q * x1;
6+
tmp = y; y = y1; y1 = tmp - q * y1;
7+
tmp = a1; a1 = b1; b1 = tmp - q * b1;
8+
}
9+
return a1;
10+
}
11+
x = 0; y = 0; a = 55; b = 80;
12+
g = gcd(a, b, x, y);
13+
@assert a * x + b * y == g; // certification check
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
A box contains N matches. Two players take turns taking one or two matches from the box.
3+
The player, whose turn it is and has no legal move to make, loses.
4+
Who has the winning strategy?
5+
*/
6+
7+
/*
8+
We show first that
9+
- if N is a multiple of 3 then the second player has a strategy to win;
10+
- otherwise, the first player has one.
11+
*/
12+
winingStrategy(n)
13+
{
14+
turn = 1;
15+
if (n % 3 != 0) {
16+
take = n % 3;
17+
n = n - take;
18+
turn = 2;
19+
}
20+
while (n > 0)
21+
{
22+
// the current turn
23+
choose take from {1,2};
24+
n = n - take;
25+
// the next turn
26+
turn = 3 - turn;
27+
take = n % 3;
28+
n = n - take;
29+
// prepare the next current turn (if any)
30+
if (n > 0) turn = 3 - turn;
31+
}
32+
return turn;
33+
}
34+
35+
for (i = 1; i < 10; ++i) {
36+
print([i, winingStrategy(i)]);
37+
}
38+

0 commit comments

Comments
 (0)