Zulip Chat Archive
Stream: Is there code for X?
Topic: count how many recursion
Asei Inoue (Sep 26 2024 at 13:26):
This is a my trial to count recursion call of gcd
function.
I use fuel
trick here to count recursion, but this way has many problems.
def mygcd' (m n fuel : Nat) : Option Nat :=
match fuel with
| 0 => none
| fuel + 1 =>
if h : m = 0 then
n
else
have : n % m < m := by
apply mod_lt
omega
mygcd' (n % m) m fuel
theorem gcd_bound (m n fuel : Nat) (h : m < fuel) : (mygcd' m n fuel).isSome := by
induction fuel generalizing m n with
| zero =>
simp_all
| succ fuel ih =>
dsimp [mygcd']
split
case isTrue hm => simp
case isFalse hm =>
have : n % m < m := by
apply mod_lt
omega
replace : n % m < fuel := by omega
replace ih := ih (n % m) m this
assumption
Asei Inoue (Sep 26 2024 at 13:29):
The following methods are being considered
-
define a function
T
that counts the number of times to recur byT(0, n) = 0, T(m, n) = T(n % m, m)+1
to show an upper bound forT
. We give up on giving a rigorous proof. -
define the
mygcd
function by reference to the gcd function, so that the logic of gcd does not have to be re-implemented inmygcd
.
Bjørn Kjos-Hanssen (Sep 27 2024 at 19:13):
How about just proving that gcd_bound
is sharp?
theorem gcd_bound_lower : ∃ m n fuel, m ≥ fuel ∧ (mygcd' m n fuel) = none := by
sorry
Last updated: May 02 2025 at 03:31 UTC