Ceylon, 202 bytes
object u satisfies{Integer*}{iterator()=>object satisfies Iterator<Integer>{variable value i=0;late Iterator<Integer>n;next()=>if(++i%7<1||'7'in"``i``")then(i<8then(n=iterator())else n).next()else i;};}
This is not a function, but an object declaration implementing an infinite sequence (Iterable). The object can be printed directly, print(u)
outputs this:
{ 1, 2, 3, 4, 5, 6, 1, 8, 9, 10, 11, 12, 13, 2, 15, 16, 3, 18, 19, 20, 4, 22, 23, 24, 25, 26, 5, 6, 29, 30, ... }
To print more, use printAll(u)
. The following code uses newlines, and also prints the sum (and the first 30 elements shown above):
shared void run() {
printAll(u.take(7^7), "\n");
print(sum({0, * u.take(7^7)}));
print(u);
}
Here is the ungolfed and commented version:
// Prints cantor's unspeakable numbers.
//
// Question: http://codegolf.stackexchange.com/q/101231/2338
// My answer: http://codegolf.stackexchange.com/a/101297/2338
// this object u (which is like a singleton class with its single instance)
// implements the Iterable<Integer> interface.
object u satisfies {Integer*} {
// That interface has just one formal method,
// `shared formal Iterator<Integer> iterator()`.
// Lets implement it by ...
iterator()
// ... providing for each call ...
=>
// ... a new (anonymous) object, which
// implements the Iterator<Integer> interface.
object satisfies Iterator<Integer> {
// This is the counter (the type `Integer`
// is longer than `value`, so we infer it).
// We start at 0.
variable value i = 0;
// This is a nested Iterator. It will be
// initialized when first needed, so we don't
// get an endless recursion when creating the
// first iterator.
late Iterator<Integer> n;
// `shared formal Integer next()` is the single method
// of Iterator which needs to be implemented.
next()
// each time it is called, the following
// expression will be evaluated.
=>
// increment the counter, then check if it
// is an unspeakable number.
if (++i % 7 < 1 || '7' in "``i``")
then
// if so, take the nested iterator (and the
// first time, for i == 7, create it first),
// and take its next element.
(i < 8 then (n = iterator()) else n).next()
else
// otherwise, just return i.
i;
};
}
8Please provide the first 7^7 numbers of that sequence such that we can check our solutions. – flawr – 2016-11-27T10:20:42.997
2Related – Adnan – 2016-11-27T10:29:56.547
2In case anybody wants to generate some more numbers and compare results: The sum of the first 7^77 numbers in the sequence is 3336402440238885119980169136020683586413168645292341926482898521634332654984279162327502549917668322950744929983987545341421076028 – Niklas B. – 2016-11-28T23:46:18.240
Sure, the number of 1's in that sequence is 22977, which means if you pick an element out of the first 7^77 uniformly at random, you have a 2 * 10^-61 chance of it being a 1 – Niklas B. – 2016-11-29T08:10:45.337
1
In case you are interested, here is a graph showing the growth of the number of repeated ones: https://drive.google.com/file/d/0B71iQwGfNtw5NGladjdOZVhoNkk/view?usp=sharing
– Niklas B. – 2016-11-29T09:41:44.187Thank you, @NiklasB. one can see that the depth grows polynomial and not logaritmically! – mschauer – 2016-11-29T18:56:42.580
Yeah my algorithm to compute the sum is only fast because of that fact. – Niklas B. – 2016-11-29T19:56:31.163