added some solutions in racket, deuglyfied 14
This commit is contained in:
parent
5f58ca8c87
commit
a6770f6bca
4 changed files with 41 additions and 16 deletions
12
10/euler10.rkt
Normal file
12
10/euler10.rkt
Normal 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)))
|
|
@ -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))
|
||||
|
|
4
16/euler16.rkt
Normal file
4
16/euler16.rkt
Normal 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
16
30/euler30.rkt
Normal 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)))))
|
Reference in a new issue