sabato 9 ottobre 2010

Problem 5

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

solution =  head $  filter (isEvenlyDivisibleFor [2..20]) [21..]
     
isEvenlyDivisibleFor range candidate = and [candidate `mod` x == 0 | x <- numbersToTest range]
    where numbersToTest = removeDivisors . reverse
          removeDivisors [] = []
          removeDivisors (x:xs) = x : (removeDivisors $ filter ((/=0).(mod x)) xs)

Nessun commento:

Posta un commento