Skip to content
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

025 #39

Merged
merged 1 commit into from
Sep 2, 2024
Merged

025 #39

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions 025-factorize.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function prod(s: seq<int>) : int {
if |s| == 0 then 1 else prod(s[..|s| - 1]) * s[|s| - 1]
}

method factorize(n: nat) returns (factors: seq<nat>)
requires n > 0
ensures prod(factors) == n
{
factors := [];
ghost var taken := 1;
var cur := n;
var i := 2;
while i * i <= cur
invariant prod(factors) == taken
invariant taken * cur == n
invariant cur >= 1
{
ghost var pre := cur;
ghost var temp := 1;
while cur % i == 0
invariant cur >= 1
invariant temp * cur == pre
invariant prod(factors) == taken * temp
decreases cur - 1
{
factors := factors + [i];

cur := cur / i;
temp := temp * i;
assert 2 <= i && 2 * cur <= i * cur;
}
taken := taken * temp;
i := i + 1;
}
if cur > 1 {
factors := factors + [cur];
taken := taken * cur;
}
assert taken == n;
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Current status:
- [ ] 22. filter_integers - ill-typed
- [x] 23. strlen
- [x] 24. largest_divisor
- [ ] 25. factorize
- [x] 25. factorize
- [x] 26. remove_duplicates
- [x] 27. flip_case
- [ ] 28. concatenate
Expand Down
1 change: 0 additions & 1 deletion REMAINING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Current status:

## Priority
- [ ] 25. factorize
- [ ] 147. get_max_triples

## Other
Expand Down