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

12
10/euler10.rkt Normal file
View file

@ -0,0 +1,12 @@
#lang racket
(require math/number-theory)
(define (primes it limit prim)
(if (equal? it limit)
prim
(if (prime? it)
(primes (+ it 1) limit (cons it prim))
(primes (+ it 1) limit prim))))
(apply + (primes 1 2000000 (list)))

View file

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

4
16/euler16.rkt Normal file
View file

@ -0,0 +1,4 @@
#lang racket
(foldl + 0 (map (λ (x) (- (char->integer x) 48))
(string->list (number->string (expt 2 1000)))))

16
30/euler30.rkt Normal file
View file

@ -0,0 +1,16 @@
#lang racket
(define (findlimit it)
(if (> (expt 10 it) (* it (expt 9 5)))
it
(findlimit (+ it 1))))
(define (digits x)
(map (λ (x) (- (char->integer x) 48))
(string->list (number->string x))))
(apply +(filter (λ (x) (equal? x
(apply + (map (λ (x) (expt x 5)) (digits x)))))
(range 2 (expt 10 (findlimit 1)))))