40答案到達(全て無限リスト)です。
Falcon
Falconの関数はクロージャらしい。
checked using 0.9.6.8.
#!/usr/bin/env falcon
// http://nabetani.sakura.ne.jp/hena/ord24eliseq/
function isqrt(n)
if n<=0: return 0
if n<4: return 1
x = 0
y = n
while x!=y and x+1!=y
x = y
y = int((int(n/y)+y)/2)
end
return x
end
function icbrt(n)
if n<0: return -icbrt(-n)
if n==0: return 0
if n<8: return 1
x = 0
y = n
while x!=y and x+1!=y
x = y
y = int((int(int(n/y)/y)+y*2)/3)
end
return x
end
function generate()
i = 0
return {=>
i += 1
return i
}
end
function drop_prev(check, prev)
a = 0
b = prev()
return {=>
while true
a = b
b = prev()
if not check(b)
return a
end
end
}
end
function drop_next(check, prev)
first = true
a = 0
b = 0
return {=>
while true
a = b
b = prev()
if first or not check(a)
first = false
return b
end
end
}
end
function drop_n(check, n, prev)
i = 0
return {=>
while true
i += 1
a = prev()
if not check(i,n)
return a
end
end
}
end
function is_sq(n): return isqrt(n)**2 == n
function is_cb(n): return icbrt(n)**3 == n
function is_multiple(i,n): return i%n == 0
function is_le(i,n): return i<=n
f = [
"S" => {prev => drop_next(is_sq,prev)},
"s" => {prev => drop_prev(is_sq,prev)},
"C" => {prev => drop_next(is_cb,prev)},
"c" => {prev => drop_prev(is_cb,prev)},
"h" => {prev => drop_n(is_le,100,prev)}
]
for i in [2:10]
f[i.toString()] = {x => {prev => drop_n(is_multiple,x,prev)}}(i)
end
stdin=stdIn()
while true
s = stdin.grabLine()
if s==0: exit()
g = generate()
for c in s.split("")
g = f[c](g)
end
cnt = 0
ret = ""
while cnt<10
if cnt>0: ret+=","
ret += g()
cnt += 1
end
> ret
end
Scheme
そんでもって クロージャといえばSchemeなので手を付けておいた(while/ハッシュテーブル等、他のLisp系言語とは相容れない書き方な気がするがもはやご愛嬌)。
#!/usr/bin/env gosh
;http://nabetani.sakura.ne.jp/hena/ord24eliseq/
(define (mydiv x y)
(truncate (/ x y))
)
(define (isqrt_impl n x y)
(if (or (= x y) (= (+ x 1) y)) x
(isqrt_impl n y (mydiv (+ (mydiv n y) y) 2))
)
)
(define (isqrt n)
(if (<= n 0) 0
(if (< n 4) 1
(isqrt_impl n 0 n)
)
)
)
(define (icbrt_impl n x y)
(if (or (= x y) (= (+ x 1) y)) x
(icbrt_impl n y (mydiv (+ (mydiv (mydiv n y) y) (* y 2)) 3))
)
)
(define (icbrt n)
(if (< n 0) (- 0 (icbrt (- 0 n)))
(if (= n 0) 0
(if (< n 8) 1
(icbrt_impl n 0 n)
)
)
)
)
(define (generate)
(let ((i 0))
(lambda ()
(set! i (+ i 1))
i
)
)
)
(define (drop_prev check prev)
(let ((a 0) (b (prev)))
(lambda ()
(set! a b)
(set! b (prev))
(while (check b)
(set! a b)
(set! b (prev))
)
a
)
)
)
(define (drop_next check prev)
(let ((a 0) (b 0) (first #t))
(lambda ()
(set! a b)
(set! b (prev))
(while (and (not first) (check a))
(set! a b)
(set! b (prev))
)
(set! first #f)
b
)
)
)
(define (drop_n check n prev)
(let ((i 0) (a 0))
(lambda ()
(set! i (+ i 1))
(set! a (prev))
(while (check i n)
(set! i (+ i 1))
(set! a (prev))
)
a
)
)
)
(define (is_sq n)
(= (expt (isqrt n) 2) n)
)
(define (is_cb n)
(= (expt (icbrt n) 3) n)
)
(define (is_multiple i n)
(= (modulo i n) 0)
)
(define (is_le i n)
(<= i n)
)
(define f (make-hash-table))
(hash-table-put! f #\S (lambda (prev) (drop_next is_sq prev)))
(hash-table-put! f #\s (lambda (prev) (drop_prev is_sq prev)))
(hash-table-put! f #\C (lambda (prev) (drop_next is_cb prev)))
(hash-table-put! f #\c (lambda (prev) (drop_prev is_cb prev)))
(hash-table-put! f #\h (lambda (prev) (drop_n is_le 100 prev)))
(hash-table-put! f #\2 (lambda (prev) (drop_n is_multiple 2 prev)))
(hash-table-put! f #\3 (lambda (prev) (drop_n is_multiple 3 prev)))
(hash-table-put! f #\4 (lambda (prev) (drop_n is_multiple 4 prev)))
(hash-table-put! f #\5 (lambda (prev) (drop_n is_multiple 5 prev)))
(hash-table-put! f #\6 (lambda (prev) (drop_n is_multiple 6 prev)))
(hash-table-put! f #\7 (lambda (prev) (drop_n is_multiple 7 prev)))
(hash-table-put! f #\8 (lambda (prev) (drop_n is_multiple 8 prev)))
(hash-table-put! f #\9 (lambda (prev) (drop_n is_multiple 9 prev)))
(define s (read-line))
(while (not (eof-object? s))
(define g (fold (lambda (e g) ((hash-table-get f e) g)) (generate) (string->list s)))
(define i 0)
(while (< i 10)
(if (> i 0) (display ","))
(display (g))
(set! i (+ i 1))
)
(print)
(set! s (read-line))
)