Ecco la soluzione al problema 5 in Haskell:

divisibilefinoan (x, n)
|n==1 = if(x `mod` n)==0 then True else False
|otherwise = if(x `mod` n)==0 then divisibilefinoan (x, (n-1)) else False

loop n = if(divisibilefinoan (n, 20)) then n else loop (n+2)

– Ottimizzazione per far partire il loop che verifica se è divisibile fino a quel numero
– da un punto più avanzato: il numero che cerchiamo sarà sicuramente >2520 (che è multiplo
– di tutti i numeri fino a 10), comprenderà i fattori 11, 13, 17 e 19 (che sono numeri primi
– compresi tra 10 e 20) e un fattore 2 aggiuntivo (dovuto a 16, che è due alla quarta,
– mentre il due compariva al massimo con esponente 3 in otto). Praticamente equivale a
– risolverlo a mano.
result = loop (2520*11*13*17*19*2)

Il testo del problema:

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 number that is evenly divisible by all of the numbers from 1 to 20?

Leave a Reply