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 Dafny. Preconditions: The input n is a non-negative integer (simulating usize). You may assume $n \le 100,000$. Requirements: Implement the method sieve_of_eratosthenes which returns a seq<bool>. Output Format: The returned sequence must be of length n. For any index i, primes[i] should be true if i is prime, and false otherwise. Algorithm:  + Initialize a boolean array of size n to true (except 0 and 1).+ Iterate a variable i from 2 up to $\sqrt{n}$. + If i is marked as prime, iterate a variable j starting from i * i up to n with a step size of i (j += i), marking these indices as false (composite). Functional Correctness: You must verify that the final state of the array exactly matches the mathematical definition of primality for every index. Verification Challenges: (1) Array Invariants: Unlike the scalar problems, 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 (j += i). You must prove that every index j visited by this loop is indeed a multiple of i, and that the loop correctly covers all multiples of i in the range $[i^2, n)$. (3) Optimization Logic ($i^2$): The standard Sieve starts marking multiples from $i^2$, not $2i$. To verify this is safe, you must use (or prove) the mathematical fact that any composite number $x < i^2$ must have a prime factor strictly smaller than i, and thus would have already been marked in a previous iteration.

// Following is the block for necessary definitions
// <preamble>
predicate divides(d: int, n: int)
  requires d != 0
{
  n % d == 0
}

predicate is_prime(n: int)
{
  n > 1 &&
  forall d :: 1 < d < n ==> !divides(d, n)
}
// </preamble>

// Following is the block for potential helper specifications
// <helpers>

// </helpers>

// Following is the block for proofs of lemmas
// <proofs>

// </proofs>

// Following is the block for the main specification
// <spec>
method sieve_of_eratosthenes(n: int) returns (primes: seq<bool>)
  // Simulate usize (non-negative) and bound constraint
  requires 0 <= n <= 100_000
  
  ensures |primes| == n
  // Functional Correctness: 
  // Dafny automatically triggers on primes[i] here because it appears in the body.
  ensures forall i :: 0 <= i < n ==> primes[i] == is_prime(i)
// </spec>
// <code>
{
  // Implement and verify the Sieve of Eratosthenes algorithm
  assume {:axiom} false;
}
// </code>

method Main() {
}