13
3
Out of all of mathematics, there will always be a few theorems that go beyond all common sense. One of these is the fact that there are different sizes of infinity. Another interesting fact is the idea that many infinities which seem to be of different size are actually of the same size. There are just as many even numbers as integers, as there are rational numbers.
The general concept of this question is to confront the bizarre reality of infinity. In this challenge, your program will output a list which will:
- At any specific moment of time, always have a whole number of entries
- Eventually contain (if left to run long enough) any specific (non-zero) rational number precisely once on the entire list
- Contain an unbounded number of empty slots (entries on the list that are needlessly set to 0)
- Have a proportion of empty slots that approaches a limit of 100%
- For every positive integer N, have an infinite number of places with N consecutive empty slots
The Challenge
Your challenge is to write that shortest possible program that will output a special list with the following rules:
- All entries with an index that is not a square number should be set to zero. So, the first entry will be nonzero, the second and third will be zero, the fourth will nonzero, etc.
- All rational numbers will be in the form of an improper fraction (such as 4/5 or 144/13) that has been simplified. The exception is zeroes, which will be simply
0
. - All (positive and negative) rational numbers should eventually appear on the list if your program runs for long enough and with enough memory. For any particular rational number, the time required may be an arbitrarily large, but always finite, amount of time.
- If run for an infinite amount of time, no non-zero rational number should ever appear twice.
Rule 3 does allow for some variation, as in there is an infinite number of different possible legal outputs.
Output will be a stream of lines. Each line will be of the general form of 5: 2/3
where the first number is the entry number, then followed by the rational number. Note that 1: 0
will always be the first line of output.
Example snippet of output:
1: 1/1
2: 0
3: 0
4: 2/1
5: 0
6: 0
7: 0
8: 0
9: -2/1
10: 0
etc...
Rules, Regulations, and Notes
This is code golf. Standard code golf rules apply. Also, due to the variation allowed in the output, you need to at least show why you believe that your list will contain all possible rational numbers exactly once, and that your solution is correct.
EDIT: Since the primes numbers did distract from the challenge, I am changing it to square numbers. This accomplishes the same purpose, and also shortens the solutions.
1Note that
1: 0
will always be the first line of output. – This contradicts your example and also does not make sense to me. – Wrzlprmft – 2015-04-13T13:15:24.3371What is the point of rule 1? Do you just want people to mutually golf two separate programs to test primality and enumerate the rationals? – Peter Taylor – 2012-05-12T21:45:22.877
It shows how a very small portion of the integers still has the same cardinality as the full set of rational numbers, and it also allows the percentage of empty slots to approach (but never reach) 100%. – PhiNotPi – 2012-05-12T21:52:21.793
I'm assuming that the program also needs to run in a fixed amount of memory, i.e. it can't assume that the machine can always allocate more? Also, is it against the rules to use (say) a C int for the list index when you know that it has a finite range? (Even though the exact limit can vary with the implementation.) Is some form of bignum required? – breadbox – 2012-05-12T22:27:39.570
1@PhiNotPi, there are far simpler ways of doing that, and it's a distraction from the more interesting part of the question. – Peter Taylor – 2012-05-12T22:48:19.773
@PeterTaylor Would square numbers be a better way? – PhiNotPi – 2012-05-12T23:00:18.277
You have 0 denoting "empty" infinitely often in the list, which contradicts your requirement that each rational appear exactly once (0 is a rational, of course). It seems to me that all of your bulleted objectives would be met by simply requiring the list to look like
r_0 e r_1 e e r_2 e e e r_3 e e e e r_4 ...
, where the nth rational (r_n, n=0,1,2,3,...) is immediately preceded by ne
s (eache
denoting "empty" -- literally a blank line if the items are on separate lines). – r.e.s. – 2012-05-13T03:09:21.337Well, in the numbered list I do mention that no non-negative integer will appear twice. I'll change the billeted list to match. – PhiNotPi – 2012-05-13T04:15:53.697
Oops, I put non- negative where I meant non- zero. That's fixed now. – PhiNotPi – 2012-05-13T04:17:40.387