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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sieve of Eratosthenes step by step in the BQN 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 BQN programming language

Source code in the bqn programming language

Primes โ† {
  ๐•ฉโ‰ค2 ? โ†•0 ;             # No primes below 2
  p โ† ๐•ŠโŒˆโˆšnโ†๐•ฉ             # Initial primes by recursion
  b โ† 2โ‰คโ†•n               # Initial sieve: no 0 or 1
  E โ† {โ†•โˆ˜โŒˆโŒพ((๐•ฉร—๐•ฉ+โŠข)โผ)n}  # Multiples of ๐•ฉ under n, starting at ๐•ฉร—๐•ฉ
  / b EโŠธ{0ยจโŒพ(๐•จโŠธโŠ)๐•ฉ}ยด p   # Cross them out
}


   Primes 100
โŸจ 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 โŸฉ
   โ‰ โˆ˜Primesยจ 10โ‹†โ†•7  # Number of primes below 1e0, 1e1, ... 1e6
โŸจ 0 4 25 168 1229 9592 78498 โŸฉ


  

You may also check:How to resolve the algorithm Extend your language step by step in the Scala programming language
You may also check:How to resolve the algorithm Cyclops numbers step by step in the Go programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Hoon programming language
You may also check:How to resolve the algorithm Morse code step by step in the Racket programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Sidef programming language