> From: Andrea Ambu
> I'm looking for the number of prime factors of a list of numbers
> If I was going for a single number i'd do:
>
> > #q:8
> > 3
>
> which is perfect. But I can't do it for a list of numbers, as q:1+i.10
> returns a matrix and # returns the number of rows. How can I get the
> number of prime factor line by line?
>
Welcome Andrea,
Matthew and Alex have both done a great job of showing and explaining how to
use @ to solve the problem you're having above.
I just wanted to add that sometimes (not in this case) you when you want to
work with a ragged array (lists of dissimilar length), the best answer is to
box the lists to prevent the fills that otherwise appear. To use Matthew's
example:
q: 453
3 151
q: 32
2 2 2 2 2
q: 453 32
3 151 0 0 0
2 2 2 2 2
]b=: <@q: 453 32
+-----+---------+
|3 151|2 2 2 2 2|
+-----+---------+
#&> b NB. count
2 5
+/&> b NB. sum
154 10
~.&.> b NB. No duplicates
+-----+-+
|3 151|2|
+-----+-+
> 2 - Zeroes matter when counting.
>
> How can I eliminate items with value 0 from a list?
> i.e. given 1 0 2 0 3 0 0 4 5 0 0 6 i'd like to obtain 1 2 3 4 5 6
>
Obviously the best solution is to prevent the 0 fills in the first place using
one of the methods described. But if you really want to count the non-zero
numbers in each row of an array, then you could do this:
q: 453 32
3 151 0 0 0
2 2 2 2 2
#...@-.&0"1 q: 453 32
2 5
countrows is an explicit verb that would do the same thing:
countrows=: 3 : '# y -. 0'"1
countrows q: 453 32
2 5
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm