How to resolve the algorithm Extensible prime generator step by step in the Icon and Unicon programming language
How to resolve the algorithm Extensible prime generator step by step in the Icon and Unicon 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 Icon and Unicon programming language
Source code in the icon programming language
![2,3,5,7] | (nc := 11) | (nc +:= |wheel2345)
import Collections # to get the Heap class for use as a Priority Queue
record filter(composite, prime) # next composite involving this prime
procedure main()
every writes((primes()\20)||" " | "\n")
every p := primes() do if 100 < p < 150 then writes(p," ") else if p >= 150 then break write()
every (n := 0, p := primes()) do if 7700 < p < 8000 then n +:= 1 else if p >= 8000 then break write(n)
every (i := 1, p := primes()) do if (i+:=1) >= 10000 then break write(p)
end
procedure primes()
local wheel2357, nc
wheel2357 := [2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2,
6, 4, 6, 8, 4, 2, 4, 2, 4, 8, 6, 4, 6, 2, 4, 6,
2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2, 10, 2, 10]
suspend sieve(Heap(,getCompositeField), ![2,3,5.7] | (nc := 11) | (nc +:= |!wheel2357))
end
procedure sieve(pQueue, candidate)
local nc
if 0 = pQueue.size() then { # 2 is prime
pQueue.add(filter(candidate*candidate, candidate))
return candidate
}
while candidate > (nc := pQueue.get()).composite do {
nc.composite +:= nc.prime
pQueue.add(nc)
}
pQueue.add(filter(nc.composite+nc.prime, nc.prime))
if candidate < nc.composite then { # new prime found!
pQueue.add(filter(candidate*candidate, candidate))
return candidate
}
end
# Provide a function for comparing filters in the priority queue...
procedure getCompositeField(x); return x.composite; end
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the Arturo programming language
You may also check:How to resolve the algorithm Assertions step by step in the Nutt programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Function definition step by step in the jq programming language
You may also check:How to resolve the algorithm Element-wise operations step by step in the AutoHotkey programming language