• + 0 comments

    js

    function redJohn(n) {
        const r = x => Math.round(x)
        const s = x => Math.sqrt(x)
        
        const fact = (n) => n <= 1 ? 1 : fact(n - 1) * n
        
        const prime = (n) => { 
            if (n % 2 == 0 && n > 2) 
                return false
            for (let i=3; i <= r(s(n)); i+=2)
                if (n % i == 0)
                    return false
            return true
        }
        
        let M = 0
        for (let i=0; i <= n/4; i++) {
            M += r(fact(n-i*3) / (fact(i) * fact(n-i*4)))
        }
        let res = 0
        for (let i=2; i <= M; i++)
            if (prime(i))
                res++
        return res
    }