栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带有轮子分解的Eratosthenes筛

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带有轮子分解的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允许循环链接列表的妙招。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485611.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号