Calculate hamming weight with low hamming weight
J, weight 34
+/,#:a.i.
Usage - place the string to be measured in quotes at the end:
+/,#:a.i.'+/,#:a.i.'
34
Alternatively, taking input from the keyboard (weight 54):
+/,#:a.i.1!:1[1
hello
21
J (33)
One lower than 34!
+/,#:3 u:
Heavily inspired by this answer, but a hamming weight of one lower.
+/,#:3 u:'+/,#:3 u:'
33
J, 39
+/,#:a.i:]
This is a function taking one argument. (Or replace ]
with the string directly; as Gareth notes, that brings the cost down to 34.)
+/,#:a.i:] 'hello world' 45 +/,#:a.i:] '+/,#:a.i:]' 39