186 #4824(2007/12/15 07:10 GMT) [ Scheme ] Rating1/1=1.00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
(use srfi-1) (use util.combinations) (define (maho n) (define (maho-in m l) (if (= m 0) 1 (letrec ((cn (/ (* n (+ (* n n) 1)) 2)) (mylist (map (lambda (li) (cons (car l) li)) (filter (lambda (li) (= (apply + li) (- cn (car l)))) (combinations (cdr l) (- n 1)))))) (apply + (map (lambda (a) (maho-in (- m n) (lset-difference equal? l a))) mylist))))) (maho-in (* n n) (iota (* n n) 1))) (define (maho-by-enm n) (define (center n) (/ (* n (+ (* n n) 1)) 2)) (define (flatten2 l c) (define (flatten1 l c) (if (null? l) c (cons (car l) (flatten1 (cdr l) c)))) (if (null? l) c (flatten2 (cdr l) (flatten1 (car l) c)))) (define (my-equal? l1 l2) (null? (lset-xor eq? (flatten2 l1 '()) l2))) (define (enm-n n) (filter (lambda (l) (= (apply + l) (center n))) (combinations (iota (* n n) 1) n))) (filter (lambda (l) (my-equal? l (iota (* n n) 1))) (combinations (enm-n n) n)))
Rating1/1=1.00-0+
2 replies [ reply ]
186 #4824() [ Scheme ] Rating1/1=1.00
;Pen4 3GHzで
;(time (maho 4)) => 392
;real 0.109/user 0.109/sys 0.000
;(time (maho 5)) => 3245664
;real 1528.250/user 1423.391/sys 104.187
;(time (maho-by-enm 4)) => 392
;real 254.094/user 249.000/sys 1.531
maho-by-enmは#4819や#4821と同じ方法です
Rating1/1=1.00-0+
2 replies [ reply ]