Repository navigation
Expand file tree
/
Copy pathhuman_eval_024.rs
More file actions
151 lines (128 loc) · 3.04 KB
/
Copy pathhuman_eval_024.rs
File metadata and controls
151 lines (128 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
### ID
HumanEval/24
*/
/*
### VERUS BEGIN
*/
use vstd::arithmetic::div_mod::{
lemma_fundamental_div_mod, lemma_fundamental_div_mod_converse_div,
};
use vstd::prelude::*;
verus! {
pub open spec fn mul(a: nat, b: nat) -> nat {
vstd::prelude::mul(a, b)
}
/// Specification for what it means for d to divide a
pub open spec fn divides(factor: nat, candidate: nat) -> bool {
exists|k: nat| mul(factor, k) == candidate
}
/// Helper function to prove a % b == 0 imples b divides a
proof fn lemma_mod_zero(a: nat, b: nat)
requires
a > 0 && b > 0,
a % b == 0,
ensures
divides(b, a),
{
lemma_fundamental_div_mod(a as int, b as int);
assert(mul(b, (a / b)) == a);
}
/// Helper function to prove b divides a imples a % b == 0
proof fn lemma_mod_zero_reversed(a: nat, b: nat)
requires
a > 0 && b > 0,
divides(b, a),
ensures
a % b == 0,
{
let k_wit = choose|k: nat| mul(b, k) == a;
assert(k_wit == a / b) by {
lemma_fundamental_div_mod_converse_div(a as int, b as int, k_wit as int, 0 as int);
}
lemma_fundamental_div_mod(a as int, b as int);
}
/// Helper function to prove everything is divided by one
proof fn lemma_one_divides_all()
ensures
forall|v: nat| divides(1 as nat, v),
{
assert forall|v: nat| divides(1 as nat, v) by {
assert(mul(1 as nat, v) == v);
}
}
/// Implementation.
fn largest_divisor(n: u32) -> (ret: u32)
requires
n > 1,
ensures
divides(ret as nat, n as nat),
ret < n,
forall|k: u32| (0 < k < n && divides(k as nat, n as nat)) ==> ret >= k,
{
let mut i = n - 1;
while i >= 2
invariant
n > 0,
i < n,
forall|k: u32| i < k < n ==> !divides(k as nat, n as nat),
decreases i,
{
if n % i == 0 {
assert(divides(i as nat, n as nat)) by {
lemma_mod_zero(n as nat, i as nat);
}
return i;
}
i -= 1;
assert forall|k: u32| i < k < n implies !divides(k as nat, n as nat) by {
if k == i + 1 {
assert(!divides(k as nat, n as nat)) by {
if (divides(k as nat, n as nat)) {
lemma_mod_zero_reversed(n as nat, k as nat);
}
}
}
}
}
assert(divides(1 as nat, n as nat)) by {
lemma_one_divides_all();
}
1
}
} // verus!
fn main() {}
/*
### VERUS END
*/
/*
### PROMPT
def largest_divisor(n: int) -> int:
""" For a given number n, find the largest number that divides n evenly, smaller than n
>>> largest_divisor(15)
5
"""
*/
/*
### ENTRY POINT
largest_divisor
*/
/*
### CANONICAL SOLUTION
for i in reversed(range(n)):
if n % i == 0:
return i
*/
/*
### TEST
METADATA = {
'author': 'jt',
'dataset': 'test'
}
def check(candidate):
assert candidate(3) == 1
assert candidate(7) == 1
assert candidate(10) == 5
assert candidate(100) == 50
assert candidate(49) == 7
*/