Skip to content

How to define gcd using the stdlib? #12

@simonmasson

Description

@simonmasson

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions