找回密码
 欢迎注册
楼主: 无心人

[分享] Haskell自我娱乐帖

[复制链接]
 楼主| 发表于 2013-4-7 15:41:13 | 显示全部楼层
牛顿迭代计算整数平方根
  1. let nsqrtN (x, y, a) = (div (x + (div a x))  2, x, a)
  2. let nsqrtLast (x, y, a) = abs(x-y)<=1
  3. let nsqrtResult (x, y, a) =  if (x*x<=a) then x else (x-1)
  4. let nsqrt a = if (a<4) then 1 else nsqrtResult \$ until nsqrtLast nsqrtN (1, a, a)
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2013-4-20 14:09:20 | 显示全部楼层

  1. let divisors n = Data.List.sort \$ (\(x, y)->x ++ y) \$ unzip [(x, div n x)|x<-[1..nsqrt n], mod n x == 0]
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-4-27 10:39 , Processed in 0.043990 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表