Skip to content

Instantly share code, notes, and snippets.

@Koitaro
Created February 7, 2011 06:47
Show Gist options
  • Select an option

  • Save Koitaro/814077 to your computer and use it in GitHub Desktop.

Select an option

Save Koitaro/814077 to your computer and use it in GitHub Desktop.
Clean : Project Euler 120 - 129
module main
import StdEnv, StdLib, BigInt
fact :: (Int -> [Int])
fact = f [2:[3,5..]] where
f [x:xs] n
| n == 1 = []
| x*x > n = [n]
| n rem x == 0 = [x:f [x:xs] (n/x)]
| otherwise = f xs n
rad :: (Int -> Int)
rad = prod o map hd o group o fact
Max :== 119999
radArr :: {#Int}
radArr =: {createArray (Max+1) 0 & [x] = rad x \\ x <- [1..Max]}
rs :: [Int]
rs =: sortBy (\x y = radArr.[x] < radArr.[y]) [1..Max]
as :: Int -> [Int]
as c = (filter (\a = c - a > a) o takeWhile (\a = c / radArr.[c] > radArr.[a] * 2)) rs
cs :: [Int]
cs =: [x \\ x <- [1..Max] | x > radArr.[x]]
Start = (toString o sum) [sum (f c (as c)) \\ c <- cs] where
f c as = [toBigInt c \\ a <- as & c <- repeat c | hit a (c-a) c]
hit a b c = ra * rb * rc <% c && gcd a b == 1 && gcd b c == 1 && gcd c a == 1 where
ra = toBigInt radArr.[a]
rb = toBigInt radArr.[b]
rc = toBigInt radArr.[c]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment