let id x = x

module Infseq =
  struct
    type 'a seq = Cons of 'a * (unit -> 'a seq)
    
    let rec nth n sq =
      match (n, sq) with
      | (0, Cons (x, _)) -> x
      | (n, Cons (x, f)) -> nth (n - 1) (f ())
    
    let rec take n sq =
      match (n, sq) with
      | (0, Cons (_, _)) -> []
      | (n, Cons (x, f)) -> x :: take (n - 1) (f ())
    
    let rec drop n sq =
      match (n, sq) with
      | (0, Cons (_, _)) -> sq
      | (n, Cons (x, f)) -> drop (n - 1) (f ())
    
    let rec map g (Cons (x, f)) = Cons (g x, fun () -> map g (f ()))
  end

let rec prime x = 
  let rec is_dividable m l =
    (m >= l * l) && ((x mod l = 0) || is_dividable m (l + 1)) in
  if is_dividable x 2 then prime (x + 1) else Infseq.Cons (x, fun () -> prime (x + 1))

let _ = 
  let rec loop () = 
    let n = Scanf.scanf "%d\n" id in
    if n = 0 then ()
    else
      begin
        Printf.printf "%d\n" (List.fold_left (+) 0 (Infseq.take n (prime 2)));
        loop ()
      end
  in loop ()