Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; Programming Praxis
- ;; http://programmingpraxis.com/2009/02/19/flavius-josephus
- ;; Flavius Josephus
- ;; Written in Scheme (Guile 2.0)
- ;; (josephus 41 3) ->
- ;; (2 5 8 11 14 17 20 23 26 29 32 35 38 0 4 9 13 18 22 27 31 36 40 6 12 19 25
- ;; 33 39 7 16 28 37 10 24 1 21 3 34 15 30)
- (define (josephus n m)
- (let loop ((s (apply circular-list (iota n)))
- (n n)
- (res '()))
- (if (= 1 n) (reverse (cons (car s) res))
- (let* ((x (drop s (- m 2))) ;; element preceding the element to remove
- (y (cadr x))) ;; element to remove
- (set-cdr! x (cddr x)) ;; remove element from circular list
- (loop (cdr x) (1- n) (cons y res))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement