-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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|) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters