DEV Community

Discussion on: Daily Challenge #102 - Pentabonacci

Collapse
colorfusion profile image
Melvin Yeo

In Golang, assuming input is a valid integer.

package main

var fibMap = map[int]int{
    0: 0,
    1: 1,
    2: 1,
    3: 2,
    4: 4,
}

func f(n int) int {
    if n <= 4 {
        return fibMap[n]
    }

    result := f(n-1) + f(n-2) + f(n-3) + f(n-4) + f(n-5)
    fibMap[n] = result
    return result
}

func countPentafib(n int) int {
    odds := make(map[int]bool)
    for i := 0; i < n; i++ {
        result := f(i)
        if result%2 == 1 {
            odds[result] = true
        }
    }

    return len(odds)
}

func main() {
}

Tests

package main

import "testing"

func TestPentaFib(t *testing.T) {
    params := map[int]int{
        5:  1,
        10: 3,
        15: 5,
    }
    for key, value := range params {
        result := countPentafib(key)

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