結果

問題 No.713 素数の和
ユーザー ichibanshiboriichibanshibori
提出日時 2018-09-16 02:04:28
言語 OCaml
(5.1.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 877 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 20,848 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-17 08:44:16
合計ジャッジ時間 777 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

let search_prime_num n =
  let arr = Array.make (n - 1) true in
  
  let rec search_prime_num' i =
    if i <= n then (
      if arr.(i - 2) then (
        let rec mark_comp_num j =
          let k = i * j in
          if k <= n then (
            if arr.(k - 2) then arr.(k - 2) <- false;
            mark_comp_num (j + 1)
          )
        in
        mark_comp_num 2
      );
      search_prime_num' (i + 1)
    )
  in
  search_prime_num' 2;
  arr

let sum_primes arr =
  let len_arr = Array.length arr in
  let rec sum_primes' i result =
    if i >= len_arr then
      result
    else
      let result' =
        match arr.(i) with
        | true -> result + i + 2
        | false -> result
      in
      sum_primes' (i + 1) result'
  in
  sum_primes' 0 0

let () =
  let n = read_line () |> int_of_string in
  search_prime_num n
  |> sum_primes
  |> Printf.printf "%d\n"
0