🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # leonistor's solution

## to Pascal's Triangle in the Erlang Track

Published at Jun 10 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Compute Pascal's triangle up to a given number of rows.

In Pascal's Triangle each number is computed by adding the numbers to the right and left of the current position in the previous row.

``````    1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
# ... etc
``````

## Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Source

Pascal's Triangle at Wolfram Math World http://mathworld.wolfram.com/PascalsTriangle.html

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### pascals_triangle_tests.erl

``````%% Based on canonical data version 1.5.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/pascals-triangle/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(pascals_triangle_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'1_zero_rows_test'() ->
?assertMatch([], pascals_triangle:rows(0)).

'2_single_row_test'() ->
?assertMatch([], pascals_triangle:rows(1)).

'3_two_rows_test'() ->
?assertMatch([, [1, 1]], pascals_triangle:rows(2)).

'4_three_rows_test'() ->
?assertMatch([, [1, 1], [1, 2, 1]],
pascals_triangle:rows(3)).

'5_four_rows_test'() ->
?assertMatch([, [1, 1], [1, 2, 1], [1, 3, 3, 1]],
pascals_triangle:rows(4)).

'6_five_rows_test'() ->
?assertMatch([, [1, 1], [1, 2, 1], [1, 3, 3, 1],
[1, 4, 6, 4, 1]],
pascals_triangle:rows(5)).

'7_six_rows_test'() ->
?assertMatch([, [1, 1], [1, 2, 1], [1, 3, 3, 1],
[1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]],
pascals_triangle:rows(6)).

'8_ten_rows_test'() ->
?assertMatch([, [1, 1], [1, 2, 1], [1, 3, 3, 1],
[1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1],
[1, 8, 28, 56, 70, 56, 28, 8, 1],
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]],
pascals_triangle:rows(10)).``````
``````-module(pascals_triangle).

-export([rows/1]).

-spec rows(pos_integer()) -> [[pos_integer()]].
rows(Count) ->
[row(N) || N <- lists:seq(0, Count-1)].

row(Row) ->
[binom(Row, K) || K <- lists:seq(0, Row)].

%% https://en.wikipedia.org/wiki/Pascal%27s_triangle#Calculating_a_row_or_diagonal_by_itself
binom(_N, 0) -> 1;
%% un-memoized version
binom(N, K) ->
(N+1-K) * binom(N, K-1) div K.
%% memoized: https://stackoverflow.com/a/3315713
%% x4 faster for N > 50
% binom(N, K) ->
% case erlang:get({binom, N, K}) of
% Val when is_integer(Val) ->
% Val;
% undefined ->
% Val = (N+1-K) * binom(N, K-1) div K,
% erlang:put({binom, N, K}, Val),
% Val
% end.``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?