gpt4 book ai didi

f# - 生成无限的数字集

转载 作者:行者123 更新时间:2023-12-01 11:36:04 25 4
gpt4 key购买 nike

我正在尝试制作一个素数生成器,它应该能够返回最多 nth 个数的素数序列。现在我认为除了我目前的解决方案感觉有点冗长而且我不得不使用可变变量之外,应该有一种更优雅的方法来处理序列。

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)

最佳答案

使用过滤器的简单解决方案

let t = Seq.initInfinite id |> Seq.filter isPrime |> Seq.take n

关于f# - 生成无限的数字集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26956101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com