The sum of consecutive odd numbers Although related challenges have been asked, this one is different to warrant its own question.



Challenge

Given a positive integer, return the longest sequence of consecutive positive odd integers whose sum is the given integer. If no such sequence exists, you may report an error in whatever way makes sense for your language, including returning a falsy value or throwing an exception.

Test Cases


  1 -> [1]
  2 -> []
  3 -> [3]
  4 -> [1, 3]
  5 -> [5]
  6 -> []
  9 -> [1, 3, 5] (note that [9] is not a valid answer)
 15 -> [3, 5, 7]
104 -> [23, 25, 27, 29] (note that [51, 53] is not a valid answer)


Scoring

This is code-golf, so the shortest answer in each language wins.
Haskell, 67 65 63 62 58 bytes

Saved 4 bytes thanks to Julian Wolf



f x=[[2*n+1,2*n+3..2*m]|n&lt;-[0..x],m&lt;-[n..x],m^2-n^2==x]!!0


Try it online!

I check if the number can be expressed as he difference of two squares: m^2-n^2. I can then construct the list of consecutive odd numbers: [2n+1,2n+3...2m-1]. Note that because the minimum n is chosen, the longest list will be output