re: Project Euler #5 - Finding the Smallest Multiple VIEW POST

FULL DISCUSSION
 

Here's mine

C

#include <stdio.h>

unsigned long gcd(unsigned long a, unsigned long b) {
 while (a != 0) {
   unsigned long c = a;
   a = b % a;
   b = c;
 }
 return b;
}

unsigned long lcm(unsigned long a, unsigned long b) {
  return a * ( b / gcd(a, b) );
}

int main () {
  unsigned int i = 2;
  unsigned long result = 1;
  for (i = 2; i < 20; i++) {
    result = lcm(result, i);
  }
  printf("%d\n", result);
}

And Go

package main

import (
    "fmt"
)

func gcd(a, b int64) int64 {
    for b != 0 {
        a, b = b, a%b
    }
    return b
}

func lcm(a, b int64) int64 {
    return a * (b / gcd(a, b))
}

func main() {
    var result, i int64 = 1, 2
    for ; i <= 20; i++ {
        result = lcm(result, i)
    }
    fmt.Println(result)
}
code of conduct - report abuse