[Jprogramming] Quora Problem

2018-05-19 Thread Skip Cave
Another interesting quora problem: How many distinct triplets have a sum of 1,000,000 (provided all numbers are integers and are positive)?

Re: [Jprogramming] Quora Problem

2018-05-19 Thread james faure
This is a good example of a really annoying limitation of the current interpreter's eager evaluation strategy, in this cases one is forced to come up with some sort of non idiomatic iterative 'hack' to get a result. Concretely, I guess you're forced to use an accumulator with ^: or an explicit l

Re: [Jprogramming] Quora Problem

2018-05-19 Thread james faure
This is a good example of a very annoying limitation of the current interpreter's eager evaluation strategy, in this case one is forced to come up with some sort of non-idiomatic iterative 'hack' to get a result. Concretely, I guess you're forced to use an accumulator with ^: or an explicit loop

Re: [Jprogramming] Quora Problem

2018-05-19 Thread Henry Rich
I don't think it's the interpreter's strategy that is at fault. odo 3#1e6 is asking the machine to create a set of 1e18 triplets, which you will then presumably check to see if they add to 1e6, and then cast out duplicates.  If somehow the interpreter could feed you the 1e18 values one at a tim

Re: [Jprogramming] Quora Problem

2018-05-19 Thread 'Pascal Jasmin' via Programming
This is more math, than eager/lazy evaluation issue. Though if lazy (programmer), you might use solution for 10 and 100 to guess at 1M. It's an extention of unique pairs that add up to a sum. <.@-: sum The extention is a matter of avoiding duplicates while summing to 1M less the largest of the

Re: [Jprogramming] Quora Problem

2018-05-19 Thread james faure
Lazy evaluation won't help you cut the processing time, but it does reduce the memory footprint to practically null, and the lambda calculus involved opens the possibility for further optimisations. I guess I don't like having to think about optimizing my J code (if it makes it any less readabl

Re: [Jprogramming] Quora Problem

2018-05-19 Thread 'Jon Hough' via Programming
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:

Re: [Jprogramming] Quora Problem

2018-05-19 Thread Roger Hui
The number of triplets is the number of size 3 partitions of 1e6. Using the function in http://code.jsoftware.com/wiki/Essays/Partitions#Partitions_with_k_Parts pnk=: 4 : 0 " 0 n=. 0>.x [ k=. y if. 1>:n<.k do. x: (0 wrote: > Another interesting quora problem: > > How many distinct triplets hav

Re: [Jprogramming] Quora Problem

2018-05-19 Thread Skip Cave
The problem is to find and count all of the unique sets of three positive integers that sum to 1,000,000. For example, one of the triples is: +/32 33 35 100 Another one: +/31 33 36 100 Yet another: +/98 1 1 100 Now just find all the other unique on

Re: [Jprogramming] Quora Problem

2018-05-19 Thread Roger Hui
> sets of three positive integers that sum to 1,000,000 That's a partition of size 3 (of 1e6). http://en.wikipedia.org/wiki/Partition_(number_theory) On Sat, May 19, 2018 at 11:17 PM, Skip Cave wrote: > The problem is to find and count all of the unique sets of three positive > integers that