Sort by

recency

|

84 Discussions

|

  • + 0 comments

    My Elixir solution:

    defmodule Solution do
        def get_next(l) do
            [0] ++ (Enum.zip(l, tl l) |> Enum.map(fn {a, b} -> a+b end)) ++ [0]
        end
        
        def pasc(_, 0), do: []
        def pasc(prev, iter) do
            next = get_next(prev)
            [next] ++ pasc(next, iter-1)
        end
        
        def get_pasc(n), do: [[0, 1, 0]] ++ pasc([0, 1, 0], n)
    end
    
    {n, _} = Integer.parse(IO.gets "")
    Solution.get_pasc(n-1)
        |> Enum.each(fn row -> 
            Enum.reject(row, &(&1 == 0))
                |> Enum.map(&Integer.to_string(&1) <> " ")
                |> List.to_string() # |> String.trim()
                |> IO.puts()
        end)
    
  • + 1 comment

    Scala recursive version:

        def main(args: Array[String]) {
            val n = StdIn.readInt()
            println("1")
            printPascalTriangle(n, List(1))
        }
        
        @tailrec
        def printPascalTriangle(n: Int, prevRow: List[Int]): Unit = {
            if (prevRow.size < n) {
                val currentRow = 
                    1 :: (for (i <- 1 to prevRow.size) yield 
                            prevRow(i - 1) + 
                                (if (i < prevRow.size) prevRow(i) else 0)
                    ).toList
                println(currentRow.mkString(" "))
                printPascalTriangle(n, currentRow)
            }
        }
    
  • + 0 comments

    Haskell | Easy to Understand | Brute-force

    facto n = product [1..n]
    
    printLine :: Int -> Int -> IO ()
    printLine vari n
        | vari == 0 = putStr "1 "
        | otherwise = do
            printLine (vari-1) n
            putStr $ (show (facto n `div` ((facto vari) * facto (n-vari)))) ++ " " 
    
    solve_ :: Int -> Int -> IO ()
    solve_ k n
        | n == k = return ()
        | otherwise = do
            printLine n n
            putStr "\n"
            solve_ k (n+1)
    
    solve :: Int -> IO ()
    solve k = solve_ k 0
    
    main :: IO ()
    main = do
        k_ <- getLine
        let k = read k_ :: Int
        solve k 
    
  • + 0 comments

    Pascal's Triangle is a fascinating mathematical concept! It's amazing how patterns emerge from simple rules. By the way, if you're looking for a beautiful smile to complement your love for math, dentalblush offers top-notch miami dental clinic in town. Keep exploring the wonders of both math and oral health!

  • + 0 comments

    Haskell

    pascal n = take n $ iterate nextPascal [1] where 
        nextPascal l = [head l] ++ [a + b | (a, b) <- zip l (tail l)] ++ [last l]
        
    pascalStr n = map (unwords . map show) $ pascal n
    
    main = do
        n <- read <$> getLine
        putStrLn . unlines $ pascalStr n