DEV Community

Discussion on: Daily Challenge #78 - Number of Proper Fractions with Denominator d

Collapse
colorfusion profile image
Melvin Yeo

My first time writing in Golang, managed to figure out the basic syntax.

package main

func gcd(a int, b int) int {
    if b == 0 {
        return a
    }

    return gcd(b, a%b)
}

func properFractions(denom int) int {
    results := 0

    for i := 1; i < denom; i++ {
        if gcd(denom, i) == 1 {
            results++
        }
    }

    return results
}

func main() {

}

For testing

package main

import "testing"

func TestProperFractions(t *testing.T) {
    params := map[int]int{
        1:  0,
        2:  1,
        5:  4,
        15: 8,
        25: 20,
    }
    for key, value := range params {
        result := properFractions(key)

        if result != value {
            t.Errorf("Incorrect answer for %d, expected: %d, got: %d", key, value, result)
        }
    }
}