Prime factors of an integer are not, in the general case, a set. And you really should be careful to avoid specifying a set when what you want is not a set.
You might be interested in https://rosettacode.org/wiki/Factors_of_an_integer#J ? That said, refinement is an important part of the specification process, so - since it seems you were looking for something different - maybe it's worth redoing the specification? Thanks, -- Raul On Mon, Oct 2, 2017 at 2:09 PM, Skip Cave <[email protected]> wrote: > My original approach was even more naive than Marc's: > > NB. from Roger Hui's Combinations essay on the J website: > https://goo.gl/WL4nXn > > c=: ((= +/"1) |.@:I.@# ]) #:@i.@(2&^) NB. Roger called this 'comb2' > > NB. I used this definition because I could cut & paste one line, and not > require an editor > > a =. 2 2 5 5 > > ~.(*/"1(4 c 4){a),(*/"1(3 c 4){a),(*/"1(2 c 4){a),, |:(1 c 4){a > > 100 20 50 4 10 25 2 5 > > > I like to sort it: > > /:~~.(*/"1(4 c 4){a),(*/"1(3 c 4){a),(*/"1(2 c 4){a),, |:(1 c 4){a > > 2 4 5 10 20 25 50 100 > > > The final goal of this exercise was to find all the divisors of an integer > (not just the prime divisors). > > > So you need to find the prime factors of the integer, for example 100: > > > ]a =. q:100 > > 2 2 5 5 > > /:~~.(*/"1(4 c 4){a),(*/"1(3 c 4){a),(*/"1(2 c 4){a),, |:(1 c 4){a > > 2 4 5 10 20 25 50 100 > > > So these are all the divisors of 100. > > > To use Raul's verb, it needs some mods. I can't do the unique until the > end, because prime factors of an integer are often duplicated. > > > F1=:1 :'u@#~ #:@i.@(2^#)' NB. Here's Raul's verb minus the initial > unique > > */F1 q:100 NB. we take the product > > 1 5 5 25 2 10 10 50 2 10 10 50 4 20 20 100 > > ~.*/F1 q:100 NB. Now we take the unique > > 1 5 25 2 10 50 4 20 100 > > /:~~.*/F1 q:100 NB. Sort it to make it pretty > > 1 2 4 5 10 20 25 50 100 > > > Didn't really need the 1, but Raul likes the empty combination. > > > Now we put it all in one verb: > > > F2=. /:~~.*/F1 q: > > F2 100 > > |length error: F2 > > | F2 100 > > F2=. /:~~.*/F1 q:] > > F2 100 > > |domain error: F2 > > | F2 100 > > > So this is above my pay grade. I'll have to stick with my inline code: > > > /:~~.*/F1 q:110 > > 1 2 5 10 11 22 55 110 > > /:~~.*/F1 q:43 > > 1 43 > > /:~~.*/F1 q:45 > > 1 3 5 9 15 45 > > /:~~.*/F1 q:444 > > 1 2 3 4 6 12 37 74 111 148 222 444 > > > So I can find all the divisors of an integer. > > > Skip > > > > > > > Skip Cave > Cave Consulting LLC > > On Mon, Oct 2, 2017 at 12:15 PM, Marc Simpson <[email protected]> wrote: > >> More naive than Raul's approach, first pass using the 'stats' lib: >> >> a=.2 5 7 >> require'stats' >> combv=: ] {~ (comb #@]) >> 3 combv a >> 2 5 7 >> 2 combv a >> 2 5 >> 2 7 >> 5 7 >> 1 combv a >> 2 >> 5 >> 7 >> combos=: (1 + i.@#) <@combv"0 1 ] >> combos a >> ┌─┬───┬─────┐ >> │2│2 5│2 5 7│ >> │5│2 7│ │ >> │7│5 7│ │ >> └─┴───┴─────┘ >> f=: 1 : ';u/"1 each combos y' >> +f a >> 2 5 7 7 9 12 14 >> *f a >> 2 5 7 10 14 35 70 >> >> /M >> >> On Mon, Oct 2, 2017 at 10:06 AM, Raul Miller <[email protected]> >> wrote: >> > For a first effort, I would go with >> > >> > F=:1 :'(u@#~ #:@i.@(2^#))@~.' >> > f=: /F >> > >> > Hopefully that makes the issues obvious - the specification here calls >> > for a result which grows exponentially with the size of the argument >> > set. >> > >> > Also: >> > >> > The ~. might be extra work, but for typical cases the effort of >> > ensuring that the argument is a set is trivial compared to the effort >> > of constructing the result. >> > >> > You did not include the empty combination in your example results, but >> > given your specification my initial inclination is to treat that as an >> > oversight. >> > >> > I defined F instead of going straight for f because for testing >> > purposes I want to be able to do (<F a), and perhaps similar things. >> > >> > Thanks, >> > >> > -- >> > Raul >> > >> > >> > On Mon, Oct 2, 2017 at 12:49 PM, Skip Cave <[email protected]> >> wrote: >> >> Given a set of integers, what is the most concise and or efficient way >> to >> >> list the numbers along with the sum of all combinations of the numbers? >> the >> >> products of all combinations? >> >> >> >> for example: >> >> >> >> a =. 2 5 7 >> >> + f a NB. 2, 5, 7, (2+5), (2+7), (5+7), (2+5+7) >> >> 2 5 7 7 9 12 14 >> >> >> >> * f a NB. 2, 5, 7, (2*5), (2*7), (5*7), (2*5*7) >> >> 2 5 7 10 14 35 70 >> >> >> >> The function 'f' should work for any verb and any size right argument >> noun >> >> vector. >> >> >> >> Skip >> >> >> >> Skip Cave >> >> Cave Consulting LLC >> >> ---------------------------------------------------------------------- >> >> For information about J forums see http://www.jsoftware.com/forums.htm >> > ---------------------------------------------------------------------- >> > For information about J forums see http://www.jsoftware.com/forums.htm >> ---------------------------------------------------------------------- >> For information about J forums see http://www.jsoftware.com/forums.htm >> > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
