2008-11-18から1日間の記事一覧

Problem 94

http://projecteuler.net/index.php?section=problems&id=94 また、pell equation. next (x,y) = (2*x+3*y,2*y+x) tolength x | (x+2) `mod` 3 == 0 = let a = (x+2) `div` 3 in 6*a-2 | (x-2) `mod` 3 == 0 = let a = (x-2) `div` 3 in 6*a+2 p094 = takeWh…

Problem 93

http://projecteuler.net/index.php?section=problems&id=93 とりあえず素直に全生成して、探索。 import Data.List import Data.Ratio perm [] = [[]] perm xs@(_:_) =concat[map (h:)$perm(delete h xs)|h<-xs] calc fs xs = concatMap (calc' fs) . perm …

Problem 204

http://projecteuler.net/index.php?section=problems&id=204 普通の解法。 import Number ham xs = h where h = 1:foldr1 merge [map (x*) h|x<-xs ] main = print.length.takeWhile(<=10^9) . ham .takeWhile(<100) $ primes

Problem 92

http://projecteuler.net/index.php?section=problems&id=92 ナイーブな解法でも解けたが、少し遅かったので、ちょっと改良。 import Data.Char (digitToInt) import Data.List (group) next :: Int -> Int next = sum.map (^2).map digitToInt.show choose …

Problem 95

単純な全探索、遅すぎる。 import Number import Data.List divsSum x = (flip(-)x).product.map f.group.factors$x where f ps@(p:_) = let n = genericLength ps + 1 in (p^n-1)`div` (p-1) chain m = chain' [] . iterate divsSum where chain' xs (y:ys)…