# 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. – Bubbler May 31 at 2:11
• Can I output m+1 integers instead of m? – Jonah May 31 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 at 9:43
• @Jonah No, you may not – caird coinheringaahing May 31 at 14:05
• @cairdcoinheringaahing Isn't it the equivalent of using 0-indexing, which is allowed for the 2nd option? – Jonah May 31 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 – Jonathan Allan May 31 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... – hyper-neutrino May 31 at 1:28

# 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!

# 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

# 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) – hyper-neutrino May 31 at 1:37
• jokes on you because I myself try to keep flagless unless neccesary – lyxal May 31 at 1:38

# 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.

# 05AB1E, 5 bytes

L<+O>


Try it online!

05AB1E looks like brainfuck :P

# 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 – Julian May 31 at 2:15

# 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 }.

# 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)

# 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)=>. – Bubbler May 31 at 1:30
• (m+n+n-1)/2 -> ((m-1)/2+n) -> (--m/2+n) – tsh May 31 at 3:41
• 19 – ophact May 31 at 5:41

# R, 28 bytes

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


Try it online!

Outputs mth (0-indexed) term.

Using straightforward formula.

# 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. – Bubbler May 31 at 1:52
• I figured outputting m+1 instead of m wouldn't matter, but I just asked caird to find out. – Jonah May 31 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. – Jonah May 31 at 16:03

# 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.

# kalk, 26 bytes

Nothing fancy, just bringing attention to kalk, 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.

# 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.

# APL (Dyalog Extended), 21 bytes

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


Try it online!

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