35
5
Write a program, given an input n, will generate all possible n-tuples using natural numbers.
n=1
(1),(2),(3),(4),(5),(6)...
n=2
(1,1),(1,2),(2,1),(2,2),(1,3),(3,1),(2,3),(3,2),(3,3)...
n=6
(1,1,1,1,1,1) (1,1,1,1,2,1) (1,1,1,2,1,1)...
- The output may be in any order that does not break any other rules.
- The program must be written to run forever and list all applicable tuples exactly once, in theory.
- In reality, your program will reach your integer type's limit and crash. This is acceptable as long the program would run infinitely long if only your integer type was unlimited.
- Each valid tuple must be listed within finite time, if only the program were allowed to run that long.
- The output may, at your option, include zeroes in addition to the natural numbers.
- You may choose your program's output format for your convenience, as long as the separation between tuples and numbers inside each tuple is clear and consistent. (For example, one tuple per line.)
- The input (n) is an integer from one to six. Required behavior is undefined for inputs outside of this range.
- Code-golf rules apply, shortest program wins.
Thanks to "Artemis Fowl" for feedback during the sandbox phase.
I assume it is valid if when the program crashes it produces some extraneous output in addition to the tuples printed so far, right? – Luis Mendo – 2019-04-15T16:32:52.773
1Must we output as we go or would a function which yields an infinite list at the end of time sufficient? – Jonathan Allan – 2019-04-15T17:15:11.557
6
"You may choose your program's output format for your convenience, as long as the separation between tuples and numbers inside each tuple is clear and consistent" - may we output differing (albeit consistently differing) separation (e.g. like this)?
– Jonathan Allan – 2019-04-15T18:04:53.543@JonathanAllan I would have to include the output of that object's infinite contents as part of the program. – billpg – 2019-04-15T19:18:16.970
Outputting a JSON array is fine. Basically don't get hung up about the output format, just be reasonable. – billpg – 2019-04-15T19:23:25.400
1Related (integers instead of natural numbers) – Esolanging Fruit – 2019-04-16T05:16:24.477
Does "The program must be written to run forever" imply an assumption that $n > 0$? – Peter Taylor – 2019-04-16T06:27:40.893
C#, why do you not have pretty-printing for arrays...oh, wait, we can output an infinite array at the end of time? Okay. – Stackstuck – 2019-04-17T16:14:33.130
@Stackstuck No, it must print each tuple within finite time. – Ørjan Johansen – 2019-04-18T02:01:39.733
@Ørjan Johansen "I would have to include the output of that object's infinite contents as part of the program" sounds like we can defer the print step. Just not the bit where we generate the tuples. – Stackstuck – 2019-04-18T03:18:25.337