3.06.2011

Euler 003

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


a=600851475143q=head$reverse$filter p$filter(\x->round a`mod`x==0)[1..round$sqrt a] p n=let c=round$sqrt$(fromIntegral::Integer->Double) n in all(\x->n`mod`x/=0)[2..c] 
171 characters.

Haskell killed me here. That GIGANTIC cast is unfortunately necessary, as far as I can tell.

2 comments:

  1. Ruby version! 67 characters.

    x=600851475143;(2..x).each{|i|((p x;break;)if x==i;x/=i;)if x%i==0}

    ReplyDelete
  2. Nice. This might be why no one uses Haskell.

    ReplyDelete