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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Extensible prime generator step by step in the PureBasic 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 PureBasic programming language

Source code in the purebasic programming language

EnableExplicit
DisableDebugger
Define StartTime.i=ElapsedMilliseconds()

Procedure.b IsPrime(n.i)
  Define i.i=5
  If n<2 : ProcedureReturn #False : EndIf
  If n%2=0 : ProcedureReturn Bool(n=2) : EndIf
  If n%3=0 : ProcedureReturn Bool(n=3) : EndIf
  While i*i<=n
    If n%i=0 : ProcedureReturn #False : EndIf
    i+2
    If n%i=0 : ProcedureReturn #False : EndIf
    i+4
  Wend  
  ProcedureReturn #True
EndProcedure

If OpenConsole("Extensible prime generator")
  Define c.i=0, n.i=2
  Print("First twenty: ")
  While c<20
    If IsPrime(n)
      Print(Str(n)+" ")
      c+1
    EndIf
    n+1
  Wend
  
  Print(~"\nBetween 100 and 150: ")
  For n=100 To 150
    If IsPrime(n)
      Print(Str(n)+" ")
    EndIf
  Next
  
  Print(~"\nNumber beween 7'700 and 8'000: ")
  c=0
  For n=7700 To 8000
    c+IsPrime(n)
  Next
  Print(Str(c))
  
  Print(~"\n10'000th prime: ")
  c=0 : n=1
  While c<10000
    n+1
    c+IsPrime(n)    
  Wend
  Print(Str(n))  
EndIf
Print(~"\nRuntime milliseconds: "+
      Str(ElapsedMilliseconds()-StartTime))
Input()

  

You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Draco programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Quadratic step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Search a list step by step in the C programming language
You may also check:How to resolve the algorithm Eban numbers step by step in the Julia programming language