ScalaでProject Euler(40)

Problem 23

本当は6の剰余で分けて考えると速いのですが、何も考えずに書いても十分に速いです。

def sieve(N :Int) = {
    val a = Array.range(0, N)
    for(p <- Iterator.from(2).takeWhile(n => n * n < N).filter(n => a(n) == n);
                    k <- Iterator.range(p * 2, N, p))
        a(k) = p
    a
}

def div_pow(n :Int, d :Int) :(Int,Int) =
    if(n % d != 0)
        (0, n)
    else {
        val (e, m) = div_pow(n / d, d)
        (e + 1, m)
    }

def sum_pows(n :Int, e :Int) =
    (1 to e).foldLeft(1)((x, y) => x * n + 1)

def sum_divs(n :Int) :Int =
    if(n == 1)
        1
    else {
        val p = a(n)
        val (e, m) = div_pow(n, p)
        sum_pows(p, e) * sum_divs(m)
    }

def is_abundant(n :Int) = sum_divs(n) - n > n

val N = 28123
val a = sieve(N)
val b = Iterator.range(2, N).filter(is_abundant).toArray

val c = new Array[Boolean](N + 1)
for(n <- b; m <- b.takeWhile(N - n >=))
    c(n + m) = true
println (Iterator.range(1, N + 1).filter(!c(_)).sum)