Somewhat clumsy, as uses each for the loop.

The key idiom is to use subarrays to iterate a window over the vector … 
        x u;.3 y

So to see subarrays of various lengths … here length 8 in i.10 (there are 3) 
and then length 9 in i.10 (there are 2) … the 2 results (for length 8 and 
length 9 are shown vertically for clarity)

  ,.((*:i.10)&(] <;. _3 [)) each 8 9                                            
                        NB. Note here the u function is just < so we can see 
the lists
┌────────────────────────────────────────────────────────────────┐
│┌───────────────────┬────────────────────┬─────────────────────┐│
││0 1 4 9 16 25 36 49│1 4 9 16 25 36 49 64│4 9 16 25 36 49 64 81││
│└───────────────────┴────────────────────┴─────────────────────┘│
├────────────────────────────────────────────────────────────────┤
│┌──────────────────────┬───────────────────────┐ │
││0 1 4 9 16 25 36 49 64│1 4 9 16 25 36 49 64 81│ │
│└──────────────────────┴───────────────────────┘ │
└────────────────────────────────────────────────────────────────┘

So now change the input vector to (*:i.100) and the length of the subarrays to 
the list 9 10 11 12 13 to show any sets that match the rule …

   ,.((*:i.100)&(] ([: < ([: 2018&= +/) # ] );. _3 [)) each 9 10 11 12 13       
           NB. Change the u function to a tacit expression to return the box of 
the subarray if its sum matches 2018
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
 │
││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
 │
│└┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
 │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
 │
│││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
 │
│└┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
 │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
 │
││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
 │
│└┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
 │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│┌┬┬┬┬┬┬┬────────────────────────────────────────────┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐│
│││││││││49 64 81 100 121 144 169 196 225 256 289 
324│││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
│└┴┴┴┴┴┴┴────────────────────────────────────────────┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘│
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
 │
││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││││
 │
│└┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘
 │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘



> On 24 Oct 2019, at 5:36 pm, Skip Cave <[email protected]> wrote:
> 
> I occasionally try to solve Quora math problems to polish my J skills.
> Here's a problem that
> 
> * "The integer 2018 can be written uniquely as the sum
> of consecutive squared integers. How many squares does the sum contain?"*
> 
> a=.2^~i.100
> 
> NB. Find how many consecutive squared integers it takes to sum to 2018. Try
> each length until I get a hit.
> 
> +/2018=+/"1]5,\a
> 
> 0
> 
> +/2018=+/"1]6,\a
> 
> 0
> 
> +/2018=+/"1]7,\a
> 
> 0
> 
> +/2018=+/"1]8,\a
> 
> 0
> 
> +/2018=+/"1]9,\a
> 
> 0
> 
> +/2018=+/"1]10,\a
> 
> 0
> 
> +/2018=+/"1]11,\a
> 
> 0
> 
> +/2018=+/"1]12,\a
> 
> 1 NB. A hit!
> 
> +/2018=+/"1]13,\a
> 
> 0
> 
> 
> So the answer is 12
> 
> List them:
> 
> b#~2018=+/"1 b=.12,\a
> 
> 49 64 81 100 121 144 169 196 225 256 289 324
> 
> 
> Is there a way to do this search without explicit looping?
> 
> 
> 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

Reply via email to