Skip to content

Instantly share code, notes, and snippets.

@kgadek
Created May 27, 2013 23:03
Show Gist options
  • Save kgadek/5659486 to your computer and use it in GitHub Desktop.
Save kgadek/5659486 to your computer and use it in GitHub Desktop.
AVSystems challenge 3
palindromes base = concat $ zipWith (\bcofs -> (map sum . sequence . zipWith (\b -> map (*b)) bcofs)) gen coefs
where
aa x = x : aa (x*base) -- [1, 10, 100, 1000, ..]
a = repeat $ aa 1 -- [ [1, ..], [1, 10, ..], [1, 10, 100, ..], ..]
b = scanl (\(a:acc) x -> base*a:a:acc) [1] [1..] -- [ [1], [10,1], [100, 10, 1], ..]
c = [-1] : [0] : map (\xs -> 0:(map (*base) xs)) c -- [ [-1], [0], [0, -10], [0, 0], [0, 0, -100], ..]
gen = zipWith3 (zipWith3 (\x y z -> x+y+z)) a b c -- [ [1], [11], [101, 10], [1001, 110], [10001,1010,100], ..]
coefs = map (flip take ([1..base-1]:repeat [0..base-1]) . fromIntegral) rptd -- [[[1..9]], [[1..9]], [[1..9],[0..9]], [[1..9],[0..9]], [[1..9],[0..9],[0..9],..], ..]
rptd = 1:1:map (+1) rptd -- [ 1, 1, 2, 2, 3, ..]
-- Wyjaśnienie:
-- * gen to suma elementów a,b,c — ciąg współczynników dla zmiennych generujących: x, xx, xyx, xyyx, xyzyx, xyzzyx, xyzazyx, xyzaazyx, xyzabazyx, ...
-- * coefs tworzy wartości które podstawiamy pod zmienne generujące; rptd określa ile jest zestawów cyfr
main = print (sum $ takeWhile (< 2^50) $ palindromes 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment