Your ixbin is equal to (4&$.$.)
R.E. Boss > -----Oorspronkelijk bericht----- > Van: Programming <programming-boun...@forums.jsoftware.com> > Namens 'Mike Day' via Programming > Verzonden: donderdag 24 oktober 2019 11:08 > Aan: programm...@jsoftware.com > Onderwerp: Re: [Jprogramming] Quora problem > > (I see RE Boss has just posted, while I was drafting - sorry if it's the > same!) > > I thought to exploit the property that > S3(n) = sum i^2, i <: n, is n(n+1)(2n+1)%6 but solving S(n) - S(m) = 2018 > isn't > that easy! > Anyway, we could define > S3 =: 6 <.@%~ (* (>: * >:@+:)) > but as we need a string of squares it's overkill. > Either way, it's useful to be able to identify the indices of ones in a > boolean > matrix: > ixbin =: $#.inv I.@, > > So we get, accprding to taste: > > -/"1 ixbin 2018 = -/~ +/\ *: i. >: 20 > 12 > -/"1 ixbin 2018 = -/~ S3 i. 20 NB. not as efficient as it looks! > 12 > > > ts'-/"1 ixbin 2018 = -/~ S3 i. 20' > 1.53e_5 8768 > ts'-/"1 ixbin 2018 = -/~ +/\ *: i. >: 20' > 8.7e_6 9856 > > MIke > > > > > On 24/10/2019 07:36, Skip Cave 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 > > > > -- > This email has been checked for viruses by Avast antivirus software. > https://www.avast.com/antivirus > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm