list - Consolidating this J code -
i'm learning j , starting basic; adding multiples of 3 , 5 below 100. got code:
(+/((((i.100)|~ 3) = 0) # (i.100)),((((i.100)|~ 5) = 0) # (i.100)))-(((i.100|~15)=0) # (i.100))
but seems there should easier way. there way make code cleaner? thanks.
note current code gives length error have suggested edit question make work. i'll include working code below.
(+/((((i.100)|~ 3) = 0) # (i.100)),((((i.100)|~ 5) = 0) # (i.100))) - (+/(((i.100)|~15)=0) # (i.100))
the same algorithm can written more (less parentheses anyway) altering order of operations (j evaluates "sentences" right left).
(+/ ((0 = 3|i.100) # i.100) , ((0 = 5|i.100) # i.100)) - +/(0 = 15|i.100)#i.100 2318
rather subtracting sum of multiples of 15 original sum avoid double counting number multiples of both 3 , 5, use ~.
(nub) remove duplicates list of multiples of 3 , multiples of 5 before summing them.
+/ ~. ((0 = 3|i.100) # i.100) , (0 = 5|i.100) # i.100 2318
for more jish approach problem see answer this stackoverflow question.
Comments
Post a Comment