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

Published on 12 May 2024 09:40 PM

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

Source code in the fsharp programming language

let primeZ fN  =primes()|>Seq.unfold(fun g-> Some(fN(g()), g))
let primesI()  =primeZ bigint
let primes64() =primeZ int64
let primes32() =primeZ int32
let pCache     =Seq.cache(primes32())
let isPrime   g=if g<2  then false else let mx=int(sqrt(float g)) in pCache|>Seq.takeWhile(fun n->n<=mx)|>Seq.forall(fun n->g%n>0)
let isPrime64 g=if g<2L then false else let mx=int(sqrt(float g)) in pCache|>Seq.takeWhile(fun n->n<=mx)|>Seq.forall(fun n->g%(int64 n)>0L)


Seq.take 20 primes32()|> Seq.iter (fun n-> printf "%d " n)


primes32() |> Seq.skipWhile (fun n->n<100) |> Seq.takeWhile (fun n->n<=150) |> Seq.iter (fun n -> printf "%d " n)


printfn "%d" (primes32() |> Seq.skipWhile (fun n->n<7700) |> Seq.takeWhile (fun n->n<=8000) |> Seq.length)


Seq.item 9999 pCache


Seq.item 10000 pCache


let strt = System.DateTime.Now.Ticks
for i = 1 to 8 do
  let n = pown 10 i // the item index below is zero based!
  printfn "The %dth prime is:  %A" n (primeZ int |> Seq.item (n - 1))
let timed = (System.DateTime.Now.Ticks - strt) / 10000L
printfn "All of the last took %d milliseconds." timed


printfn "The first 20 primes are:  %s"
  ( primesSeq() |> Seq.take 20
      |> Seq.fold (fun s p -> s + string p + " ") "" )
printfn "The primes from 100 to 150 are:  %s"
  ( primesSeq() |> Seq.skipWhile ((>) (prime 100))
      |> Seq.takeWhile ((>=) (prime 150))
      |> Seq.fold (fun s p -> s + string p + " ") "" )
printfn "The number of primes from 7700 to 8000 are:  %d"
  ( primesSeq() |> Seq.skipWhile ((>) (prime 7700))
      |> Seq.takeWhile ((>=) (prime 8000)) |> Seq.length )
let strt = System.DateTime.Now.Ticks
for i = 1 to 8 do
  let n = pown 10 i // the item index below is zero based!
  printfn "The %dth prime is:  %A" n (primesSeq() |> Seq.item (n - 1))
let timed = (System.DateTime.Now.Ticks - strt) / 10000L
printfn "All of the last took %d milliseconds." timed


  

You may also check:How to resolve the algorithm Empty string step by step in the BASIC programming language
You may also check:How to resolve the algorithm Write language name in 3D ASCII step by step in the Perl programming language
You may also check:How to resolve the algorithm Solve a Holy Knight's tour step by step in the Tcl programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm Map range step by step in the Frink programming language