-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHumanEval76.lean
More file actions
144 lines (125 loc) · 3.98 KB
/
HumanEval76.lean
File metadata and controls
144 lines (125 loc) · 3.98 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
module
public section
/-!
## Implementation
-/
private def isSimplePowerHelper (x n : Nat) (hx : 0 < x) (hn : 1 < n) : Bool :=
if _ : x = 1 then
true
else if hdvd : n ∣ x then
isSimplePowerHelper (x / n) n (Nat.pos_of_dvd_of_pos (Nat.div_dvd_of_dvd hdvd) hx) hn
else
false
termination_by x
decreasing_by
rw [Nat.div_lt_iff_lt_mul (by grind), Nat.lt_mul_iff_one_lt_right hx]
grind
def isSimplePower (x n : Nat) : Bool :=
if _ : n = 0 then
x ≤ 1
else if _ : n = 1 then
x = 1
else if _ : x = 0 then
false
else
isSimplePowerHelper x n (by grind) (by grind)
/-!
## Tests
-/
example : isSimplePower 16 2 = true := by native_decide
example : isSimplePower 143214 16 = false := by native_decide
example : isSimplePower 4 2 = true := by native_decide
example : isSimplePower 9 3 = true := by native_decide
example : isSimplePower 16 4 = true := by native_decide
example : isSimplePower 24 2 = false := by native_decide
example : isSimplePower 128 4 = false := by native_decide
example : isSimplePower 12 6 = false := by native_decide
example : isSimplePower 1 1 = true := by native_decide
example : isSimplePower 1 12 = true := by native_decide
/-!
## Verification
-/
private theorem isSimplePowerHelper_iff :
isSimplePowerHelper x n hx hn ↔ ∃ i, n ^ i = x := by
fun_induction isSimplePowerHelper x n hx hn
· simp
· rename_i ih
rw [ih]
constructor
· rintro ⟨i, hi⟩
refine ⟨i + 1, ?_⟩
simp only [Nat.pow_add, Nat.pow_one]
rw [hi, Nat.div_mul_cancel ‹_›]
· rintro ⟨i, hi⟩
match i with
| 0 => grind
| i + 1 =>
refine ⟨i, ?_⟩
rw [Nat.eq_div_iff_mul_eq_left (by grind) ‹_›]
grind
· simp only [Bool.false_eq_true, false_iff, not_exists]
intro i
match i with
| 0 => grind
| i' + 1 =>
have := Nat.dvd_mul_left n (n ^ i')
grind
theorem isSimplePower_iff :
isSimplePower x n ↔ ∃ i, n ^ i = x := by
fun_cases isSimplePower
· rename_i hn
cases hn
constructor
· intro h
match x with
| 0 => exact ⟨1, by grind⟩
| 1 => exact ⟨0, by grind⟩
| _ + 2 => grind
· rintro ⟨i, hi⟩
match i with
| 0 | _ + 1 => grind
· simp_all [eq_comm]
· simp_all
· apply isSimplePowerHelper_iff
/-!
## Prompt
```python3
def is_simple_power(x, n):
"""Your task is to write a function that returns true if a number x is a simple
power of n and false in other cases.
x is a simple power of n if n**int=x
For example:
is_simple_power(1, 4) => true
is_simple_power(2, 2) => true
is_simple_power(8, 2) => true
is_simple_power(3, 2) => false
is_simple_power(3, 1) => false
is_simple_power(5, 3) => false
"""
```
## Canonical solution
```python3
if (n == 1):
return (x == 1)
power = 1
while (power < x):
power = power * n
return (power == x)
```
## Tests
```python3
def check(candidate):
# Check some simple cases
assert candidate(16, 2)== True, "This prints if this assert fails 1 (good for debugging!)"
assert candidate(143214, 16)== False, "This prints if this assert fails 1 (good for debugging!)"
assert candidate(4, 2)==True, "This prints if this assert fails 1 (good for debugging!)"
assert candidate(9, 3)==True, "This prints if this assert fails 1 (good for debugging!)"
assert candidate(16, 4)==True, "This prints if this assert fails 1 (good for debugging!)"
assert candidate(24, 2)==False, "This prints if this assert fails 1 (good for debugging!)"
assert candidate(128, 4)==False, "This prints if this assert fails 1 (good for debugging!)"
assert candidate(12, 6)==False, "This prints if this assert fails 1 (good for debugging!)"
# Check some edge cases that are easy to work out by hand.
assert candidate(1, 1)==True, "This prints if this assert fails 2 (also good for debugging!)"
assert candidate(1, 12)==True, "This prints if this assert fails 2 (also good for debugging!)"
```
-/