LoginSignup
3
0

More than 5 years have passed since last update.

Project EulerをHaskellで解いていく(Problem3: Largest prime factor)

Posted at

TL;DR

Haskellの勉強を兼ねてProject Eulerを解いていきます。
始めたばかりでわからないことが多いのでコメント頂けると嬉しいです。

問題文

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

13195 の素因数は 5, 7, 13, 29 である.
600851475143 の素因数のうち最大のものを求めよ.

コード

primes::Int -> [Int]
primes 1 = []
primes n = x : primes (n `div` x)
  where
    factors n = [x | x <- [1..n], n `mod` x == 0]
    x = (factors n) !! 1

gcdPrime::Int -> Int
gcdPrime n = maximum $ primes n

main::IO()
main = do
  print $ gcdPrime 600851475143

うーん…なんかもうちょっと良い実装がありそう…

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0