-1
\$\begingroup\$

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

\$\endgroup\$

closed as unclear what you're asking by xnor, tsh, Luis Mendo, John Dvorak, manatwork Jul 8 at 8:51

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

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

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

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

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)
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Hi do you know any online editor support it so I can test? \$\endgroup\$ – chau giang Jul 8 at 8:41
  • \$\begingroup\$ @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. \$\endgroup\$ – Expired Data Jul 8 at 9:20
  • \$\begingroup\$ @chaugiang here's a pastebin for output for 10 instead of 1000 \$\endgroup\$ – Expired Data Jul 8 at 9:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.