Given two inputs, a number n and a dimension d, generate the nth d-dimensional pyramid number.
That was confusing, let me try again.
For d = 1, the numbers start 1,2,3,4,5 and is the number of points in a line n points long.
For d = 2, the numbers start 1,3,6,10,15 and is the number of points in a triangle with side length n, also known as the triangle numbers e.g.
0
0 0
0 0 0
0 0 0 0
For d=3, the numbers start 1,4,10,20,35 and is the number of points in a pyramid of side n. For d=4, it's a 4-d pyramid, and so on.
Beyond this, visualization gets a bit tricky so you will have to use the fact that the nth d-dimensional pyramid number is equal to the sum of the first n d-1-dimensional pyramid numbers.
For example, the number of dots in a 3-d pyramid of side 5 is the sum of the first 5 triangle numbers: 1+3+6+10+15 = 35.
You can expect reasonable input (within your languages boundaries), although Standard loopholes apply. No builtins explicitly for this purpose (looking at you, Mathematica)
Numbers are 1-indexed, unless you specify otherwise.
Example recursive code in Javascript:
function pyramid(dim,num){ //declare function
if(dim == 0){ //any 0-dimensional is a single point, so return 1
return 1;
} else { //otherwise
function numbersUpTo(x){ //helper function to get the numbers up to x
if(x==0){ //no numbers up to 0
return [];
} else { //otherwise recurse
return [x].concat(numbersUpTo(x-1));
}
}
var upto = numbersUpTo(num).map(function(each){ //for each number up to num
return pyramid(dim-1,each); //replace in array with pyramid(dimension - 1,the number)
});
return upto.reduce((a,b)=>a+b); //get sum of array
}
}
This is code-golf, so fewest bytes wins.
nCr(n+d, d)
? If so, I'm afraid it's a duplicate of existing nCr challenges. \$\endgroup\$nCr(n+d, d-1)
\$\endgroup\$n
from 0 andd
from 1, then @Bubbler's formula is correct. \$\endgroup\$nCr(n+d-1, d)
. \$\endgroup\$