How to resolve the algorithm Sieve of Eratosthenes step by step in the M2000 Interpreter programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sieve of Eratosthenes step by step in the M2000 Interpreter programming language

Table of Contents

Problem Statement

The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer.

Implement the   Sieve of Eratosthenes   algorithm, with the only allowed optimization that the outer loop can stop at the square root of the limit, and the inner loop may start at the square of the prime just found. That means especially that you shouldn't optimize by using pre-computed wheels, i.e. don't assume you need only to cross out odd numbers (wheel based on 2), numbers equal to 1 or 5 modulo 6 (wheel based on 2 and 3), or similar wheels based on low primes. If there's an easy way to add such a wheel based optimization, implement it as an alternative version.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sieve of Eratosthenes step by step in the M2000 Interpreter programming language

Source code in the m2000 programming language

Module EratosthenesSieve (x) {
      \\ Κόσκινο του Ερατοσθένη
      Profiler
      If x>2000000 Then Exit
      Dim i(x+1): k=2: k2=sqrt(x)
      While k<=k2{if i(k) else for m=k*k to x step k{i(m)=1}
      k++}
      Print str$(timecount/1000,"####0.##")+" s"
      Input "Press enter skip print or a non zero to get results:", a%
      if a% then For i=2to x{If i(i)=0 Then Print i, 
      }
      Print:Print "Done"
}
EratosthenesSieve 1000

  

You may also check:How to resolve the algorithm Show the epoch step by step in the Pascal programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Population count step by step in the Wren programming language
You may also check:How to resolve the algorithm Count in octal step by step in the Java programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the BBC BASIC programming language