Problem: sieve_method

Your task is to implement the classic Sieve of Eratosthenes algorithm to find all prime numbers up to a limit n and verify its correctness in Lean. Given a non-negative integer n, return a boolean array of size n where the i-th element is true iff i is prime. Preconditions: The input n is a natural number (Nat). You may assume n <= 100,000. Requirements: Implement the function `sieve_of_eratosthenes` which returns an `Array Bool`. (1) Output Format: The returned array must be of size n. For any index i, result[i] should be true if i is prime, and false otherwise. (2) Algorithm: Initialize a boolean array to true. Iterate i from 2 up to sqrt(n). If i is prime, iterate j starting from i*i up to n with a step size of i (j += i), marking these indices as false. Verification Challenges: (1) Array Invariants: You must maintain an invariant over the entire array. For example: "For all k < n, if arr[k] is true, then k has no prime factors smaller than the current loop index i". (2) Inner Loop Stride: The inner loop increments by i. You must prove that every index j visited by this loop is indeed a multiple of i. (3) Optimization Logic (i*i): The standard Sieve starts marking multiples from i*i. To verify this is safe, you must prove the lemma that any composite number x < i*i must have a prime factor strictly smaller than i.

import Mathlib

-- Precondition definitions
@[reducible, simp]
def sieve_of_eratosthenes_precond (n : Nat) : Prop :=
  -- !benchmark @start precond
  n ≤ 100_000
  -- !benchmark @end precond

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

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

-- Postcondition definitions
@[reducible, simp]
def sieve_of_eratosthenes_postcond (n : Nat) (result : Array Bool) (h_precond : sieve_of_eratosthenes_precond n) : Prop :=
  -- !benchmark @start postcond
  result.size = n ∧
  ∀ i : Nat, i < n →
    ((result.getD i false = true) ↔
      (i > 1 ∧ ∀ d : Nat, (1 < d ∧ d < i) → i % d ≠ 0))
  -- !benchmark @end postcond

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

-- Proof content
theorem sieve_of_eratosthenes_postcond_satisfied (n : Nat) (h_precond : sieve_of_eratosthenes_precond n) :
    sieve_of_eratosthenes_postcond n (sieve_of_eratosthenes n h_precond) h_precond := by
  -- !benchmark @start proof
  sorry
  -- !benchmark @end proof