好像不能让娃跟AI学写程序啊

来源: 古代的事物 2022-12-19 13:10:52 [] [博客] [旧帖] [给我悄悄话] 本文已被阅读: 次 (13103 bytes)

Here is an OCaml program that computes the 5th biggest prime number less than 1000:


 
(* Helper function to check if a number is prime *)
let is_prime n =
    let rec is_prime_rec i =
        i * i > n || (n mod i <> 0 && is_prime_rec (i+1))
    in n < 2 || is_prime_rec 2

(* Find the 5th biggest prime number less than 1000 *)
let rec find_fifth_biggest_prime count n =
    if count = 5 then n
    else if is_prime n then find_fifth_biggest_prime (count+1) (n-1)
    else find_fifth_biggest_prime count (n-1)

let fifth_biggest_prime = find_fifth_biggest_prime 0 999

(* Print the result *)
print_int fifth_biggest_prime;
print_newline ()

所有跟帖: 

现在就把目标定为JS啦 -xiaociwei- 给 xiaociwei 发送悄悄话 xiaociwei 的博客首页 (0 bytes) () 12/19/2022 postreply 13:11:00

倒也没有。。。 -古代的事物- 给 古代的事物 发送悄悄话 古代的事物 的博客首页 (50 bytes) () 12/19/2022 postreply 13:14:11

请您先登陆,再发跟帖!

发现Adblock插件

如要继续浏览
请支持本站 请务必在本站关闭Adblock

关闭Adblock后 请点击

请参考如何关闭Adblock

安装Adblock plus用户请点击浏览器图标
选择“Disable on www.wenxuecity.com”

安装Adblock用户请点击图标
选择“don't run on pages on this domain”