Ove*_*ive 3 primes f# lazy-sequences
我正在尝试制作一个素数生成器,它应该能够返回一个素数序列,直到第n个数.现在我认为除了我当前的解决方案之外,还应该有一种更优雅的方式来处理序列,感觉有点冗长,我不得不使用mutables.
0
|> Seq.unfold (fun x -> if isPrime x
then Some(x, x + 1)
else
let mutable y = x
while isPrime y <> true do
y <- y + 1
Some(y, y + 1))
|> Seq.take(n)
Run Code Online (Sandbox Code Playgroud)
使用过滤器的简单解决方案
let t = Seq.initInfinite id |> Seq.filter isPrime |> Seq.take n
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87 次 |
| 最近记录: |