Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- inline unsigned int step(unsigned int i)
- {
- return (i&0x1)*(i*3+1)+((i+1)&0x1)*(i>>1);
- }
- unsigned int collatz(unsigned int i)
- {
- unsigned int cur = i;
- unsigned steps = 0;
- while((cur=step(cur))!=1) steps++;
- return steps;
- }
- (define (listfrom a b)
- (if (= a b)
- (cons a empty)
- (cons a (listfrom (+ 1 a) b))))
- (define (max-collatz a b)
- (foldl max 0 (map collatz-loop (listfrom a b))))
- (define (max-collatz-num a b)
- (foldl (lambda (c r)
- (if (> (collatz-loop c) (collatz-loop r)) c r))
- a
- (listfrom a b)))
- (define (collatz x)
- (if (even? x)
- (/ x 2)
- (+ (* x 3) 1)))
- (define (collatz-loop x)
- (if (= x 1) 1
- (+ (collatz-loop (collatz x)) 1)))
- (define (collatz-loop x)
- (define (internal x counter)
- (if (= x 1) counter
- (internal (collatz x) (+ counter 1))))
- (internal x 1))
- (define (collatz-range a b)
- (if (= a b)
- (cons a (collatz-loop a))
- (let ((this (collatz-loop a))
- (rest (collatz-range (+ a 1) b)))
- (if (< this (cdr rest)) rest
- (cons a this)))))
- (collatz-range 1 20) ; returns (18 . 21), which means that (collatz-loop 18) returns 21
- (define (collatz-range a b)
- (define (internal a best)
- (if (< b a) best
- (internal (+ a 1)
- (let ((x (collatz-loop a)))
- (if (< x (cdr best))
- best
- (cons a x))))))
- (internal a (cons -1 -1)))
Add Comment
Please, Sign In to add comment