• + 0 comments

    So for haskell you have to kind of get weird with it. The list random access operator is too slow to solve this the way I think you normally would. What I do it generate the list of pentagonal numbers and then access them in the order the test cases require. I noticed that they are either ascending or descending but also in order up to the amount provided. That's how I got around using the !! operator. Let me know if there's an actual performant dp solution.

    import Data.List (scanl1)
    import Control.Monad (mapM_)
    
    -- vertices in a pentagon with each line containing n vertices
    pNums = scanl1 (\acc e -> acc + (e * 3 - 2)) [1 ..]
    
    main = do
        n <- getLine >>= return . read
        i <- getLine >>= return . read
        let nums = let ns = take n pNums in 
                    if i == 1 
                    then ns 
                    else reverse ns
        mapM_ (putStrLn . show) nums