forked from leanprover/human-eval-lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumanEval24.lean
More file actions
135 lines (114 loc) · 4.2 KB
/
HumanEval24.lean
File metadata and controls
135 lines (114 loc) · 4.2 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
/-- Computes the largest divisor in O(sqrt n) via trial divison. -/
def largestDivisor (n : Nat) : Nat :=
go 2
where
go (i : Nat) : Nat :=
if h : n < i * i then
1
else if n % i = 0 then
n / i
else go (i + 1)
termination_by n - i
decreasing_by
have : i < n := by
match i with
| 0 => omega
| 1 => omega
| i + 2 => exact Nat.lt_of_lt_of_le (Nat.lt_mul_self_iff.2 (by omega)) (Nat.not_lt.1 h)
omega
example : largestDivisor 3 = 1 := by native_decide
example : largestDivisor 7 = 1 := by native_decide
example : largestDivisor 10 = 5 := by native_decide
example : largestDivisor 100 = 50 := by native_decide
example : largestDivisor 49 = 7 := by native_decide
inductive LargestDivisorSpec (n : Nat) : Nat → Prop
| one : (∀ j, 2 ≤ j → j * j ≤ n → n % j ≠ 0) → LargestDivisorSpec n 1
| div {i} : (∀ j, 2 ≤ j → j < i → n % j ≠ 0) → 2 ≤ i → n % i = 0 → LargestDivisorSpec n (n / i)
theorem largestDivisorSpec_go {n i : Nat} (hi : 2 ≤ i)
(hi' : ∀ j, 2 ≤ j → j < i → n % j ≠ 0) : LargestDivisorSpec n (largestDivisor.go n i) := by
fun_induction largestDivisor.go n i
case case1 i hni =>
have := @Nat.mul_self_lt_mul_self_iff i
grind [LargestDivisorSpec.one]
case case2 i hni hni' =>
grind [LargestDivisorSpec.div]
case case3 i hni hni' ih =>
grind
theorem largestDivisorSpec_largestDivisor {n : Nat} :
LargestDivisorSpec n (largestDivisor n) := by
rw [largestDivisor]
exact largestDivisorSpec_go (Nat.le_refl _) (by omega)
theorem Nat.lt_mul_iff {a b : Nat} : a < a * b ↔ 0 < a ∧ 1 < b := by
obtain (rfl|ha) := Nat.eq_zero_or_pos a
· simp
· simp [ha]
theorem mod_ne_zero_of_forall_of_div_lt {n i : Nat} (hn : 1 < n)
(h : ∀ j, 2 ≤ j → j < i → n % j ≠ 0) (hi : n % i = 0) (k : Nat) (hk : n / i < k) (hk₁ : k < n) :
n % k ≠ 0 := by
intro hk₂
obtain ⟨d, rfl⟩ := Nat.dvd_of_mod_eq_zero hk₂
rw [Nat.lt_mul_iff] at hk₁
have hi₀ : 0 < i := by
apply Nat.pos_iff_ne_zero.2
rintro rfl
simp only [Nat.mod_zero] at hi
omega
refine h d ?_ ?_ (by simp)
· omega
· rw [Nat.div_lt_iff_lt_mul hi₀] at hk
exact Nat.lt_of_mul_lt_mul_left hk
theorem mod_ne_zero_of_forall_of_lt_mul_self {n : Nat} (hn : 1 < n)
(h : ∀ j, 2 ≤ j → j * j ≤ n → n % j ≠ 0) (k : Nat) (hk : n < k * k) (hk₁ : k < n) :
n % k ≠ 0 := by
intro hk₂
obtain ⟨d, rfl⟩ := Nat.dvd_of_mod_eq_zero hk₂
rw [Nat.lt_mul_iff] at hk₁
refine h d ?_ ?_ (by simp)
· omega
· refine Nat.mul_le_mul_right _ (Nat.le_of_lt ?_)
exact Nat.lt_of_mul_lt_mul_left hk
theorem largestDivisorSpec.out {n i : Nat} (hn : 1 < n) :
LargestDivisorSpec n i → i < n ∧ n % i = 0 ∧ ∀ j, j < n → n % j = 0 → j ≤ i := by
rintro (h|h)
· refine ⟨hn, Nat.mod_one _, fun j hj hj₁ => Nat.not_lt.1 (fun hj₂ => ?_)⟩
by_cases hj₃ : j * j ≤ n
· exact h j (by omega) hj₃ hj₁
· exact mod_ne_zero_of_forall_of_lt_mul_self hn h j (by omega) hj hj₁
· rename_i i hi hi'
refine ⟨?_, ?_, fun j hj hj₁ => ?_⟩
· apply Nat.div_lt_self <;> omega
· exact Nat.mod_eq_zero_of_dvd (Nat.div_dvd_of_dvd (Nat.dvd_of_mod_eq_zero hi'))
· exact Nat.not_lt.1 (fun hj₂ => mod_ne_zero_of_forall_of_div_lt hn h hi' j hj₂ hj hj₁)
theorem largestDivisor_eq_iff {n i : Nat} (hn : 1 < n) :
largestDivisor n = i ↔ i < n ∧ n % i = 0 ∧ ∀ j, j < n → n % j = 0 → j ≤ i := by
have h := largestDivisorSpec.out hn (by exact largestDivisorSpec_largestDivisor)
exact ⟨fun h' => h' ▸ h, fun hi => Nat.le_antisymm (hi.2.2 _ h.1 h.2.1) (h.2.2 _ hi.1 hi.2.1)⟩
/-!
## Prompt
```python3
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
"""
```
## Canonical solution
```python3
for i in reversed(range(n)):
if n % i == 0:
return i
```
## Tests
```python3
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
```
-/