added some solutions in racket, deuglyfied 14

This commit is contained in:
Lowl3v3l 2017-06-06 20:29:13 +02:00
parent 5f58ca8c87
commit a6770f6bca
4 changed files with 41 additions and 16 deletions

View file

@ -1,18 +1,11 @@
#lang racket
#lang typed/racket
(define (longest-collatz n)
(let
((pivot (list 0 0)))
(define (collatz lst)
(if (equal? (car lst) 1)
lst
(if (even? (car lst))
(collatz (cons (quotient (car lst) 2) lst))
(collatz (cons (+ 1 (* 3 (car lst))) lst)))))
(for ((i (range 1 n)))
(if (>= (length (collatz (list i))) (second pivot))
(set! pivot (list i (length (collatz (list i)))))
0))
pivot))
(: collatz (-> Nonnegative-Integer (Listof Nonnegative-Integer)))
(define (collatz x)
(cond
((equal? x 1) (list 1))
((even? x) (cons x (collatz (quotient x 2))))
((odd? x) (cons x (collatz (+ 1 (* 3 x)))))
(else (list))))
(display (first (longest-collatz 1000000)))
(argmax (λ ((x : Nonnegative-Integer)) (length (collatz x))) (range 1 1000000))