본문 바로가기

Julia

오일러 프로젝트 10번 (Julia)

이백만 이하 소수의 합

범위가 정해져있는 구간의 소수는 에라토스테네스의 체를 사용하면 가장 빠르게 구할 수 있다.

@timev let n = 200_0000
         s = vcat([false], trues(n-1))
         for i=2:Int(floor(√n + 0.5))
           s[i] && (s[2i:i:n] = falses((n - i) ÷ i))
         end
         [i for (i,x)=enumerate(s) if x] |> sum |> println
       end

수행결과

142913828922
  0.061972 seconds (88.16 k allocations: 9.983 MiB)
elapsed time (ns): 61972000
bytes allocated:   10467893
pool allocs:       88105
non-pool GC allocs:46
malloc() calls:    9
realloc() calls:   1