| Bytes | Lang | Time | Link |
|---|---|---|---|
| 032 | Wolfram Language Mathematica | 220307T174238Z | att |
| 056 | Factor + math.combinatorics math.unicode | 220307T020135Z | chunes |
| 046 | R | 220309T095106Z | Xi'a |
| 045 | Python 3.8 prerelease | 220307T112358Z | solid.py |
| 045 | Haskell | 220307T225631Z | AZTECCO |
| 008 | Husk | 220307T115220Z | Dominic |
| 021 | Charcoal | 220307T112537Z | Neil |
| 030 | Retina 0.8.2 | 220307T111343Z | Neil |
| 008 | 05AB1E | 220307T084143Z | Kevin Cr |
| 038 | JavaScript Node.js | 220307T014906Z | tsh |
| 107 | C# Visual C# Interactive Compiler | 220307T034330Z | Andrew B |
| 010 | Pyth | 220307T014413Z | sinvec |
| 006 | Vyxal | 220307T005913Z | lyxal |
| 007 | Jelly | 220307T010758Z | Unrelate |
Wolfram Language (Mathematica), 39 37 32 bytes
<|Tr@#->#&/@Subsets@Range@#|>@#&
Returns the partition in increasing order. For decreasing order, +5 bytes: Try it online!
Factor + math.combinatorics math.unicode, 56 bytes
[ dup 1 [a,b] all-subsets [ Σ = ] with filter infimum ]
1 [a,b] all-subsetsPowerset of n..1dup ... [ Σ = ] with filterSelect the subsets that sum to the inputinfimumGet the smallest one
Python 3.8 (pre-release), 45 bytes
Inspired by the answer of user tsh. -9 bytes thanks to user att.
lambda n:n*[p:=round((2*n)**.5)]and[p]+f(n-p)
Haskell, 45 bytes
f 0=[]
f n=[a:f(n-a)|a<-[1..n],[a]>f(n-a)]!!0
f(n) searches, starting from a = 1 , a list to append to a :
- lexicographically less than a,
that sums to n (with a).
Such list is f( n-a ).
Husk, 8 bytes
↔ḟo=¹ΣṖḣ
Husk has no 'integer partitions' built-in, but since the array must be strictly decreasing (no duplicates) we can build it by selecting a subset of n..1 that sums to n.
If we do it the other way around (selecting from 1..n), then the first 'hit' is already the right set, and we just need to reverse the order of its elements to make it decreasing.
ḣ # start with range 1..input:
Ṗ # get all subsequences;
↔ḟo # now get the first element that satisfies:
Σ # its sum
=¹ # equals the input;
↔ # and finally reverse it.
Charcoal, 21 bytes
NθWLΦθ‹Σ…·⁰κθ«≧⁻ιθ⟦Iι
Try it online! Link is to verbose version of code. Explanation:
Nθ
Input n.
WLΦθ‹Σ…·⁰κθ«
Count how many triangular numbers starting from 0 are less than n and repeat until this becomes zero.
≧⁻ιθ
Subtract that number from n.
⟦Iι
Output that number on its own line.
Retina 0.8.2, 30 bytes
.+
$*
(?=(\G1|1\1)*1)1\1?
$.&¶
Try it online! Link includes test cases. Explanation:
.+
$*
Convert to unary.
(?=(\G1|1\1)*1)1\1?
Repeatedly match the smallest number whose triangular number exceeds the remainder, calculated as one more than the largest number whose triangular number is strictly less than the input.
$.&¶
Convert to decimal and list each number on its own line.
05AB1E, 8 bytes
Læí.ΔOQ
Try it online or verify all test cases.
Explanation:
L # Push a sum in the range [1, (implicit) input]
æ # Pop and get the powerset of this
# (which is already in the correct order)
í # Reverse each inner list to make the inner lists in decreasing order
.Δ # Keep the first inner list that's truthy for:
O # Where the sum
Q # Equals the (implicit) input
# (after which this found result is output implicitly)
JavaScript (Node.js), 38 bytes
f=n=>n?[p=.5+(n+n)**.5|0,...f(n-p)]:[]
That is, find out the smallest \$x\$ where
$$ \sum_{i=1}^{x}i= \frac{(x+1)\cdot x}{2}\ge n $$
which is
$$ x=\left\lceil\frac{\sqrt{8n+1}-1}{2}\right\rceil $$
As att and Bubbler suggested in the comments. This formula could be simplified into
$$ x=\left\lfloor\sqrt{2n}+\frac{1}{2}\right\rfloor $$
C# (Visual C# Interactive Compiler), 107 bytes
x=>{var y=new List<int>();while(x!=0){y.Add((int)Math.Ceiling((Math.Sqrt(x*8+1)-1)/2));x-=y[^1];}return y;}
I think i can save a few bytes somewhere here, but i can't quite figure it out. Haskell tricks with unrolling multiple arguments comes to mind but it's been too long since i golfed properly to remember it (Lost access to old account when school email expired feelsbad). feel free to improve this and take the points. Uses the same approach as the Javascript answer, but without the JS list comprehension tricks. In other words, find the lowest value such that the sum from 1 to n is greater than x, push that to the list, subtract n from x and repeat until x is 0.
From my early tests, using Floor and the third formula is the same number of characters in C#, though i couldn't really get it working.
This can save a bunch of characters by taking an empty list as a parameter and editing/filling it in place (removes the need for the "new" and the return, effectively making the outer function a one-liner. Further, it could be made recursive, which removes the while loop, though idk if that actually saves anything), but i'd guess that's blocked by loophole rules.
making the list a list of double will save 2 bytes but the results are then doubles, and this raises the possibility of floating point nonsense and potentially printing out 3.0000000004 or something similar.
Edit: Uses the new Index object and it's ^ operator, introduced in C# 8.0. Not normally a fan b/c it makes the code more obtuse, but it's perfect for code golf. ^1 is shorthand for the last element in the list.
Vyxal, 7 6 bytes
Ṅ~Þush
-1 thanks to EmanresuA reminding me that ~ exists.
Explained
Ṅ~Þush
Ṅ # From all integer partitions of the input,
~ # Keep only partitions where:
Þu # The uniqufied version equals the partition (this makes sure that only partitions without duplicates are kept)
sh # Sort the list and take the first item
Jelly, 7 bytes
ŒṗUQƑƇṂ
The only real difference from lyxal's Vyxal solution is that Jelly's integer partitions builtin generates them in nondecreasing order, requiring them to be reversed before finding the minimum.
ŒṗU Nonincreasing integer partitions.
Ƈ Filter to those which
Ƒ are unchanged by
Q uniquification.
Ṃ Take the minimum.