# 康托尔难以置信的数字

58

``````1 2 3 4 5 6 ( ) 8 9 10 11 12 13 ( ) 15 16 ( ) 18 ...
``````

Cantor版本的游戏是通过将序列“ 1 2 3 4 5 6（）8 ...”递归填充到上面的间隙（）中来定义的序列。

`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 31 32 33 34 1 36 8 38 ... `

8

2

2

1

Niklas B.

6

# Pyth，25 23 22字节

``````.V1=+Y
?}7+PbjbT@Y~hZb
``````

``````.V1=+Y
?}7+PbjbT@Y~hZb

Z = 0, Y = []    Implicit variable assignment
.V1              Infinite incrementing for loop with variable b, starting at 1:
=+Y            Y = Y +
(newline)          (Implicitly print the result of the following:)
?                   If
}7                  7 is in
Pb                the prime factorisation of b
+                  or
jbT             the digits of b:
@Y            Index into Y at index
Z          Z
~h          (Increment Z)
else:
b      b
``````

1
23个字节。它可以工作，因为cuz `7`是素数，因此可以通过检查素因数分解来完成

Maltysen，2016年

1

mschauer '16

23

# Python 2，77 75 74 70字节

``````def f(n=0):
i=f()
while 1:n+=1;yield next(i)if'7'in`n`or n%7<1else n
``````

mschauer

@mschauer谢谢，没想到那个。
PurkkaKoodari '16

`if n%7<1or'7'in`n`else n`可能会稍快一些（相同的字节数），因为`n%7<1`它比检查字符串快，而且`or`短路。太糟糕了，`yield[n,next(i)][n%7<1or'7'in`n`]`无法正常工作。
mbomb007 '16

@ mbomb007我认为速度不是这里的问题，但谢谢。:)
PurkkaKoodari's

10

# Perl，47 46 41 39字节

``````say\$_=\$_%7*!/7/?\$_:\$a[\$b++]for@a=1..1e6
``````

### 代码细目：

``````say\$_=\$_%7*!/7/?\$_:\$a[\$b++]for@a=1..1e6
@a=1..1e6 #Assign the range (1..1,000,000) to the array @a
for          #and then loop through this list, with \$_ as an alias for the list member.  As an alias, modifying \$_ modifies @a.
\$_%7*!/7/?\$_:\$a[\$b++]             #Ternary operation
\$_%7                              #Returns the residue modulo 7...
*!/7/                         #...and multiplies it by the negation of whether or not there exists a 7 \$_
#Since % and * have the same operator precedence, it must be evaluated in this order
#otherwise we would get (!/7/*\$_)%7 instead of (\$_%7)*!/7/
?\$_                      #If the result is non-zero (i.e. truthy), then return \$_
:\$a[\$b++]             #Otherwise, return the \$b-th element of @a, and increment \$b
\$_=                                  #Reassign the result back to \$_, modifying @a
say                                     #Prints the result of the assignment, separated by newlines
``````

1
`say\$a[@a]=\$_=...`如果我没记错的话赢2个字节。

@Dada实际上因为这使我不必分配给`\$_`，所以它节省了5个字节。谢谢！

1

haneefmubarak

@Grimy，请不要编辑其他代码。如果您想改善答案，请添加包含该改善的评论，或发布您自己的答案。由于您可能不会通过评论进入OP，只需发布​​您自己的答案即可。
ovs

5

# PHP，80（Wahooka）57 54字节

``````for(;;)echo\$a[]=strpos(++\$n,55)<-\$n%7?"\$n ":\$a[+\$b++];
``````

5

``````i#x|mod x 7<1||'7'`elem`show x=f!!i:(i+1)#(x+1)|y<-x+1=x:i#y
f=0#1
``````

`f` 是数字的无限列表。

`f`开始一个新的迭代，`1`并使用一个索引选择要从0开始的数字。每当有间隔时，我们都会选择一个新的迭代作为它的`ith`元素，并使用继续当前的迭代`i+1`。如果没有差距，我们采用当前数字`x`并继续增加`i`

4

# MATL，26 25字节

``````9e5:`t7\yFYA!7-A*~s:2M(2M
``````

### 这个怎么运作

``````9e5:       % Generate array of numbers [1 2 ... 9e5]. This array will become the
% output, after some numbers have been replaced
`          % Do...while
t        %   Duplicate the array of numbers
7\       %   Modulo 7. Gives zero for multiples of 7
y        %   Duplicate the array of numbers
FYA!     %   Matrix of decimal digits, with a column for each number
7-       %   Subtract 7 to each entry of that matrix
A        %   Array that contains "true" for columns that only contain nonzeros;
%   that is, for numbers that do not have 7 as digit
*        %   Multiply. This corresponds to a logical "and" of the two conditions.
%   A zero indicates that the number at that index needs to be replaced
~        %   Logical negate. Each "true" corresponds to a number to be replaced
s        %   Sum. This is the amount of numbers to be replaced, say n
:        %   Push array [1 2 ... n]
2M       %   Push array of logical values again
(        %   Replace the numbers at the positions indicated by the logical array
%   by the values [1 2 ... n]
2M       %   Push n again. This is used as loop condition, so if it is nonzero
%   the next iteration will be executed. Note that this executes one
%   too many iterations: the exit condition is that no replacing has
%   been needed in the current iteration; but then the current iteration
%   (which will be the last) was not really necessary. This does not
%   matter; the last iteration is useless but also harmless
% End do...while implicitly. Display implicitly
``````

3

# Tcl，121字节

``set r 0;set n 0;while {[set r [expr \$r+1]]} {if {![expr \$r%7]||(7 in[split \$r ""])} {puts [set n [expr \$n+1]]} {puts \$r}}``

``````set r 0
set n 0
while {[set r [expr \$r+1]]} {
if {![expr \$r % 7] || (7 in [split \$r ""])} {
puts [set n [expr \$n+1]]
} {
puts \$r
}
}``````

sergiol

sergiol '17

sergiol

3

# PHP，106 80字节

``````for(\$n=1;;\$n++)echo\$a[]=!(strpos(\$n,"7")>-1||\$n%7==0)?"\$n ":array_shift(\$a)." ";
``````

``````\$n=1;while(1){if(!(strpos(\$n,"7")>-1||\$n%7==0)){echo\$a[]=\$n." ";}else{echo\$a[]=array_shift(\$a)." ";}\$n++;}
``````

1
`for(\$n=1;;\$n++)echo\$a[]=!(strpos(\$n,7)>-1||\$n%7==0)?"\$n ":array_shift(\$a)." ";`我不知道字节数，但是我敢肯定它比106字节低很多。试试看，看看是否可行。

Wahooka '16

1

7。– Crypto

1

Christoph

3

# 朱莉娅62字节

``````x=[];j=0;for i=1:7^7;x=[x;i%7<1||('7' in "\$i")?x[j+=1]:i]end;x
``````

3

# Perl 6的， 74个57 54  53字节

``sub u{my@u;(1..*).map: {if \$_%%7||.comb('7') {@u||=u;@u.shift} else {\$_}}}``
``sub u{(1..*).map: {\$_%%7||.comb('7')??(@||=u).shift!!\$_}}``
``sub u{map {\$_%%7||.comb('7')??(@||=u).shift!!\$_},1..*}``
``sub u{map {\$_%%7||.comb(~7)??(@||=u).shift!!\$_},1..*}``

## 展开：

``````sub u{
map             # for each element transform using:

{ # bare block lambda with implicit parameter ｢\$_｣

\$_ %% 7     # if it is divisible by 7
||          # or
.comb(~7)   # contains the number 7 (implicit method call on ｢\$_｣)

??            # then
( @ ||= u ) # store a new instance of the Seq into an unnamed state array if it is empty
# ( it is only empty the first time it is seen in this Seq instance )
.shift      # pull one off of the front

!!            # else
\$_          # return the value
},

1 .. *          # infinite range starting at one ( elements to be mapped over )
}``````

## 测试：

``````\$ time perl6 -e'sub u{map {\$_%%7||.comb(~7)??(@||=u).shift!!\$_},1..*};put 203511962727 == sum u()[^7**7]'
True

real    2m45.744s
user    2m45.416s
sys     0m0.212s``````

2

# 锡兰，202字节

``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;};}``

`{ 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, ... }`

``````shared void run() {
printAll(u.take(7^7), "\n");
print(sum({0, * u.take(7^7)}));
print(u);
}``````

``````// Prints cantor's unspeakable numbers.
//
// Question:  http://codegolf.stackexchange.com/q/101231/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;
};
}``````

2

# Ruby，80个字节

```l=->x{x%7==0||x.to_s[/7/]};a=(1..100);b=a.reject &l p a.map{|x|!l[x]?x:b.shift}```

1

ETHproductions 2016年

Christopher Lates'Nov

ETHproductions 2016年

2

# Dyalog APL，39 个字节

`{(⍵⍴⍨⍴i)@(i←⍸('7'∊¨⍕¨⍵)∨0=7|⍵)⊢⍵}⍣≡⍳7*7`

`⍳7*7`是1 2 3 ... 7 7

`{ }⍣≡`定点运算符-重复应用功能直到结果稳定

`A@I⊢B` 修改操作员-取代在指数中的元素`I``B``A`

`0=7|⍵` 参数可被7整除的位掩码

`'7'∊¨⍕¨⍵` 位掩码，其中参数的十进制格式包含7

`∨` 要么

`⍸` 上述位掩码中的哪一个是正确的？

`i←` 分配给 `i`

`⍵⍴⍨⍴i` 将参数重塑为 `i`

mschauer

2

# C 157155字节

``````int c,r;main(_,p){if(_){p=--_;c[_]=1;for(;;){printf("%d ",c[_]);main(0,++_+1);c[_]=r?_+1:c[p++];}}else!p?r=1:p%7?p%10-7?main(0,p/10):(r=0):(r=0);}
``````

``````int cantor_n;

int cantor_n_safe(int x) {
if (!x) return 1;
if (x % 7 == 0) return 0;
if (x % 10 == 7) return 0;
return cantor_n_safe(x / 10);
}

int main(_, prev_index) {
prev_index = --_;
cantor_n[_] = 1;
for(;;) {
printf("%d ", cantor_n[_]);
_++;
if (!cantor_n_safe(_+1)) {
cantor_n[_] = cantor_n[prev_index++];
} else {
cantor_n[_] = _+1;
}
}
return 0;
}
``````

``````int c;int r;
safe(x){
!x?
r=1:
x%7?
x%10-7?
safe(x/10):
(r=0):
(r=0);
}

main(_){
int p;
p=--_;
c[_]=1;
for(;;){
printf("%d ",c[_]);
safe(++_+1);
if (!r) {
c[_]=c[p++];
} else {
c[_]=_+1;
}
}
}
``````

LambdaBeta

2

## R，86个字节

``````x=1;while(T<7^7){T=T+1;x[T]=if(!T%%7|7%in%el(strsplit(c(T,""),""))){F=F+1;x[F]}else T}
``````

-4字节替换`7%in%el(strsplit(c(T,""),""))``55%in%utf8ToInt(paste(T))`？（未经测试）
JayCe

2

# C-115字节

``````s,r;g(x){return x%10-7&&(!x||g(x/10));};f(i){(r=++s[i])%7&&g(r)||f(i+1);}main(){for(;;f(0),printf("%d\n",r));}
``````

mschauer

2

## Javascript，80个字节

``````n=[]
r=l=>(m=n[l]=++n[l]||1,!/7/.test(m)m%7?m:r(l+1))
for(;;)console.log(r(0))
``````

``````n = []
r = l => (m = n[l] = ++n[l] || 1, !/7/.test(m) && m % 7 ? m : r(l + 1))
var tot = 0
for (i = 0; i + 1; i++) {
v = r(0)
tot += v
if (i > Math.pow(7, 7) - 11) {
console.log(v)
}
if (i === Math.pow(7, 7) - 1) {
console.log(tot)
break
}
}
``````

SyntaxError：缺少），放在括号中
l4m2，18年

1

# Mathematica，82个字节

``````Nest[#2&[i=1,If[Or@@(#==7&)/@IntegerDigits@#,i++,#]&/@#]&,Table[i,{i,7^7}],20]
``````

1

# JavaScript 81个字节

### 原始（98字节）

``````for(c=0,i=1;i<=Math.pow(7,7);i++)/7/.test(i)||i%7==0?(6==c?c=1:c++,console.log(c)):console.log(i);
``````

### 打高尔夫球

``````p=console.log;for(c=0,i=1;i<9e5;i++)/7/.test(i)||i%7==0?(6==c?c=1:c++,p(c)):p(i);
``````

DJMcMayhem

DJMcMayhem

1

## Befunge，100或156个字节

``````"O":0>\#09#:p#-:#1_v<0\$.<
9\*"~"g9+1:+1::p00:<+3\$_^#g01\$\$<v"~":/"~"p9g00%"~"::+1+g9\*"~"+g
:#15#+5#g+#0%#17#\-#/!#+\#5:#5_^>%00g1+9p"~"/00g2+9p::7%!10>>p#0
``````

``````"O":0>\#09#:p#-:#1_v<0\$.<
%7::p9g00:+1g9:p00:<+1\$_^#g01\$\$<v01!
:#15#+5#g+#0%#17#\-#/!#+\#5:#5_^>p#0
``````

1

## Clojure，130字节

``````#(let[R(range(inc %))](rest((reduce(fn[[r s]i](if(or(=(mod i 7)0)((set(str i))\7))[(assoc r i(r s))(inc s)][r s]))[(vec R)0]R)0)))
``````

1

## Perl6，41个字节

``````put {(\$_=++\$)%%7|m/7/??@_[\$++]!!\$_}…1e6
``````

1

# Tcl，64字节

``````while 1 {puts [expr {[incr i]%7&&7ni[split \$i ""]?\$i:[incr s]}]}
``````

hdrz

mschauer

@mschauer：好的，有时间我会解决的……
sergiol

@hdrz我尝试了您的解决方案，它与mschauer讲述的问题相同！
sergiol

1

# JavaScript，64字节

``````for(f=n=>!/7/.test(m=++n)*m%7?m:f(n.t=n.t||);;)alert(f(f))
``````

``output=[];index=0;for(f=n=>!/7/.test(m=++n)*m%7?m:f(n.t=n.t||);index<100;)output[index++]=f(f);console.log(output.join(','))``

ps与其他几个（`console.log`）JavaScript答案进行比较，它是70个字节
l4m2，18年

1

# Japt，25个字节

``````[]L³õ@pX%7«/7/tX ?X:UgV°

``````

### 开箱及其工作方式

``````[]L³õX{UpX%7&&!/7/tX ?X:UgV++

[]                            Assign empty array to U
L³õX{                       Map over 1 to 1,000,000 inclusive...
X%7&&                  If X is not divisible by 7 and
!/7/tX            X does not have a digit 7
?X:UgV++   then X, otherwise the next element already in U
Up                       Push it to the end of U

Implicit print U
``````