带有轮子分解的Eratosthenes筛

带有轮子分解的Eratosthenes筛,第1张

带有轮子分解的Eratosthenes筛

您可以走得更远。这是几年前我写的一些OCaml代码:

let eratosthene borne =  let remove_multiples a lst =    let rec remmult multa li accu = function        []         -> rev accu      | head::tail ->          if multa = head          then remmult (a*(hd li)) (tl li)  accu      tail          else remmult   multa        li (head::accu) tail    in    remmult (a * a) lst [] lst  in  let rec first_primes accu ll =    let a = hd ll in     if a * a > borne then (rev accu) @ ll     else first_primes (a::accu) (remove_multiples a (tl ll))  in  let start_list =(* Hard pre of the differences of consecutive numbers that are prime*)(* with 2 3 5 7 starting with 11... *)     let rec lrec = 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 :: lrec     and listPrime2357 a llrec accu =      if a > borne then rev accu      else listPrime2357 (a + (num (hd llrec))) (tl llrec) (a::accu)    in    listPrime2357 (num 11) lrec []  in  first_primes [(num 7);(num 5);(num 3);(num 2)] start_list;;

请注意,OCaml允许循环链接列表的妙招



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5477750.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存