-
Notifications
You must be signed in to change notification settings - Fork 13
Adding 92, 94, 97 #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -9,7 +9,141 @@ use vstd::prelude::*; | |
|
|
||
| verus! { | ||
|
|
||
| // TODO: Put your solution (the specification, implementation, and proof) to the task here | ||
| pub open spec fn is_prime_spec(n: nat) -> bool { | ||
| (n > 1) && forall|i| 1 < i < n ==> #[trigger] (n % i) != 0 | ||
| } | ||
|
|
||
| pub closed spec fn is_prime_so_far(n: nat, k: nat) -> bool | ||
| recommends | ||
| n >= k > 1, | ||
| { | ||
| forall|i| 1 < i < k ==> #[trigger] (n % i) != 0 | ||
| } | ||
|
|
||
| fn is_prime(n: u32) -> (res: bool) | ||
| ensures | ||
| res == is_prime_spec(n as nat), | ||
| { | ||
| if n < 2 { | ||
| return false; | ||
| } | ||
| let mut k = 2; | ||
| let mut res = true; | ||
|
|
||
| while (k < n) | ||
| invariant | ||
| 2 <= k <= n, | ||
| res == is_prime_so_far(n as nat, k as nat), | ||
| decreases n - k, | ||
| { | ||
| assert((is_prime_so_far(n as nat, k as nat) && (n as nat) % (k as nat) != 0) | ||
| == is_prime_so_far(n as nat, (k + 1) as nat)); | ||
|
|
||
| res = res && n % k != 0; | ||
| k = k + 1; | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| pub open spec fn spec_sum_digits(n: nat) -> nat | ||
| decreases n, | ||
| { | ||
| if n < 10 { | ||
| n | ||
| } else { | ||
| (n % 10) + spec_sum_digits(n / 10) | ||
| } | ||
| } | ||
|
|
||
| pub proof fn sum_digits_decreases(x: nat) | ||
| requires | ||
| x >= 10, | ||
| ensures | ||
| spec_sum_digits(x) <= x, | ||
| decreases x, | ||
| { | ||
| let sub_x = x / 10; | ||
| let inc_x = x % 10; | ||
|
|
||
| if sub_x >= 10 { | ||
| sum_digits_decreases(sub_x); | ||
| assert(inc_x + sub_x <= x + 10); | ||
| } else { | ||
| assert(inc_x + spec_sum_digits(sub_x) <= sub_x + 10); | ||
| } | ||
| } | ||
|
|
||
| fn sum_digits(n: u32) -> (count: u32) | ||
| ensures | ||
| count == spec_sum_digits(n as nat), | ||
| decreases n, | ||
| { | ||
| if n < 10 { | ||
| n | ||
| } else { | ||
| assert(spec_sum_digits((n as nat)) <= n) by { sum_digits_decreases(n as nat) }; | ||
| (n % 10) + sum_digits(n / 10) | ||
| } | ||
| } | ||
|
|
||
| pub open spec fn spec_find_largest_prime_so_far(s: Seq<nat>, i: nat) -> nat | ||
| recommends | ||
| 0 <= i <= s.len(), | ||
| decreases i, | ||
| { | ||
| if i <= 0 { | ||
| 0 | ||
| } else { | ||
| let first_i = s.take(i as int); // first i numbers | ||
|
|
||
| // largest of the first i-i numbers in the list | ||
| let largest_in_front = spec_find_largest_prime_so_far(s, (i - 1) as nat); | ||
|
|
||
| // is the i'th number prime and bigger than the largest of the first i-1 numbers? | ||
| if is_prime_spec(first_i.last()) && first_i.last() > largest_in_front { | ||
| first_i.last() | ||
| } else { | ||
| largest_in_front | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub open spec fn spec_find_largest_prime(s: Seq<nat>) -> nat { | ||
| spec_find_largest_prime_so_far(s, s.len()) | ||
| } | ||
|
|
||
| fn find_largest_prime(lst: Vec<u32>) -> (ret: u32) | ||
| ensures | ||
| ret == spec_find_largest_prime([email protected]_values(|val: u32| val as nat)), | ||
| { | ||
| let mut largest_prime = 0; | ||
| for i in 0..lst.len() | ||
| invariant | ||
| largest_prime == 0 || is_prime_spec(largest_prime as nat), | ||
| largest_prime == spec_find_largest_prime_so_far( | ||
| [email protected]_values(|val: u32| val as nat), | ||
| i as nat, | ||
| ), | ||
| { | ||
| let num = lst[i]; | ||
| if is_prime(num) && num > largest_prime { | ||
| largest_prime = num; | ||
| } | ||
| } | ||
| largest_prime | ||
| } | ||
|
|
||
| // note: "skjkasdkd" is the specified function name | ||
| fn skjkasdkd(lst: Vec<u32>) -> (ret: u32) | ||
| ensures | ||
| ret == spec_sum_digits( | ||
| spec_find_largest_prime([email protected]_values(|val: u32| val as nat)) as nat, | ||
| ), | ||
| { | ||
| let largest_prime = find_largest_prime(lst); | ||
| let total = sum_digits(largest_prime); | ||
| total | ||
| } | ||
|
|
||
| } // verus! | ||
| fn main() {} | ||
|
|
@@ -44,6 +178,7 @@ skjkasdkd | |
|
|
||
| /* | ||
| ### CANONICAL SOLUTION | ||
| def skjkasdkd(lst): | ||
| def isPrime(n): | ||
| for i in range(2,int(n**0.5)+1): | ||
| if n%i==0: | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really weird keyboard-mash of a function name. I realize it's the one the prompt chose, but I think it would help to add a comment to that effect, so future readers aren't puzzled by it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed!