let p1 n = (n mod 3 = 0);;

let rec p2 n i =
  let num = string_of_int n in
  if i = (String.length num) then false
  else num.[i] = '3' || p2 n (i+1) ;;

let rec numbers n res =
  if n = 0 then res
  else if (p1 n) || (p2 n 0) then numbers (n-1) (n :: res)
  else numbers (n-1) res ;;
                         
let () =
  let n = Scanf.scanf "%d\n" (fun n -> n) in
  let n_lst = numbers n [] in
  List.iter (Printf.printf " %d") n_lst ; print_newline();;
