A monadic link.
Try it online! - Almost no point in this link (see below)!
In true golfer's style this is truly inefficient - it hits the 60s time out at TIO for the 365 test case! Locally this finishes in 37s.
Ẇa6ḌạÐṂ⁸Ṫ - Main link: n
Ẇ - all sublists - this has an implicit make_range on it's input
- so, for example, an input of 3 yields [,,,[1,2],[2,3],[1,2,3]]
- the important things are: that it contains both a list of the length of the
- decimal number, and a list 1 shorter; and that it's lists only contain
- non-zero numbers and are monotonically increasing in length.
6 - literal 6
a - and (vectorises), this changes all the values to 6s
- so, the example above becomes [,,,[6,6],[6,6],[6,6,6]]
Ḍ - convert to decimal (vectorises) [ 6, 6,, 6, 66, 66, 666 ]
⁸ - link's right argument, n
ÐṂ - filter keep those with minimal:
ạ - absolute difference (for 366 this keeps 66 AND 666; same goes for 3666; etc.)
Ṫ - tail - get the rightmost result (for 366 keeps 666, since it's longer)
A patch to make the same algorithm run within the 60s limit for 365 and 366 on TIO is to avoid the implicit vectorisation of
Ẇa6Ḍ€ạÐṂ⁸Ṫ (try that), however this will now seg-fault for an input of 999 (Triangle(999) is only 499,500 but each is a list of integers, making a total of Tetrahedral(999) = 166,666,500 integers, not memory efficient, at least in Python).