どう書く
yhpg
icon

多段階選抜 (Icon)

多段階選抜
解答日
シリーズ:yieldの練習/ジェネレータを入れ子に/整数平方根・立方根の実装

問題
 

http://nabetani.sakura.ne.jp/hena/ord24eliseq/
https://qiita.com/Nabetani/items/1c83005a854d2c6cbb69

Ruby
2014/8/2(当日)
https://qiita.com/cielavenir/items/9f15e29b73ecf98968a5

C#/Python
2014/8/4
https://qiita.com/cielavenir/items/a1156e6a4f71ddbe5dcb

 
ここから上はdrop_prev_square/drop_prev_cubicをまとめる前の答案

Go/C#/Ruby/Python
2014/8/5
https://qiita.com/cielavenir/items/2a685d3080862f2c2c47

PHP/JavaScript
2014/9/9
https://qiita.com/cielavenir/items/28d613ac3823afbf8407

VB
2014/9/10
https://qiita.com/cielavenir/items/cb7266abd30eadd71c04

D
2015/12/21
https://qiita.com/cielavenir/items/47c9e50ee60bef2847ec

Perl
2017/3/10
https://qiita.com/cielavenir/items/6dfbff749d833c0fd423

Lua
2017/3/13
https://qiita.com/cielavenir/items/c60fe7e8da73487ba062

C++20(TS)
2017/3/15

https://qiita.com/cielavenir/items/e1129ca185008f49cbab (MSVC)
https://qiita.com/cielavenir/items/1cfa90d73d11bb7dc3d4 (clang)

F#
2017/3/17
https://qiita.com/cielavenir/items/a698d6a26824ff53de81

Boo/Nemerle
2017/5/13
https://qiita.com/cielavenir/items/e2a783f0fe4b0fe0ed48

Perl6
2017/5/15
https://qiita.com/cielavenir/items/656ea17fa96c865c4498

Kotlin
2017/5/25
https://qiita.com/cielavenir/items/9c46ce8d9d12e51de285

Crystal
2018/5/8
https://qiita.com/cielavenir/items/1815bfa6a860fd1f90db

MoonScript
2018/6/16
https://qiita.com/cielavenir/items/8b03cce0386f4537b5ad

Julia/Rust
2018/12/20
https://qiita.com/cielavenir/items/3ddf72b06d625da0c4a5

Nim
2018/12/26
https://qiita.com/cielavenir/items/5728944867e609fd52a7

Tcl
2018/12/31
https://qiita.com/cielavenir/items/76cbd9c2022b48c9a2c9

Pascal/Cobra
2019/1/16
https://qiita.com/cielavenir/items/81b81baf8dfc1f877903

Icon
2019/1/17
https://qiita.com/cielavenir/items/889622dcc721f5a4da24

(icbrtの実装に関する)補題
2017/5/11
整数除算であってもn/(x*y)はn/x/yに等しい(ことの証明)
https://qiita.com/cielavenir/items/21a6711afd6be8c18c55

25言語到達 (すべて無限リストです)

# ええ。特に不要なCobra答案を作ったのはIconを25言語目にするためでした。

処理系 http://www2.cs.arizona.edu/icon/ (Homebrew版はうまく動かないようです)

古くからある言語でconcurrency使えるのはすごいと思うけど、無名関数がなさそう。つらすぎます…

#!/usr/bin/env icon

# http://qiita.com/Nabetani/items/1c83005a854d2c6cbb69
# http://nabetani.sakura.ne.jp/hena/ord24eliseq/

procedure isqrt(n)
local x,y
if n<=0 then
return 0
if n<4 then
return 1
x:=0
y:=n
while x~=y & x+1~=y do {
x:=y
y:=(n/y+y)/2
}
return x
end

procedure icbrt(n)
local x,y
if n<0 then
return icbrt(-n)
if n==0 then
return 0
if n<8 then
return 1
x:=0
y:=n
while x~=y & x+1~=y do {
x:=y
y:=(n/y/y+2*y)/3
}
return x
end

procedure is_sq(n)
local x
x:=isqrt(n)
return x*x==n
end

procedure is_cb(n)
local x
x:=icbrt(n)
return x*x*x==n
end

procedure is_multiple(i,n)
return i%n==0
end

procedure is_le(i,n)
return i<=n
end

procedure generate()
local i
i:=1
repeat {
suspend i
i:=i+1
}
end

procedure drop_prev(check,prev)
local a,b
a:=@prev
b:=@prev
repeat {
if not check(b) then
suspend a
a:=b
b:=@prev
}
end

procedure drop_next(check,prev)
local a,b
a:=@prev
b:=@prev
suspend a
repeat {
if not check(a) then
suspend b
a:=b
b:=@prev
}
end

procedure drop_n(check,n,prev)
local i,a
i:=0
repeat {
i:=i+1
a:=@prev
if not check(i,n) then
suspend a
}
end

procedure fS(e)
return create drop_next(is_sq,e)
end
procedure fs(e)
return create drop_prev(is_sq,e)
end
procedure fC(e)
return create drop_next(is_cb,e)
end
procedure fc(e)
return create drop_prev(is_cb,e)
end
procedure fh(e)
return create drop_n(is_le,100,e)
end
procedure f2(e)
return create drop_n(is_multiple,2,e)
end
procedure f3(e)
return create drop_n(is_multiple,3,e)
end
procedure f4(e)
return create drop_n(is_multiple,4,e)
end
procedure f5(e)
return create drop_n(is_multiple,5,e)
end
procedure f6(e)
return create drop_n(is_multiple,6,e)
end
procedure f7(e)
return create drop_n(is_multiple,7,e)
end
procedure f8(e)
return create drop_n(is_multiple,8,e)
end
procedure f9(e)
return create drop_n(is_multiple,9,e)
end

procedure main()
local f,s,g,i,z
f:=table()
f["S"]:=fS
f["s"]:=fs
f["C"]:=fC
f["c"]:=fc
f["h"]:=fh
f["2"]:=f2
f["3"]:=f3
f["4"]:=f4
f["5"]:=f5
f["6"]:=f6
f["7"]:=f7
f["8"]:=f8
f["9"]:=f9
repeat {
if s:=read() then {
g:=create generate()
every i:=1 to *s do {
g:=f[s[i]](g)
}
every i:=0 to 9 do {
if i>0 then
writes(',')
z:=@g
writes(z)
}
write('')
} else {
break
}
}
end