Find all a, b, c, d, e, such that a + b + c + d + e = 1000 with no more than 3 loops [closed]

The rule is:

• a, b, c, d, e is an integer from 0 to 1000, no relation between a, b, c, d, e so a can equal to b or c, ...
• can not use more than 3 loops (it can be 3 nested loops or 3 normal loops, or 2 nested loops + 1 normal loop, ...)
• you need to find all the answers for (a, b, c, d, e) that is:
a + b + c + d + e = 1000

• Your answer can be any format: line by line, array, object, ... but it must be clear so anyone can see it.

• Shortest bytes will be win for each language!

For example here is when I used 5 loops to solve this puzzle and it violates the rules:

https://repl.it/repls/BestKnobbyDonateware

• Using no more than 3 loops is a non-observable requirement. – Arnauld Jul 8 '19 at 7:38
• stop assuming features of languages. How is a loop count? – tsh Jul 8 '19 at 7:38
• @Arnauld I am not sure about that, I think any language can do that. tsh: I updated my question. – chau giang Jul 8 '19 at 7:45
• Clearly, this can be done with no loops: Just print the hard-coded numbers. – Adám Jul 8 '19 at 8:03
• @Adám: It is brilliant idea, you rock! – chau giang Jul 8 '19 at 8:04

APL (Dyalog Unicode), 23 16 bytesSBCS

Full program. No loops. Prints a list of quintuplets with two spaces between them. Each quintuplet has the variables separated by single spaces. Requires 0-indexing (⎕IO←0) which is default on many systems.

⍸1E3=⊃∘.+/5⍴⊂⍳1001


Since the above code requires more memory than available on TIO, here's exactly the same, but with a + b + c + d + e between 0 and 10 and a sum of 10:

⍸1E1=⊃∘.+/5⍴⊂⍳11.0


Try it online!

⍳1001 the ɩntegers 0…1000

⊂ enclose to treat as a unit

5⍴ reshape that to length 5

∘.+/ reduction by outer-sum (like outer product, but with plus)
this creates a 5D array with all dimensions being 1000 elements long

⊃ disclose (the reduction had to enclose the result to reduce the rank from 1 to 0)

1E3 5D Boolean mask where equal to 1000

⍸ list of ɩndices where true

• the output has a lot of number how can read it? – chau giang Jul 8 '19 at 8:32
• @chaugiang I've rewritten it. It now prints a b c d e with single spaces in between and quintuplets separated by two spaces. – Adám Jul 8 '19 at 8:37
• awesome, it look so much better now! – chau giang Jul 8 '19 at 8:39
• @Adám Out of curiosity, is there any reason for the trailing .0 in the TIO-10 version? – Kevin Cruijssen Jul 8 '19 at 8:39
• @KevinCruijssen Just to keep the code length identical to the full version. – Adám Jul 8 '19 at 8:40

05AB1E, 8 bytes

₄Ý5ãʒO₄Q


No loops.. (Well, maybe 1 if you count the filter as a loop I guess.)

Outputs as a list of lists of integers.

Try it online with 10 instead of 1000.

Explanation:

₄Ý        # Push a list in the range [0,1000]
5ã      # Create quintuples by repeating the cartesian product 5 times
ʒ     # Filter this list by:
O    #  Where the sum
₄Q  #  Is equal to 1000
# (output the result implicitly after the filter)


VDM-SL, 57 bytes

{[a,b,c,d,e]|a,b,c,d,e in set{0,...,1000}&a+b+c+d+e=1000}


Set comprehension - 0 loops

• Hi do you know any online editor support it so I can test? – chau giang Jul 8 '19 at 8:41
• @chaugiang don't think there is one, you can use overture though which is based off eclipse and should support all platforms if you're really interested. It's worth pointing out that it probably will take several hours to terminate. – Expired Data Jul 8 '19 at 9:20
• @chaugiang here's a pastebin for output for 10 instead of 1000 – Expired Data Jul 8 '19 at 9:33