If I understand the problem correctly, you want to find the number of partitions of 1,000,000 which have size 3. e.g. Partitions of 10 of size 3 are: 1 1 8, 1 2 7, 1 3 6, 1 4 5, 2 2 6, 2 3 5, 2 4 4, 3 3 4 So there are 8.
In general a recursive function to find all partitions of y of size x is: nparts=:4 :0 if. (x =1) *. y =1 do. 1 elseif. y =1 do. 0 elseif. x >y do. 0 elseif. (x <: 0) +. y <:0 do. 0 elseif. 1 do. ((x-1) nparts (y-1)) + x nparts y- x end. ) 3 nparts 10 8 This, unfortunately blows up the stack for large y (e.g. 1,000,000). I don't have time to modify it into an iterative approach, but will try later. -------------------------------------------- On Sun, 5/20/18, Skip Cave <s...@caveconsulting.com> wrote: Subject: [Jprogramming] Quora Problem To: "programm...@jsoftware.com" <programm...@jsoftware.com> Date: Sunday, May 20, 2018, 5:43 AM Another interesting quora problem: How many distinct triplets have a sum of 1,000,000 (provided all numbers are integers and are positive)? <https://www.quora.com/qemail/track_click?al_imp=eyJ0eXBlIjogMzUsICJoYXNoIjogMTcxMDQ0NjU4Mn0%3D&al_pri=QuestionLinkClickthrough&aoid=oMo937UDzj8&aoty=2&aty=4&click_pos=1&ct=1526747413834671&et=103&id=62216bf7b30d40aea1d79bf4401c8317&request_id=289&src=1&st=1526747413838426&stories=2518686273&uid=bqluVqSeN78&v=0> The obvious straightforward solution would be to use the odometer verb: odo=: #: i.@(*/) 1e6=+/"1 odo 3#1e6 |limit error: odo | 1000000=+/"1 odo 3#1000000 Ooops. Unfortunately, I don't have near enough memory to use this approach. Any suggestions? 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