# Triangle-style sequences

Consider the triangular numbers and their forward differences:

$$T = 1, 3, 6, 10, 15, 21, ... \\ \Delta T = 2,3,4,5,6, ...$$

If we alter $$\\Delta T\$$ so that it begins with a different integer, we get a different, yet similar sequence (assuming that it begins with $$\T'_1 = 1\$$):

$$\Delta T' = 3,4,5,6,7,8,... \\ T' = 1, 4, 8, 13, 19, 26, 34,...$$

This can be extended to begin with negative numbers:

$$\Delta T' = -2,-1,0,1,2,3,... \\ T' = 1,-1,-2,-2,-1,1,4,...$$

More generally, for a given integer $$\n\$$, we can define a "triangle-style" sequence $$\T'\$$ as a sequence whose forward differences form the sequence $$\n, n+1, n+2, n+3, ...\$$, and that has $$\1\$$ as its first term

You should take an integer $$\n\$$ and do one of:

• Take a positive integer $$\m\$$ and output the first $$\m\$$ integers of the "triangle-style" sequence for $$\n\$$
• Take an integer $$\m\$$ and output the $$\m\$$th integer in the "triangle-style" sequence for $$\n\$$. You may use either 0 or 1 indexing
• Output all integers in the "triangle-style" sequence for $$\n\$$

This is , so the shortest code in bytes wins

## Test cases

These are the first 10 outputs for each provided $$\n\$$:

 n -> out
-4 -> 1, -3, -6, -8, -9, -9, -8, -6, -3, 1
-3 -> 1, -2, -4, -5, -5, -4, -2, 1, 5, 10
-2 -> 1, -1, -2, -2, -1, 1, 4, 8, 13, 19
-1 -> 1, 0, 0, 1, 3, 6, 10, 15, 21, 28
0 -> 1, 1, 2, 4, 7, 11, 16, 22, 29, 37
1 -> 1, 2, 4, 7, 11, 16, 22, 29, 37, 46
2 -> 1, 3, 6, 10, 15, 21, 28, 36, 45, 55
3 -> 1, 4, 8, 13, 19, 26, 34, 43, 53, 64
4 -> 1, 5, 10, 16, 23, 31, 40, 50, 61, 73
39 -> 1, 40, 80, 121, 163, 206, 250, 295, 341, 388
68 -> 1, 69, 138, 208, 279, 351, 424, 498, 573, 649
48 -> 1, 49, 98, 148, 199, 251, 304, 358, 413, 469

• Strictly speaking, the deltas of the triangular numbers start at 2, not 1. May 31, 2021 at 2:11
• Can I output m+1 integers instead of m? May 31, 2021 at 2:33
• $T^{'}(n, m) = T(n + m - 2) - T(n - 1) + 1$. Now if only you had used true 0-indexing starting from $T(0) = 0$ ...
– Neil
May 31, 2021 at 9:43
• @Jonah No, you may not May 31, 2021 at 14:05
• @cairdcoinheringaahing Isn't it the equivalent of using 0-indexing, which is allowed for the 2nd option? May 31, 2021 at 15:49

f n=scanl(+)1[n..]


Try it online!

Outputs an infinite list.

f n = scanl (+) 1 [n..]
[n..]  -- Make an infinite list n, n+1, ...
scanl (+) 1        -- Cumulative sum, starting at 1


# Jelly, 4 bytes

Ḷ+S‘


Try it online!

Takes 0-based $$\m\$$ and $$\n\$$, and returns the $$\m\$$-th term.

### How it works

Ḷ+S‘  Left arg: m, Right arg: n
Ḷ     [0..m-1]
+    [n..n+m-1]
S   Sum
‘  Increment

• Looks like you ninja'd me :p May 31, 2021 at 1:27
• Wait, what? Did I somehow miss this? I could've sworn I tried this when going through the like 50 combinations of built-ins I imagined might work... May 31, 2021 at 1:28

# Vyxal, 4 bytes

ʁ+∑›


Try it Online!

An exact port of Bubbler's jelly answer. Takes m as the 0-based index to retrieve and n as the variant

## Explained

ʁ+∑›
ʁ    # [0 ... m-1]
+   # ↑ + n (vectorises)
∑  # sum(↑)
› # ↑ + 1 (implicitly output)

• no flag abuse \o/ (yes I will keep bullying you about it, no I am not serious) May 31, 2021 at 1:37
• jokes on you because I myself try to keep flagless unless neccesary May 31, 2021 at 1:38

# Python 2, 24 bytes

Takes an integer $$\n\$$ and outputs the $$\m\$$th integer in the "triangle-style" sequence for $$\n\$$. Uses 0-indexing.

lambda n,m:~-m*m/2+n*m+1


Try it online!

### Python 2, 26 bytes

lambda n,m:m*(m+n+n-1)/2+1


Try it online!

# convey, 14 bytes

1"}
+<
",{
>+1


Try it online!

The lower loop is the increment, that starts with the input { and gets increased by one +1 each iteration. It's get copied " into +, where the accumulator loops. Every iteration it gets copied into the output }.

# Jelly, 5 bytes

r+ṖS‘


Try it online!

Given left argument $$\n\$$ and right argument $$\m\$$, output the $$\m\$$th element of the $$\n\$$-variant triangular sequence (0-indexed).

# PowerShell Core, 40 38 bytes

param($a,$b)($i=1) 2..$b|%{($i+=$a++)}


Try it online!

Saved two bytes by removing superfluous parentheses

# 05AB1E, 5 bytes

L<+O>


Try it online!

05AB1E looks like brainfuck :P

• It fails for $n=0$ because the L creates the list [1,0]. If you change L< to Ý¨ it works as intended, although unfortunately doesn't look like brainfuck anymore. ;) Nov 5, 2021 at 9:18

# K (ngn/k), 9 bytes

{1+/x+!y}


Try it online!

Takes n as x, and m as y; outputs the m-th integer in the "triangle-style" sequence for n (0-indexed). Feels like there is a more clever way, but...

• x+!y generate n..n+m-1
• 1+/ take the sum (seeded with 1)

# kalker, 26 bytes

Nothing fancy, just bringing attention to kalker, a lovely command-line calculator that I recently stumbled upon.

f(a,b)=Σ(1,b,n)+(b-1)(a-2)


An online interpreter can be found here.

# Python 3.8 (pre-release), 44 bytes

f=lambda n,m,a=1:m and[a]+f(n+1,m-1,n+a)or[]


Try it online!

f=lambda n,m,a=1:m and[a]+f(n+1,m-1,n+a)or[]
a=1                             #Set accumulator to 1 initially
m and                       #If m>0
[a]+f(n+1,m-1,n+a)     #Prepend accumulator to the rest of
#Every time, the accumulator increases by
#the current n
#the sequence
or[] #Otherwise, return an empty list


# Husk, 5 bytes

∫:1¡→


Try it online!

an infinite list.

# PowerShell, 30 bytes

param($m,$n)$m*($m+2*$n-1)/2+1  Try it online! -1 byte thanks to @Julian • you can save one char by replacing $n+$n with 2*$n May 31, 2021 at 2:15

# R, 28 bytes

function(n,m)m*(m-1)/2+m*n+1


Try it online!

Outputs mth (0-indexed) term.

Using straightforward formula.

# JavaScript (Node.js), 19 bytes

n=>m=>m--*(m/2+n)+1


Try it online!

-5 bytes thanks to Bubbler, tsh and ophact

• You can omit f= from byte count since it's not recursive, and n=>m=> is one byte shorter than (n,m)=>. May 31, 2021 at 1:30
• (m+n+n-1)/2 -> ((m-1)/2+n) -> (--m/2+n)
– tsh
May 31, 2021 at 3:41
• 19
– user100690
May 31, 2021 at 5:41

# MATLAB/Octave, 22 bytes

@(n,m)sum([1,n:m+n-2])


Try it online!
Anonymous function. Outputs mth integer of sequence for n.

Alternatively, if we want to output all elements from 1st to mth we can achieve so with 25 bytes:

@(n,m)cumsum([1,n:m+n-2])


Try it online!

The part m:m+n-2 of the functions is actually sequence ΔT - vector starting from n to m+n-2.
We subtract 2 to get the correct number - one value is added as the 1 which starts the triangle sequence and second is added by the fact we include both ends of the vector.
We could subtract 1 and get 0-based indexing but since MATLAB is 1-base indexed I decided to stay consistent and subtract 2 as it doesn't change the length of the code.

# K (oK), 19 bytes

f:{1+y*(y+x+x-1)%2}


Try it online!

-2 thanks to tip of @Razetime

• you can move the +1 to the left Aug 4, 2021 at 4:49
• @Razetime thanks Aug 4, 2021 at 4:51
• y*(y+x+x-1)%2 could be y%2%y+x+x-1 (assuming y+x+x-1 is never 0)
– ngn
Aug 5, 2021 at 12:48
• or the whole thing could be {1+/x+!y}
– ngn
Aug 5, 2021 at 12:50
• also, f: is usually not counted in the byte score
– ngn
Aug 5, 2021 at 12:51

# Commentator, 44 bytes

 {- {-//-}-}
?{-*///{- {-#          /*-}-}-}


Try it online!

outputs all entries in the triangle style sequence for n.

# J, 13 bytes

1+*+2%~]-~]*]


Try it online!

Based on same formula used by pajonk and others.

Returns mth element, with 0 indexing.

• ]-~]*] m subtracted from m*m...
• 2%~ divided by 2...
• 1+*+ plus m*n plus 1
• I don't think this conforms to any of the allowed output method. May 31, 2021 at 1:52
• I figured outputting m+1 instead of m wouldn't matter, but I just asked caird to find out. May 31, 2021 at 2:34
• @Bubbler caird confirmed my original approach was illegal (though I don't think it should be, since it's the equivalent of 0 indexing for option 1). A J translation of the closed-form formula turned out to be 1 byte shorter than 1+/\@,(+i.@<:), though, so I switched to that. May 31, 2021 at 16:03

# APL (Dyalog Extended), 21 bytes

f←{(⍺×(⍺+⍵+⍵-1)÷2)+1}


Try it online!

Dyadic function taking $$\m\$$ on the left and $$\n\$$ on the right.

# Pari/GP, 44 bytes

(n,m)->Vec(1/(1-x)^3-(2-n)*x/(1-x)^2+O(x^m))


Try it online!

# JavaScript (Node.js), 44 bytes

n=>g=(m,j=1,i=n)=>m--?[j,...g(m,j+i++,i)]:[]


Try it online!

Take a positive integer m and output the first m integers of the "triangle-style" sequence for n.

Simple recursive function.

# Charcoal, 14 bytes

Ｉ⁺⊘×Ｉη⊕η×⊖η⁻θ²


Try it online! Explanation: $$\ T'(n, m) = T(m) + (m - 1)(n - 2) \$$.

# Retina 0.8.2, 84 bytes

\d+
$* ^, -, ^- -11 ^1, -1, ^11 (?=(1*,1)?(1*))1$2
-(1*)(1*,)\1
-$2 -?, ^$|1+
$.&  Try it online! Link includes test cases. Takes n,m as input. Explanation: \d+$*


Convert to unary.

^,
-,
^-
-11
^1,
-1,
^11



Subtract 2 from $$\ n \$$.

(?=(1*,1)?(1*))1
$2  Calculate $$\ (n - 2)(m - 1) \$$ and $$\ T(m) \$$. -(1*)(1*,)\1 -$2
-?,



Take the sum.

^$|1+$.&


Convert to decimal.

# Raku, 19 bytes

{[\+] [1,|[$_..*]]}   [$_..*]    -- generate infinite list from topic variable $_ (implicity set when calling fn with 1 param) to infinity |[$_..*]    -- flatten once
[1,|[$_..*] -- put the flattened version into a list with 1 at the front [\+] [1,|[$_..*]]   -- triangular reduce on addition
{[\+] [1,|[\$_..*]]}  -- put in a block


Try it online!

# MathGolf, 4 bytes

r+Σ)


Similar as the Jelly, Vyxal, and 05AB1E answers.
Inputs are a 0-based $$\m\$$ and $$\n\$$, and it outputs the $$\m\$$'th triangle-style number.

Try it online.

Explanation:

r     # Push a list in the range [0, (implicit) first input-integer)
+    # Add the second (implicit) input-integer to each value in this list
Σ   # Sum this list
)  # Increase it by 1
# (after which the entire stack is output implicitly as result)


# Whitespace, 80 71 bytes

[S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_m][S N
S _Duplicate_m][S S S T N
_Push_1][T  S S T   _Subtract][S S S T  S N
_Push_2][T  S T S _Integer-divide][S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_n][T    S S S _Add][T   S S N
_Multiply][S S S T  N
_Push_1][T  S S S _Add][T   N
S T _Print_as_integer_to_STDOUT]


Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Formula used:

Given a 0-based $$\m\$$ and $$\n\$$, it outputs the $$\m\$$'th triangular-style number with the following formula:

$$T(n,m) = m\left(\left\lfloor\frac{m-1}{2}\right\rfloor + n\right) + 1$$

# T-SQL, 88 bytes

SELECT-sum(~number+~(1/~number)*~-~-@)over(order by number)FROM spt_values
WHERE'P'=type


Try it online

# Rust, 20 bytes

|n,m|(m+2*n-1)*m/2+1


Try it online!

Takes $$\n\$$ and $$\m\$$ and returns the $$\m\$$-th integer (0-based indexing).

Uses the straightforward formula.

# Pyth, 12 bytes

M.u++NGYUtH1


Takes $$\n\$$ and $$\m\$$, and returns the first $$\m\$$ integers of the sequence for $$\n\$$.

Try it online!