Skip to content

Commit

Permalink
102, 106, 142, 157, 159, 163
Browse files Browse the repository at this point in the history
  • Loading branch information
WeetHet committed Aug 2, 2024
1 parent 974bdcc commit 07b7fb4
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .zed/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tab_size": 2
}
29 changes: 29 additions & 0 deletions 102-choose_num.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
method choose_num(x : int, y : int) returns (num : int)
requires 0 <= x && 0 <= y
ensures num == -1 || (num >= x && num <= y)
ensures num == -1 || num % 2 == 0
ensures num == -1 || (forall i : int :: x <= i <= y && i % 2 == 0 ==> num >= i)
ensures num == -1 <==> x >= y
{
num := -1;
if x >= y {
return;
}
if x % 2 == 0 {
num := x;
} else {
num := x + 1;
}
var i := x + 2;
while i <= y
invariant i >= x && i <= y + 1
invariant num == -1 || num % 2 == 0
invariant forall j : int :: x <= j < i && j % 2 == 0 ==> num >= j
invariant num == -1 || (num >= x && num < i)
{
if i % 2 == 0 {
num := i;
}
i := i + 1;
}
}
54 changes: 54 additions & 0 deletions 106-f.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function factorial_spec(n : int) : int
requires n >= 0
decreases n
{
if n == 0 then 1 else n * factorial_spec(n - 1)
}

function sum_spec(n : int) : int
requires n >= 0
decreases n
{
if n == 0 then 1 else n + sum_spec(n - 1)
}

method f(n : int) returns (result : seq<int>)
requires n >= 1
ensures |result| == n
ensures forall i : int :: i >= 0 && i < |result| && i % 2 == 0 ==> result[i] == factorial_spec(i)
ensures forall i : int :: i >= 0 && i < |result| && i % 2 != 0 ==> result[i] == sum_spec(i)
{
result := [];
var i := 0;
while i < n
invariant i >= 0 && i <= n
invariant |result| == i
invariant forall i : int :: i >= 0 && i < |result| && i % 2 == 0 ==> result[i] == factorial_spec(i)
invariant forall i : int :: i >= 0 && i < |result| && i % 2 != 0 ==> result[i] == sum_spec(i)
{
if i % 2 == 0 {
var x := 1;
var j := 0;
while j < i
invariant j >= 0 && j <= i
invariant x == factorial_spec(j)
{
j := j + 1;
x := x * j;
}
result := result + [x];
} else {
var x := 1;
var j := 0;
while j < i
invariant j >= 0 && j <= i
invariant x == sum_spec(j)
{
j := j + 1;
x := x + j;
}
result := result + [x];
}
i := i + 1;
}
}
23 changes: 23 additions & 0 deletions 142-sum_squares.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
method sum_squares(a : seq<int>) returns (result : seq<int>)
ensures |result| == |a|
ensures forall i : int :: i >= 0 && i < |result| && i % 3 == 0 ==> result[i] == a[i] * a[i]
ensures forall i : int :: i >= 0 && i < |result| && i % 4 == 0 && i % 3 != 0 ==> result[i] == a[i] * a[i] * a[i]
{
result := [];
var i := 0;
while i < |a|
invariant i >= 0 && i <= |a|
invariant |result| == i
invariant forall j : int :: j >= 0 && j < |result| && j % 3 == 0 ==> result[j] == a[j] * a[j]
invariant forall j : int :: j >= 0 && j < |result| && j % 4 == 0 && j % 3 != 0 ==> result[j] == a[j] * a[j] * a[j]
{
if i % 3 == 0 {
result := result + [a[i] * a[i]];
} else if i % 4 == 0 && i % 3 != 0 {
result := result + [a[i] * a[i] * a[i]];
} else {
result := result + [a[i]];
}
i := i + 1;
}
}
5 changes: 5 additions & 0 deletions 157-right_angle_triangle.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
method right_angle_triangle(a : int, b : int, c : int) returns (result : bool)
ensures result == (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a)
{
result := a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a;
}
12 changes: 12 additions & 0 deletions 159-eat.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
method eat(number : int, need : int, remaining : int) returns (result: seq<int>)
requires number >= 0 && need >= 0 && remaining >= 0
ensures |result| == 2
ensures remaining >= need ==> result[0] == number + need && result[1] == remaining - need
ensures remaining < need ==> result[0] == number + remaining && result[1] == 0
{
if remaining < need {
result := [number + remaining, 0];
} else {
result := [number + need, remaining - need];
}
}
36 changes: 36 additions & 0 deletions 163-generate_integers.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
method min(a : int, b : int) returns (m : int)
ensures m == a || m == b
ensures m <= a && m <= b
{
if a < b { m := a; } else { m := b; }
}

method max(a : int, b : int) returns (m : int)
ensures m == a || m == b
ensures m >= a && m >= b
{
if a > b { m := a; } else { m := b; }
}

method generate_integers(a : int, b : int) returns (result: seq<int>)
ensures forall i : int :: i >= 0 && i < |result| ==> result[i] % 2 == 0
ensures forall i : int :: i >= 0 && i < |result| ==> result[i] in [2, 4, 6, 8]
{
var left := min(a, b);
var right := max(a, b);

var lower := max(2, left);
var upper := min(8, right);

result := [];
var i := lower;
while i <= upper
invariant forall i : int :: i >= 0 && i < |result| ==> result[i] % 2 == 0
invariant forall j : int :: j >= 0 && j < |result| ==> result[j] in [2, 4, 6, 8]
{
if i % 2 == 0 {
result := result + [i];
}
i := i + 1;
}
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ Current status:
- [ ] 99. closest_integer
- [ ] 100. make_a_pile
- [ ] 101. words_string
- [ ] 102. choose_num
- [x] 102. choose_num
- [ ] 103. rounded_avg
- [ ] 104. unique_digits
- [ ] 105. by_length
- [ ] 106. f
- [x] 106. f
- [ ] 107. even_odd_palindrome
- [ ] 108. count_nums
- [ ] 109. move_one_ball
Expand Down Expand Up @@ -159,10 +159,10 @@ Current status:
- [ ] 154. cycpattern_check
- [ ] 155. even_odd_count
- [ ] 156. int_to_mini_roman
- [ ] 157. right_angle_triangle
- [x] 157. right_angle_triangle
- [ ] 158. find_max
- [ ] 159. eat
- [x] 159. eat
- [ ] 160. do_algebra
- [ ] 161. solve
- [ ] 162. string_to_md5
- [ ] 163. generate_integers
- [x] 163. generate_integers

0 comments on commit 07b7fb4

Please sign in to comment.