19
0
Description
There have been quite a few other challenges concerning these numbers before, and I hope this one is not among them.
The n th triangular number equals the sum of all natural numbers up to n, simple stuff. There are a wikipedia page and an entry at OEIS, for those who wish to inform themselves further.
Now, Gauss found out that every natural number may be expressed as a sum of three triangular numbers (these include 0
), and it is fine to have one number more than once, e.g. 0 + 1 + 1 = 2
.
Challenge
Your task is to write a program or function, given a natural number (including 0
), prints three triangular numbers that sum up to the argument. You may print the numbers separeted by spaces, as an array, or by another method you like. However, it is forbidden to use any builtin functions to directly get an array, a range or any other form of collection containing a list of triangular numbers (for instance a single atom that yields the range).
Test cases
9 -> 6 + 3 + 0 or 3 + 3 + 3
12 -> 6 + 6 + 0 or 6 + 3 + 3 or 10 + 1 + 1
13 -> 6 + 6 + 1
1 -> 1 + 0 + 0
0 -> 0 + 0 + 0
Note: If there is more than one possible combination, you may print any or all, but you must print any combination only once, eliminating all combinations that are a result of rearranging other combinations. I'd really appreciate a try-it link and an explanation, I really love to see how you solve the problem ;)
This is code-golf, so standard loopholes apply. May the shortest answer in bytes win!
You're welcome to! ;) – racer290 – 2017-07-17T12:47:54.997
1For 12 you can also do 1 + 1 + 10. – Erik the Outgolfer – 2017-07-17T12:59:04.277
I guess
INPUT a: PRINT 0,0,a
would be a loophole? – steenbergh – 2017-07-17T13:25:32.2801@steenbergh
a
won't always be a triangular number – Felipe Nardi Batista – 2017-07-17T13:28:36.4303I can parse "builtin functions to directly get an array, a range or any other form of collection containing a list of triangular numbers" in two ways, but neither of them makes sense. The first prohibits all builtins which directly get an array, but that seems to prohibit all use of arrays in every language I know; the other prohibits builtins to "directly get ... a range ... containing a list of triangular numbers", but I don't know what that would mean. – Peter Taylor – 2017-07-17T13:38:46.550
@PeterTaylor that means that no builtin functions returning a collection of triangular numbers and taking no arguments are banned - you must generate the required collection by using a formula or the sum of the necessary natural numbers instead. – racer290 – 2017-07-17T13:42:11.090
2So built-in functions which take an argument
n
and return a list of the firstn
triangle numbers are permitted? That feels rather targetted against some specific language, although I don't know which. – Peter Taylor – 2017-07-17T13:49:32.1734I urge you to lift this restriction. I promise you it won't improve the inter-language answer quality or fairness in the way you think. – Lynn – 2017-07-17T13:51:16.010
1For 13 you can do 10+3+0, right? – Kevin Cruijssen – 2017-07-17T14:17:13.657
@PeterTaylor I'm not, I just want to avoid that one can just use a collection of triangular numbers that is directly provided by the language they use. I'm not familiar with any golfing language, so I can't know if there is a builtin in some language. I want them to solve the generation of the numbers on their own, because there are a different ways, and it gives the challenge a bit of a twist, in my opinion. – racer290 – 2017-07-17T15:07:35.747
@KevinCruijssen Yep. – racer290 – 2017-07-17T15:08:11.313
1@racer290 He's asking because for the other test cases you listed all possible answers. – mbomb007 – 2017-07-17T15:43:02.917