# gcd.mul

The gcd program written in MUL, Go, PHP, C and Rust, with the same output and exit code, and the token count of each version.

## Source

`examples/compare/gcd.mul` is stored in the compact layout, as AI agents write it: a space only between two words, no other whitespace. The page lays it out for reading; the Compact button shows the stored text, and Copy always copies it.

```mul
module compare.gcd fn gcd(a:i32,b:i32)->i32{var x=a var y=b while y!=0{let rest=x%y x=y y=rest}return x}pub fn main()->i32 io{print("gcd 1071 462")return gcd(1071,462)}
```

## Verified IR

What `mul ir` prints after the verifier has accepted the program.

```mul-ir
module compare.gcd

fn @gcd(%0: i32, %1: i32) -> i32 {
b0:
  jump b1(%0, %1)
b1(%2: i32, %3: i32):
  %4 = const.i32 0
  %5 = ne.i32 %3, %4
  branch %5, b2, b3
b2:
  %6 = rem.i32 %2, %3
  jump b1(%3, %6)
b3:
  return %2
}

fn @main() -> i32 io {
b0:
  %0 = const.string "gcd 1071 462"
  print %0
  %1 = const.i32 1071
  %2 = const.i32 462
  %3 = call.i32 @gcd(%1, %2)
  return %3
}
```

## Run

Output and exit code of the program in MUL (interpreter and Cranelift), Go, PHP, C and Rust, from `examples/compare/evidence/gcd/run.txt`. The value `main` returns is the exit code.

```console
$ mul run examples/compare/gcd.mul
gcd 1071 462
exit code 21

$ mul run --backend cranelift examples/compare/gcd.mul
gcd 1071 462
exit code 21

$ go build -o gcd examples/compare/gcd.go && ./gcd
gcd 1071 462
exit code 21

$ php examples/compare/gcd.php
gcd 1071 462
exit code 21

$ cc -o gcd examples/compare/gcd.c && ./gcd
gcd 1071 462
exit code 21

$ rustc --edition 2021 -O -o gcd examples/compare/gcd.rs && ./gcd
gcd 1071 462
exit code 21
```

## Tokens

Token count of each file, MUL as the base: first the offline estimate of `mul tokens`, then the same files counted with the real BPE vocabularies (`--tokenizer-cmd`). The estimate applies fixed rules fitted to the pilot study (design document, chapter 23) instead of a learned vocabulary of merges, so it is a few percent off on each file; quote the BPE numbers.

```console
$ mul tokens examples/compare/gcd.mul --compare examples/compare/gcd.go examples/compare/gcd.php examples/compare/gcd.c examples/compare/gcd.rs
tokenizer: estimate
note: offline estimate, not a real tokenizer (about 5% per file on GPT-style BPE); use --tokenizer-cmd for exact counts
file                      layout        tokens      diff         %
examples/compare/gcd.mul  compact           66      base
examples/compare/gcd.go   as written        84       +18    +27.3%
examples/compare/gcd.php  as written        89       +23    +34.8%
examples/compare/gcd.c    as written        99       +33    +50.0%
examples/compare/gcd.rs   as written        85       +19    +28.8%
```

```console
$ mul tokens examples/compare/gcd.mul --compare examples/compare/gcd.go examples/compare/gcd.php examples/compare/gcd.c examples/compare/gcd.rs --tokenizer-cmd "python3 research/token-sample/count.py cl100k_base"
tokenizer: cl100k_base
file                      layout        tokens      diff         %
examples/compare/gcd.mul  compact           66      base
examples/compare/gcd.go   as written        90       +24    +36.4%
examples/compare/gcd.php  as written        91       +25    +37.9%
examples/compare/gcd.c    as written       104       +38    +57.6%
examples/compare/gcd.rs   as written        91       +25    +37.9%
```

```console
$ mul tokens examples/compare/gcd.mul --compare examples/compare/gcd.go examples/compare/gcd.php examples/compare/gcd.c examples/compare/gcd.rs --tokenizer-cmd "python3 research/token-sample/count.py o200k_base"
tokenizer: o200k_base
file                      layout        tokens      diff         %
examples/compare/gcd.mul  compact           67      base
examples/compare/gcd.go   as written        91       +24    +35.8%
examples/compare/gcd.php  as written        92       +25    +37.3%
examples/compare/gcd.c    as written       105       +38    +56.7%
examples/compare/gcd.rs   as written        92       +25    +37.3%
```

## The same program in other languages

`make compare` checks that each of them prints the same output and exits with the same code.

### Go

`examples/compare/gcd.go`

```go
package main

import (
	"fmt"
	"os"
)

func gcd(a int32, b int32) int32 {
	x := a
	y := b
	for y != 0 {
		rest := x % y
		x = y
		y = rest
	}
	return x
}

func main() {
	fmt.Println("gcd 1071 462")
	os.Exit(int(gcd(1071, 462)))
}
```

### PHP

`examples/compare/gcd.php`

```php
<?php

function gcd(int $a, int $b): int
{
    $x = $a;
    $y = $b;
    while ($y != 0) {
        $rest = $x % $y;
        $x = $y;
        $y = $rest;
    }
    return $x;
}

echo "gcd 1071 462\n";
exit(gcd(1071, 462));
```

### C

`examples/compare/gcd.c`

```c
#include <stdint.h>
#include <stdio.h>

static int32_t gcd(int32_t a, int32_t b) {
    int32_t x = a;
    int32_t y = b;
    while (y != 0) {
        int32_t rest = x % y;
        x = y;
        y = rest;
    }
    return x;
}

int main(void) {
    puts("gcd 1071 462");
    return gcd(1071, 462);
}
```

### Rust

`examples/compare/gcd.rs`

```rust
fn gcd(a: i32, b: i32) -> i32 {
    let mut x = a;
    let mut y = b;
    while y != 0 {
        let rest = x % y;
        x = y;
        y = rest;
    }
    x
}

fn main() {
    println!("gcd 1071 462");
    std::process::exit(gcd(1071, 462));
}
```
