Skip to main content
added 90 characters in body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372

MATL, 15 14 bytes

:"@:t~]vG:s:)z

Input is 1-based.

Try it online! Or verify the first values.

How it works

       % Implicit input: n
:      % Range: [1 2 ... n].
"      % For each
  @    %   Push iteration index, k (goes from 1 to n)
  :    %   Range: gives [1 2 ... k]
  t    %   Duplicate
  ~    %   Negate, element-wise: gives [0 0 ... 0] (k zeros)
]      % End
v      % Concatenate everything into a column vector
G      % Push n again
:      % Range: [1 2 ... n]
s      % Sum: gives n-th triangular number, T(n)
:      % Range
)      % Index: keeps the first T(n) values
z      % Number of nonzeros
       % Implicit output

MATL, 15 14 bytes

:"@:t~]vG:s:)z

Input is 1-based.

Try it online!

How it works

       % Implicit input: n
:      % Range: [1 2 ... n].
"      % For each
  @    %   Push iteration index, k (goes from 1 to n)
  :    %   Range: gives [1 2 ... k]
  t    %   Duplicate
  ~    %   Negate, element-wise: gives [0 0 ... 0] (k zeros)
]      % End
v      % Concatenate everything into a column vector
G      % Push n again
:      % Range: [1 2 ... n]
s      % Sum: gives n-th triangular number, T(n)
:      % Range
)      % Index: keeps the first T(n) values
z      % Number of nonzeros
       % Implicit output

MATL, 15 14 bytes

:"@:t~]vG:s:)z

Input is 1-based.

Try it online! Or verify the first values.

How it works

       % Implicit input: n
:      % Range: [1 2 ... n].
"      % For each
  @    %   Push iteration index, k (goes from 1 to n)
  :    %   Range: gives [1 2 ... k]
  t    %   Duplicate
  ~    %   Negate, element-wise: gives [0 0 ... 0] (k zeros)
]      % End
v      % Concatenate everything into a column vector
G      % Push n again
:      % Range: [1 2 ... n]
s      % Sum: gives n-th triangular number, T(n)
:      % Range
)      % Index: keeps the first T(n) values
z      % Number of nonzeros
       % Implicit output
added 1 character in body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372

MATL, 15 14 bytes

:"@:t~]vG:s:)z

Input is 1-based.

Try it online!

How it works

       % Implicit input: n
:      % Range: [1 2 ... n].
"      % For each
  @    %   Push iteration index, k (goes from 1 to n)
  :    %   Range: gives [1 2 ... k]
  t    %   Duplicate
  ~    %   Negate, element-wise: gives [0 0 ... 0] (k zeros)
]      % End
v      % Concatenate everything into a column vector
G      % Push n again
:      % Range: [1 2 ... n]
s      % Sum: gives n-th triangular number, T(n)
:      % Range
)      % Index: keeps the first T(n) values
z      % Number of nonzeros
       % Implicit output

MATL, 15 14 bytes

:"@:t~]vG:s:)z

Input is 1-based.

Try it online!

How it works

       % Implicit input: n
:      % Range: [1 2 ... n].
"      % For each
  @    %   Push iteration index k (goes from 1 to n)
  :    %   Range: gives [1 2 ... k]
  t    %   Duplicate
  ~    %   Negate, element-wise: gives [0 0 ... 0] (k zeros)
]      % End
v      % Concatenate everything into a column vector
G      % Push n again
:      % Range: [1 2 ... n]
s      % Sum: gives n-th triangular number, T(n)
:      % Range
)      % Index: keeps the first T(n) values
z      % Number of nonzeros
       % Implicit output

MATL, 15 14 bytes

:"@:t~]vG:s:)z

Input is 1-based.

Try it online!

How it works

       % Implicit input: n
:      % Range: [1 2 ... n].
"      % For each
  @    %   Push iteration index, k (goes from 1 to n)
  :    %   Range: gives [1 2 ... k]
  t    %   Duplicate
  ~    %   Negate, element-wise: gives [0 0 ... 0] (k zeros)
]      % End
v      % Concatenate everything into a column vector
G      % Push n again
:      % Range: [1 2 ... n]
s      % Sum: gives n-th triangular number, T(n)
:      % Range
)      % Index: keeps the first T(n) values
z      % Number of nonzeros
       % Implicit output
added 597 characters in body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372

MATL, 15 14 bytes

:"@:t~]vG:s:)z

Input is 1-based.

Try it online!

How it works

       % Implicit input: n
:      % Range: [1 2 ... n].
"      % For each
  @    %   Push iteration index k (goes from 1 to n)
  :    %   Range: gives [1 2 ... k]
  t    %   Duplicate
  ~    %   Negate, element-wise: gives [0 0 ... 0] (k zeros)
]      % End
v      % Concatenate everything into a column vector
G      % Push n again
:      % Range: [1 2 ... n]
s      % Sum: gives n-th triangular number, T(n)
:      % Range
)      % Index: keeps the first T(n) values
z      % Number of nonzeros
       % Implicit output

MATL, 15 14 bytes

:"@:t~]vG:s:)z

Input is 1-based.

Try it online!

MATL, 15 14 bytes

:"@:t~]vG:s:)z

Input is 1-based.

Try it online!

How it works

       % Implicit input: n
:      % Range: [1 2 ... n].
"      % For each
  @    %   Push iteration index k (goes from 1 to n)
  :    %   Range: gives [1 2 ... k]
  t    %   Duplicate
  ~    %   Negate, element-wise: gives [0 0 ... 0] (k zeros)
]      % End
v      % Concatenate everything into a column vector
G      % Push n again
:      % Range: [1 2 ... n]
s      % Sum: gives n-th triangular number, T(n)
:      % Range
)      % Index: keeps the first T(n) values
z      % Number of nonzeros
       % Implicit output
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372
Loading