Let me add that, following this thread so far, I too learned a few things today - and pardon me for outlining the steps for further reference.

I (again) did a very base level approach:

   3 + 8 9 10
11 12 13
   5 + 8 9 10
13 14 15
    3 5 + 8 9 10
|length error
|   3 5    +8 9 10

At that stage I realized that (/) isn't only "Insert" but also used to force a "Table":

    3 5 +/ 8 9 10
11 12 13
13 14 15

These lines take the "Modulo" and, in a second step, throw a "1" for a match [division result zero], "0" otherwise):

    3 5 |/ 8 9 10
2 0 1
3 4 0
    3 5 (0=|/) 8 9 10
0 1 0
0 0 1

I then combined these results by ORing (+.) the two rows:

   +./  3 5 (0=|/) 8 9 10
0 1 1

Here comes Raul for help with his "first multiply, then add up" example

   2 * 3 5 7
6 10 14
    2 +/ .* 3 5 7
30

This technique seems to work for anything dyadic, e.g. if I replaced the "multipy" (*) with [third] "root" (%:) I got

   3 %: 2 3 4
1.25992 1.44225 1.5874
   3 +/ .%: 2 3 4
4.28957

As he (Raul) pointed out, if one replaces the "multiply" with the "equal to zero comparison" one can rewrite thus

   3 5 (0 +./ .=(|/)) 8 9 10
0 1 1
   3 5 (0 +./ .=(|/8+i.)) 3
0 1 1

Going back to the original example

   3 5 (0 +./ .=(|/i.)) 20
1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 0 0 1 0

which I now understand a bit better, reading from right to left, as

- take the current index (i.),
- check the modulo result (|/),
- compare with zero(0 .=),
- OR the table rows (+./) produced by the left vector (to get the result vector).

And this is a fairly general notation; playing around (this is fun):

   3 5 7 (0=(|/i.)) 25
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0
   3 5 7 (0 +./ .=(|/i.)) 25
1 0 0 1 0 1 1 1 0 1 1 0 1 0 1 1 0 0 1 0 1 1 0 0 1

Thanks for your patience ...
-M



At 2016-05-05 22:03, you wrote:
> 3 5 (0 +./ .= (|/ i.)) 20 the ".=" is hard to read. Its equivalent to ". =" or in this case 3 5 (0 +./@:= (|/ i.)) 20 ----- Original Message ----- From: Geoff Canyon <[email protected]> To: [email protected] Sent: Thursday, May 5, 2016 5:54 PM Subject: Re: [Jprogramming] Project Euler 1 So there are a few learning opportunities here -- euphemism for things I don't understand ;-) I get how adding 0 = transforms the modulo results into a 1 for "divisible" and 0 for "not divisible": 3 5 (0 = (|/ i.)) 20 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 But I'm not seeing how this combines those results with an OR: 3 5 (0 +./ .= (|/ i.)) 20 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 0 0 1 0 Maybe I'm just not seeing how the forks/hooks resolve themselves? I seem to recall there was a command to get J to box a command to show how that flow works, but I don't remember it. Or maybe that's not it at all and I'm just confused. thx gc On Wed, May 4, 2016 at 11:35 PM, Raul Miller <[email protected]> wrote: > On Wed, May 4, 2016 at 10:35 PM, Geoff Canyon <[email protected]> wrote: > > So I tried to write code to solve the general case of Project Euler > problem > > 1. The problem given is to find the sum of all the positive integers less > > than 1000 that are divisible by 3 or 5. Obviously the specific case is > > highly optimizable. But I wanted to solve the general, with any number of > > divisors and any upper limit. > > ... > > Here's the code. As always, I suck at J, so improvements/suggestions are > > welcome. > > > > pe1 =: +/@(([:i.]) * 1&-@(0&i.)@*/"1@|:@(|"0 1 i.)) > > Maybe: > pe1=: [:+/@I. 0 +./ .= (|/ i.) > > ? > > Assuming email anti-spam bots do not eat my line for including an @ > character. Maybe, instead: > > pe1=: [:+/ [:I. 0 +./ .= (|/ i.) > > ... > > -- > Raul > ---------------------------------------------------------------------- > 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

Reply via email to