Euler Solution 29

From ProgSoc Wiki

Jump to: navigation, search

Solutions for Problem 29

For a and b in the range 2 to 100, how many distinct numbers are there of the form ab

Haskell by SanguineV

Runtime: 3.976 seconds

{- A function to ensure a list is unique, very boring. -}
unique :: (Eq a) => [a] -> [a]
unique [] =  []
unique (x:xs) = x : unique (filter (\y -> x /= y) xs)

{- For each number b, map it over the range of a, then fold the results into a single list.
 - Ensure the list is unique and cound how many entries it has. -}
main = print (length (unique ((\n -> foldl (\x y -> x ++ (map (\z -> y ^ z) [2..n])) [] [2..n]) 100)))

Python by Althalus

Runtime: 2.33 seconds

list = []
for a in range(2,101):
	for b in range(2,101):
		if a**b not in list: list.append(a**b)
print len(list)
Personal tools