dc, 46 bytes
[[{}]]sx256?^dd3^8d^1-/8092541**r255/BF*+d0=xP
Try it online!
Input on stdin, output on stdout.
This works by computing a formula for the desired output as a base-256 number. The P command in dc is then used to print the base-256 number as a string.
Further explanation:
Let n be the input n. The dc program computes the sum of
A = floor(256^n / 255) * 125 (BF is interpreted by dc as 11*10+15 = 125)
and
B = floor((256^n)^3 / (8^8-1)) * 8092541 * (256^n).
For A:
Observe that 1 + 256 + 256^2 + ... + 256^(n-1) equals (256^n-1)/255, by the formula for a geometric progression, and this equals floor(256^n / 255). So this is the number consisting of n 1's in base 256.
When you multiply it by 125 to get A, the result is the number consisting of n 125's in base 256 (125 is a single digit in base 256, of course). It's probably better to write the digits in base 256 as hex numbers; 125 is hex 7D, so A is the base-256 number consisting of n 7D's in a row.
B is similar:
This time observe that 1 + 16777216 + 16777216^2 + ... + 16777216^(n-1) equals (16777216^n - 1)/16777215, and this equals floor(16777216^n/16777215).
Now, 256^3 = 16777216, and 8^8-1 = 16777215, so this is what we're computing as floor((256^n)^3 / (8^8-1)).
From the geometric series representation, this number in base 256 is 100100100...1001 with n of the digits being 1 and the rest of the digits being 0.
This is multiplied by 8092541, which is 7B7B7D in hexadecimal. In base 256, this is a three-digit number consisting of the digits 7B, 7B, and 7D (writing those digits in hex for convenience).
It follows that the product written in base 256 is a 3n-digit number consisting of the 3 digits 7B 7B 7D repeated n times.
This is multiplied by 256^n, resulting in a 4n-digit base-256 number, consisting of the 3 digits 7B 7B 7D repeated n times, followed by n 0's. That's B.
Adding A + B now yields the 4n-digit base-256 number consisting of the 3 digits 7B 7B 7D repeated n times, followed by n 7D's. Since 7B and 7D are the ASCII codes for {
and }
, respectively, this is the string consisting of n copies of {{}
followed by n copies of }
, which is exactly what we want for n > 0. The P command in dc prints a base-256 number as a string, just as we need.
Unfortunately, n=0 has to be treated as a special case. The computation above happens to yield a result of 0 for n=0; in that case, I've just hard-coded the printing of the string {}
.
4@mbomb007 It doesn't matter whether the definition is "wrong" or not. It's still a fine challenge (and a different one). – Martin Ender – 2017-03-06T17:53:06.263
Closely related. – Martin Ender – 2017-03-06T17:53:34.337
4@mbomb007 The test cases and the definition given in this challenge match up, and are different from the other challenge. If anything, the link could be improved, but I don't think the link is relevant to the challenge itself. – Martin Ender – 2017-03-06T17:54:22.877
He called it the Von Neumann construction, though, and that's not what this challenge is. That's what the dup is. It follows that each natural number is equal to the set of all natural numbers less than it – mbomb007 – 2017-03-06T17:55:24.077
1Can we return a set-like object such as a list of lists from a function or print our language's representation to STDOUT? – Dennis – 2017-03-06T18:14:10.840
@Dennis Yes that is fine – Post Rock Garf Hunter – 2017-03-06T19:10:59.967