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

Reply via email to