Skip to content

Commit

Permalink
77, 78, 82, 83 (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeetHet authored Aug 12, 2024
1 parent 8400457 commit 7f130af
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
30 changes: 30 additions & 0 deletions 077-is_cube.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
method cube_root(N: nat) returns (r: nat)
ensures cube(r) <= N < cube(r + 1)
ensures r <= N
{
r := 0;
while cube(r + 1) <= N
invariant cube(r) <= N
{
r := r + 1;
}
}

method is_cube(n: nat) returns (r: bool)
ensures r ==> exists r :: 0 <= r <= n && n == cube(r)
ensures !r ==> forall r :: 0 <= r <= n ==> n != cube(r)
{
var root := cube_root(n);
if cube(root) == n {
r := true;
} else {
cube_of_larger_is_larger();
r := false;
}
}

function cube(n: int): int { n * n * n }

lemma cube_of_larger_is_larger()
ensures forall smaller : int, larger : int :: smaller <= larger ==> cube(smaller) <= cube(larger)
{}
45 changes: 45 additions & 0 deletions 078-hex_key.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function IsPrimeHexDigit(c: char): bool
{
c == '2' || c == '3' || c == '5' || c == '7' || c == 'B' || c == 'D'
}

function count_prime_hex_digits_rec(num: seq<char>) : (count : int)
ensures 0 <= count <= |num|
{
if |num| == 0 then 0
else (if IsPrimeHexDigit(num[0]) then 1 else 0) + count_prime_hex_digits_rec(num[1..])
}

lemma count_prop(s: seq<char>)
requires |s| > 0
ensures count_prime_hex_digits_rec(s) == count_prime_hex_digits_rec(s[..|s| - 1]) + (
if IsPrimeHexDigit(s[ |s| - 1 ]) then 1 else 0
)
{
if (|s| > 1) {
assert (s[1..][..|s[1..]| - 1]) == s[1..|s| - 1];
}
}

method count_prime_hex_digits(s: seq<char>) returns (count : int)
ensures count == count_prime_hex_digits_rec(s)
ensures 0 <= count <= |s|
{
count := 0;
var i := 0;
while(i < |s|)
invariant 0 <= i <= |s|
invariant count == count_prime_hex_digits_rec(s[..i])
{
assert count_prime_hex_digits_rec(s[..i + 1]) == count_prime_hex_digits_rec(s[..i]) + (
if IsPrimeHexDigit(s[ i ]) then 1 else 0
) by {
assert s[..i+1][..i] == s[..i];
count_prop(s[..i + 1]);
}
count := count + if IsPrimeHexDigit(s[i]) then 1 else 0;
i := i + 1;
}
assert s[..i] == s;
return count;
}
11 changes: 11 additions & 0 deletions 082-prime_length.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
predicate IsPrime(n: int)
{
n > 1 &&
forall k :: 2 <= k < n ==> n % k != 0
}

function PrimeLength(s: string): bool
ensures PrimeLength(s) <==> IsPrime(|s|)
{
IsPrime(|s|)
}
18 changes: 18 additions & 0 deletions 083-starts_one_ends.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
method CountNumbersStartingOrEndingWithOne(n: nat) returns (count: nat)
requires n > 0
ensures n == 1 ==> count == 1
ensures n > 1 ==> count == 18 * Pow(10, n - 2)
{
if n == 1 {
count := 1;
} else {
count := 18 * Pow(10, n - 2);
}
}

function Pow(base: nat, exponent: nat): nat
ensures exponent == 0 ==> Pow(base, exponent) == 1
ensures exponent > 0 ==> Pow(base, exponent) == base * Pow(base, exponent-1)
{
if exponent == 0 then 1 else base * Pow(base, exponent-1)
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ Current status:
- [ ] 74. total_match
- [ ] 75. is_multiply_prime
- [ ] 76. is_simple_power
- [ ] 77. iscube
- [ ] 78. hex_key
- [x] 77. iscube
- [x] 78. hex_key
- [ ] 79. decimal_to_binary
- [ ] 80. is_happy
- [ ] 81. numerical_letter_grade
- [ ] 82. prime_length
- [ ] 83. starts_one_ends
- [x] 82. prime_length
- [x] 83. starts_one_ends
- [ ] 84. solve
- [x] 85. add
- [ ] 86. anti_shuffle
Expand Down

0 comments on commit 7f130af

Please sign in to comment.