How to resolve the algorithm Extensible prime generator step by step in the CoffeeScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Extensible prime generator step by step in the CoffeeScript programming language

Table of Contents

Problem Statement

Write a generator of prime numbers, in order, that will automatically adjust to accommodate the generation of any reasonably high prime. The routine should demonstrably rely on either:

The routine should be used to:

Show output on this page. Note: You may reference code already on this site if it is written to be imported/included, then only the code necessary for import and the performance of this task need be shown. (It is also important to leave a forward link on the referenced tasks entry so that later editors know that the code is used for multiple tasks). Note 2: If a languages in-built prime generator is extensible or is guaranteed to generate primes up to a system limit, (231 or memory overflow for example), then this may be used as long as an explanation of the limits of the prime generator is also given. (Which may include a link to/excerpt from, language documentation). Note 3:The task is written so it may be useful in solving the task   Emirp primes   as well as others (depending on its efficiency).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Extensible prime generator step by step in the CoffeeScript programming language

Source code in the coffeescript programming language

primes = () ->
    yield 2
    yield 3

    sieve = ([] for i in [1..3])
    sieve[0].push 3
    [r, s] = [3, 9]
    pos = 1
    n = 5
    loop
        isPrime = true
        if sieve[pos].length > 0  # this entry has a list of factors
            isPrime = false
            sieve[(pos + m) % sieve.length].push m for m in sieve[pos]
            sieve[pos] = []

        if n is s  # n is the next square
            if isPrime
                isPrime = false  # r divides n, so not actually prime
                sieve[(pos + r) % sieve.length].push r  # however, r is prime
            r += 2
            s = r*r

        yield n if isPrime
        n += 2
        pos += 1
        if pos is sieve.length
            sieve.push []  # array size must exceed largest prime found
            sieve.push []  # adding two entries keeps size = O(sqrt n)
            pos = 0

    undefined  # prevent CoffeeScript from aggregating values

module.exports = {
    primes
}


primes = require('sieve').primes

gen = primes()
console.log "The first 20 primes: #{gen.next().value for _ in [1..20]}"

p100_150 = (while (p = gen.next().value) < 150 then p).filter (n) -> n > 100
console.log "The primes between 100 and 150: #{p100_150}"

while gen.next().value < 7700
    undefined
count = 1
while gen.next().value < 8000
    ++count

console.log "There are #{count} primes between 7,700 and 8,000."

n = 10
c = 0
gen = primes()
loop
    p = gen.next().value
    c += 1
    if c is n
        console.log "The #{n}th prime is #{p}"
        break if n is 10_000_000
        n *= 10


  

You may also check:How to resolve the algorithm Polynomial long division step by step in the REXX programming language
You may also check:How to resolve the algorithm Factorial step by step in the Go programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the Perl programming language