Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

euler

Name: Anonymous 2012-01-17 13:33


{-
 - Project euler 51, be happy /prog/
 - Released under the GPLv2 /because i am a hipster
 - time ./eu51 throw 0.037 s
 - this is you to see that even a bad algorithm (if you check this out
 - it's crap) it is not that verbose
 -}
{-# LANGUAGE BangPatterns #-}
module Main where

import Data.Numbers.Primes
import qualified Data.Sequence as S
import Data.Int (Int8)
import Data.Tuple (swap)
import Data.List (unfoldr, find)
import Data.Maybe (isJust, fromJust)
import Data.Foldable (foldl')

type Primo = S.Seq Int8

main :: IO ()
main = print $ find eightCombinations rangoPrimosProp

toPrimo :: Int -> Primo
toPrimo = let listDigit 0 = Nothing 
              listDigit n = Just . swap $ divMod n 10
          in S.fromList . map fromIntegral . reverse . unfoldr listDigit

solucion :: Primo
solucion = toPrimo 121313

mySol :: Primo
mySol = toPrimo 111857

shead :: S.Seq a -> a
shead n | (a S.:< _) <- S.viewl n = a

primoToInt :: Primo -> Int
primoToInt = foldl' (\acc a -> acc*10 + fromIntegral a) 0

eightCombinations :: Primo -> Bool
eightCombinations !n =
  let digitos = S.elemIndicesL (fromJust $ threeDigitsRepeat n) n
      singleCombination d = foldr (flip S.update d) n digitos
      criterioFiltro = isPrime . primoToInt
      isMinorGenerator a = (length a) == 8 && head a == n
  in  isMinorGenerator . filter criterioFiltro $ map singleCombination [0..9]

rangoPrimos :: [Int]
rangoPrimos = takeWhile (<10^6) . dropWhile (<10^5) $ primes

rangoPrimosProp :: [Primo]
rangoPrimosProp = filter criterio $ map toPrimo rangoPrimos
  where
    criterio a = isJust (threeDigitsRepeat a >>= lastRepeat a >>= goodValues)
 
threeDigitsRepeat :: Primo -> Maybe Int8
threeDigitsRepeat = go . S.unstableSort
  where   
    go :: Primo -> Maybe Int8
    go s | S.null s = Nothing
         | takeEquals s == 3 = Just head'
         | otherwise = go (S.dropWhileL (head' ==) s)
      where
        head' = shead s
        takeEquals = length . S.elemIndicesL head'
       
lastRepeat :: Primo -> Int8 -> Maybe Int8
lastRepeat s n = let (_ S.:> final) = S.viewr s
                 in if n == final then Nothing else Just n

goodValues :: Int8 -> Maybe Int8
goodValues n = if elem n [0..2] then Just n else Nothing

Name: Anonymous 2012-01-17 13:34

No exceptions

Name: Anonymous 2012-01-17 13:35

actual code on my prog? GTFO

Name: Anonymous 2012-01-17 14:19

Op is italian

Name: Anonymous 2012-01-17 14:33

>BangPattern

haskell confirmed for sexiest language!

Name: Anonymous 2012-01-17 14:48

>>4
solucion
That's Spanish, Italian's soluzione.

Name: Anonymous 2012-01-17 14:57

>>6
Soluzione il mio culo

Name: Anonymous 2012-01-17 14:59

import
lol, python

Name: Anonymous 2012-01-17 15:08

>>8
no read SICP

Name: Anonymous 2012-01-17 15:09

>>7
mi piace la figa!

Name: Anonymous 2012-01-17 15:27

verifica i miei doppi

Name: Anonymous 2012-01-17 15:59

>>11
doppi bennissimi! bravo!

Name: Anonymous 2012-01-17 16:00

>>10-12
Tornate su /g/, ``froci''

Name: Anonymous 2012-01-17 16:02

>>13
porco dio! non sono mai stato la /g/, terrone rottinculo di merda

Name: Anonymous 2012-01-17 16:46

>>14
ma vaffanculo va...

Name: Anonymous 2012-01-17 16:56

Cristo, dove siamo finiti, italiani su /prog/.

Name: Anonymous 2012-01-17 17:06

fuck off pizzafuckers

Name: Anonymous 2012-01-17 18:03

Cosa cazzo e' successo?Non posso assentarmi un giorno

Name: Anonymous 2012-01-17 18:20

>>18
e'
I VERI ITALIANI usano lettere accentate

Name: Anonymous 2012-01-17 18:23

>>19
tastiera americana

Name: Anonymous 2012-01-17 18:24

>>20
Lèarn to usè it.

Name: Anonymous 2012-01-17 18:57

Fuck off, [spoiler]``Marios''[/spoiler

Name: Anonymous 2012-01-17 19:27

>>22
Non chiamarmi Mario, ``John''.

Name: Anonymous 2012-01-17 19:30

>>22
BBcode Mastah!

Name: Anonymous 2012-01-17 20:51

>>22
Ecco , Il maestro di bbcode

Name: Anonymous 2012-01-17 20:56

>>22
Ecco, the dolphin

Name: Anonymous 2012-01-17 21:00

>>26
Ho riso.

Name: Anonymous 2012-01-17 21:11

>>27
Per la cena?

Name: Anonymous 2012-01-17 22:09

>>1
rango primo
JAQUEAME EL ANO, ``MARICON"

Name: Anonymous 2012-01-17 22:37

>>29
y los tamales por el culo no te arden?

Name: Anonymous 2012-01-18 7:34

>>28
Ho pasta. Dohohoho, sono così intelligente.

Name: Anonymous 2012-01-18 9:27

I came here to get dubz..

Name: Anonymous 2012-01-18 9:29

and check em.

Name: Anonymous 2012-01-18 10:39

El tamal de Troya.

Name: Anonymous 2012-01-18 10:47

Qu'est un fil polyglotte sans toto?
Déjà il me semble que >>1-san devrait utiliser un vrai descendant de ML, comme OCaml!

Name: Anonymous 2012-01-18 11:20

>>30
Si. Pero me gusta cuando mi culo arde.

Name: Anonymous 2012-01-18 13:56

>>35
lol ML

Name: Anonymous 2012-01-18 17:43

182
rsa :: Integral a => a -> a -> a
rsa p q = sum $ filter (\e -> gcd e (ps*qs) == 1 && gcd (e-1) ps == 2 && gcd (e-1) qs == 2) e
    where
        ps = p-1
        qs = q-1
        e = [2..ps*qs-1]

solution :: Integer
solution = rsa 1009 3643

Name: Anonymous 2012-01-18 17:49

>>37
ITT: Mario Language

Name: Anonymous 2012-01-18 17:57

>>39
Pizza, mandolino, /prog/

Name: Anonymous 2012-01-18 20:24

>>38
hontoni?

Don't change these.
Name: Email:
Entire Thread Thread List