Problem: trial_division_optimized

Your task is to implement the "Optimized Trial Division" primality test algorithm and verify its functional correctness in Lean. Given a non-negative integer n, determine whether n is prime by dividing it by integers from 2 up to sqrt(n). Postconditions: Returns true iff n > 1 and has no divisors between 2 and n-1. Preconditions: n is a natural number (Nat). Verification Challenges: (1) Algorithm: Stop checking when i * i > n. (2) Proof Requirement: You must prove that if no divisor is found up to sqrt(n), then no divisor exists up to n-1. This requires stating and proving the number-theoretic lemma: if n is composite, it has a factor less than or equal to its square root.

import Mathlib

-- Precondition definitions
@[reducible, simp]
def check_prime_precond (n : Nat) : Prop :=
  -- !benchmark @start precond
  True
  -- !benchmark @end precond

-- !benchmark @start auxcode
-- !benchmark @end auxcode

-- Main function definitions
def check_prime (n : Nat) (h_precond : check_prime_precond n) : Bool :=
  -- !benchmark @start code
  sorry
  -- !benchmark @end code

-- Postcondition definitions
@[reducible, simp]
def check_prime_postcond (n : Nat) (result : Bool) (h_precond : check_prime_precond n) : Prop :=
  -- !benchmark @start postcond
  result = true ↔ (n > 1 ∧ ∀ d : Nat, 1 < d ∧ d < n → n % d ≠ 0)
  -- !benchmark @end postcond

-- !benchmark @start lemma
-- !benchmark @end lemma

-- Proof content
theorem check_prime_postcond_satisfied (n : Nat) (h_precond : check_prime_precond n) :
    check_prime_postcond n (check_prime n h_precond) h_precond := by
  -- !benchmark @start proof
  sorry
  -- !benchmark @end proof