User:Blackbombchu/Sandbox/Modified sieve of Eratosthenes

Theorem
Take the list of all positive integers. Suppose that crossing off an integer removes it from the list if it isn't already removed and leaves it gone if it is already removed. For any prime number p, suppose you cross 1 off the list then for all the prime numbers q less than p in increasing order, you cross off all the numbers that can be expressed as q multiplied by a number ≥ q that has not already been crossed off the list, then every number from 1 to p2 - 1 is prime if and only if it hasn't been crossed off the list.

Proof
A positive integer is prime if and only if it is greater than 1 and no integers between 1 and itself are a factor of it. 1 is not prime and is crossed off the list. All positive integers other than 1 that got crossed off the list are composite because there exists a prime number q < p such that that number can be expressed as q multiplied by a number that wasn't crossed off the list before crossing off numbers for q and so q is a factor of it. After you cross off multiples of 2, none of the remaining numbers > 2 in the list are a multiple of 2. For any even number, 3 × that number is a multiple of 2 and so was already crossed off the list so crossing of only numbers that can be expressed 3 × an odd number ≥ 3 gives a list of numbers such that all of the ones > 3 are not a multiple of 2 or 3. Since none of those remaining numbers in the list > 3 are a multiple of 2, they're not a multiple of 4 either.

For each prime number q > 2, suppose that after you cross off number for the prime number before q, the set of all remaining numbers ≥ q on the list is the set of all positive integers > 1 that are not a multiple of any prime number < q, then every number > 1 and < q and those not on the list that ≥ q are a multiple of a smaller prime number than q so q multiplied by any of them equals a number that's a multiple of a smaller prime number than q so after you cross off all numbers that can be expressed as q × a number ≥ q that wasn't already crossed off the list, the set of all remaining numbers > q on the list is the set of all numbers > 1 that are not a multiple of any prime number up to q.

For any composite number r, once numbers have been crossed off for every prime number s < r, if none of the remaining numbers on the list ≥ r are a multiple of any positive integer from 2 to r - 1, then none of the remaining numbers on the list > r are a multiple of r because they're not a multiple of the second smallest factor of r and so are not a multiple of any number from 2 to r.

For any prime number q, if the set of all remaining numbers on the list > q after you cross off numbers for q is the set of all numbers > 1 that are not a multiple of any number from 2 to q, then by induction, for for each composite number after q until the number before the next prime number, the set of all remaining number on the list > that number is the set of all numbers > 1 that are not all multiple of any number from 2 to that number, so after you cross off the numbers that can be expressed as the prime number after q × a number ≥ the prime number after q that hasn't already been crossed off, the set of all remaining numbers > the prime number after q on the list is the set of all numbers > 1 that are not a multiple of any number from 2 to the prime number after q. By induction, for every positive integer r > 2, the set of all remaining numbers > r on the list after crossing off numbers for every prime number up to r is the set of all numbers > 1 that are not a multiple of any number from 2 to r so after you cross off numbers for all prime numbers up to p2 - 1, the set of all remaining numbers in the list from 1 to p2 - 1 is the set of all prime numbers from 1 to p2 - 1. For every prime number q ≥ p, the first number to get crossed off when crossing off numbers for q is q2 which > p2 - 1 so the after you cross off numbers just for all prime numbers < p, the set of remaining numbers in the list from 1 to p2 - 1 is the set of all prime numbers from 1 to p2 - 1.