Ferramentas de Site


algoritmo:desafios:google_treasure_hunt:problema4

Problema 4

Questão

Find the smallest number that can be expressed as the sum of 3 consecutive prime numbers, the sum of 77 consecutive prime numbers, the sum of 375 consecutive prime numbers, the sum of 729 consecutive prime numbers, and is itself a prime number.

Exemplo

For example, 41 is the smallest prime number that can be expressed as
the sum of 3 consecutive primes (11 + 13 + 17 = 41) and
the sum of 6 consecutive primes (2 + 3 + 5 + 7 + 11 + 13 = 41).

Soluções

Haskell

import Data.List(group)
 
primos = 2 : filter (isPrime primos) [3,5..]
isPrime (x:xs) y = x*x > y || (y `mod` x) /= 0 && isPrime xs y
 
primos3 = somaPrimos 3 primos
primos77 = somaPrimos 77 primos
primos375 = somaPrimos 375 primos
primos729 = somaPrimos 729 primos
 
somaPrimos x ps = sum h : somaPrimos x t
  where h = take x ps
        t = tail ps
 
a@(x:xs) <*> b@(y:ys) | x < y  = x : (xs <*> b)
                      | x == y = x : y : (xs <*> ys)
                      | otherwise = y : (a <*> ys)
 
 
juntaSomas = primos3 <*> primos77 <*> primos375 <*> primos729
 
menor = head . filter iguais . group
  where iguais = (4==) . length
 
main = print $ menor juntaSomas

Por Betovsky.

algoritmo/desafios/google_treasure_hunt/problema4.txt · Última modificação em: 2021/12/12 11:49 por staff