球拍-126字节降至98字节 91字节
一个非常幼稚的解决方案-也许可以通过一个不错的算法和一些我不知道的Lisp技巧将其削减很多
(define(g x[c 0][d 0][i 2])(cond[(= x 0)c][(= i x)(g d(+ 1 c))][(=(modulo x i)0)(g x c d(+ 1 i))][else(g x c(+ 1 d)(+ 1 i))]))
编辑:按要求解释。就像我说的那样,这是一个非常幼稚的递归解决方案,并且可能要短得多。
(define (g x [c 0] [d 0] [i 2]) ;g is the name of the function - arguments are x (input), c (counter for steps), d (non-divisor counter), i (iterator)
(cond
[(= x 0) c] ;once x gets to 0 c is outputted
[(= i x) (g d (+ 1 c))] ;if iterator reaches x then we recurse with d as input and add 1 to c
[(= (modulo x i) 0) (g x c d (+ 1 i))] ;checks if iterator is non divisor, then adds it to d and increments iterator
[else(g x c (+ 1 d) (+ 1 i))])) ;otherwise just increments iterator
编辑2:98字节的版本,使用的算法更少(虽然仍然很愚蠢,并且可以更短)
(define(g x)(if(< x 1)0(+ 1(g(length(filter(λ(y)(>(modulo x y)0))(cdr(build-list x values))))))))
说明:
(define (g x) ;function name g, input x
(if (< x 1)
0 ;returns 0 if x < 1 (base case)
(+ 1 ;simple recursion - adds 1 to output for each time we're looping
(g (length ;the input we're passing is the length of...
(filter (λ (y) (> (modulo x y) 0)) ;the list where all numbers which are 0 modulo x are 0 are filtered out from...
(cdr (build-list x values)))))))) ;the list of all integers up to x, not including 0
编辑3:通过替换(cdr(build-list x values))
为保存7个字节(build-list x add1)
(define(g x)(if(< x 1)0(+ 1(g(length(filter(λ(y)(>(modulo x y)0))(build-list x add1)))))))