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

How to define gcd using the stdlib? #12

Closed
simonmasson opened this issue Aug 20, 2022 · 0 comments · Fixed by #29
Closed

How to define gcd using the stdlib? #12

simonmasson opened this issue Aug 20, 2022 · 0 comments · Fixed by #29

Comments

@simonmasson
Copy link

I would like to write a simple Gcd function. It would look like this in python3:

def Gcd(a,b):
    if a == b : 
        return a
    if a > b : 
        return Gcd(a-b, b)
    if a < b : 
        return Gcd(a, b-a)

For now, I cannot do conditions and negation between two integers.

I tried to do this:

module Gcd;
  open import Stdlib.Prelude;
  open import Stdlib.Data.Nat.Ord;

  If : Bool -> ℕ → ℕ → ℕ;
  If true x _ := x;
  If false _ x := x;

  gcd : ℕ → ℕ → ℕ;
  gcd a b :=
    If (a == b)
      a
    (If (a > b)
      (gcd a-b b)
    (gcd a b-a));
end;
jonaprieto added a commit that referenced this issue Jan 9, 2023
@jonaprieto jonaprieto linked a pull request Jan 9, 2023 that will close this issue
jonaprieto added a commit that referenced this issue Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant