Collection from a sequence that constitute a perfect square
05AB1E, 10 bytes
ݨn6*æʒOŲ
Try it online!
How?
ݨn6*æʒOŲ || Full program. I'll call the input N. Ý || 0-based inclusive range. Push [0, N] ∩ ℤ. ¨ || Remove the last element. n || Square (element-wise). 6* || Multiply by 6. æ || Powerset. ʒ || Filter-keep those which satisfy the following: O ||---| Their sum... Ų ||---| ... Is a perfect square?
Haskell, 114 104 103 86 bytes
f n=[x|x<-concat<$>mapM(\x->[[],[x*x*6]])[0..n-1],sum x==[y^2|y<-[0..],y^2>=sum x]!!0]
Thanks to Laikoni and Ørjan Johansen for most of the golfing! :)
Try it online!
The slightly more readable version:
-- OEIS A033581
ns=map((*6).(^2))[0..]
-- returns all subsets of a list (including the empty subset)
subsets :: [a] -> [[a]]
subsets[]=[[]]
subsets(x:y)=subsets y++map(x:)(subsets y)
-- returns True if the element is present in a sorted list
t#(x:xs)|t>x=t#xs|1<2=t==x
-- the function that returns the square subsets
f :: Int -> [[Int]]
f n = filter (\l->sum l#(map(^2)[0..])) $ subsets (take n ns)
Pyth, 12 bytes
-2 bytes thanks to Mr. Xcoder
fsI@sT2ym*6*
Try it online!
2 more bytes need to be added to remove []
and [0]
, but they seem like valid output to me!
Explanataion
fsI@sT2ym*6*
f filter
y the listified powerset of
m*6*ddQ the listified sequence {0,6,24,...,$input-th result}
sT where the sum of the sub-list
sI@ 2 is invariant over int parsing after square rooting