Project Euler #116: Red, green or blue tiles

  • + 0 comments

    Swift. [Segmentation Fault]

    ~Don't know what to do..

    var factorials = [1:1]
    func factorial(n: Int) -> Int{
        if let fact = factorials[n]{
            return fact
        }
        if n > 0{
            let r = n*factorial(n: n-1)
            factorials[n] = r
            return r
        }
        return 1
    }
    func getTiles(n: Int, m: Int) -> Int{
        var result = 0
        for i in 1...n/m{
            let tile = i
            let empty = n-(i*m)
            let sum = tile+empty
            result += factorial(n: sum)/(factorial(n: tile)*factorial(n: empty))
        }
        return result
    }
    func ways(n: Int) -> Int{
        return getTiles(n: n, m: 2)+getTiles(n: n, m: 3)+getTiles(n: n, m: 4)
    }