これはまぁ、1,2,...,20 の最小公倍数を求めれば良いだけの話。
object P005 {
def gcd(a:Long, b:Long):Long = {
if (a>b) gcd(b,a)
else if (a==0) b
else gcd(b%a, a)
}
def lcd(a:Long, b:Long):Long = a/gcd(a,b)*b
def p005(n:Int):Long = List.range(1, n+1).foldLeft(1L)(lcd(_,_))
def main(args:Array[String]) {
println(new java.util.Date)
println(p005(10))
println(p005(20))
println(new java.util.Date)
}
}
No comments:
Post a Comment