diff options
| author | Abigail <abigail@abigail.be> | 2021-11-23 16:00:43 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-11-25 17:19:51 +0100 |
| commit | fb39699b31d65405e7d104ee2b1c1c4edcdf51a8 (patch) | |
| tree | e33b7f66d49b274f4a8a322f2651d87d9d400b78 /challenge-140/abigail/scheme | |
| parent | fd4849655113ac9706753441de784780cc72c389 (diff) | |
| download | perlweeklychallenge-club-fb39699b31d65405e7d104ee2b1c1c4edcdf51a8.tar.gz perlweeklychallenge-club-fb39699b31d65405e7d104ee2b1c1c4edcdf51a8.tar.bz2 perlweeklychallenge-club-fb39699b31d65405e7d104ee2b1c1c4edcdf51a8.zip | |
Solutions for week 140, part 1.
Diffstat (limited to 'challenge-140/abigail/scheme')
| -rw-r--r-- | challenge-140/abigail/scheme/ch-1.scm | 41 | ||||
| -rw-r--r-- | challenge-140/abigail/scheme/ch-2.scm | 62 |
2 files changed, 103 insertions, 0 deletions
diff --git a/challenge-140/abigail/scheme/ch-1.scm b/challenge-140/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..45c7e0fc71 --- /dev/null +++ b/challenge-140/abigail/scheme/ch-1.scm @@ -0,0 +1,41 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm +;;; + +(use-modules (ice-9 rdelim)) +(use-modules (srfi srfi-1)) + +(define (bin2dec bin) + (define len (string-length bin)) + (cond ((= len 0) 0) + (else (+ (string->number (string-take-right bin 1)) + (* 2 (bin2dec (string-drop-right bin 1))))))) + +(define (_dec2bin dec) + (cond ((= dec 0) "") + (else (string-concatenate + (list (_dec2bin (floor-quotient dec 2)) + (number->string (modulo dec 2))))))) + +(define (dec2bin dec) + (cond ((= dec 0) "0") + (else (_dec2bin dec)))) + +(define (main) + (define line (read-line)) + (define parts) + (if (not (eof-object? line)) + (begin + (display (dec2bin (fold + 0 (map bin2dec (string-split line #\ ))))) + (newline) + (main) + ) + ) +) + + +(main) diff --git a/challenge-140/abigail/scheme/ch-2.scm b/challenge-140/abigail/scheme/ch-2.scm new file mode 100644 index 0000000000..c38d93c582 --- /dev/null +++ b/challenge-140/abigail/scheme/ch-2.scm @@ -0,0 +1,62 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-2.scm < input-file +;;; + +(use-modules (ice-9 rdelim)) +(use-modules (srfi srfi-1)) + +;; +;; Return the number of divisors of n, where the divisors don't +;; exceed neither i nor j +;; +(define (divisors n i j) + (define s (inexact->exact (round (sqrt n)))) + (define c 0) + (do ((d 1 (1+ d))) + ((> d s)) + (if (= (modulo n d) 0) + (begin + (if (and (<= d i) (<= (/ n d) j)) (set! c (+ c 1))) + (if (and (<= d j) (<= (/ n d) i)) (set! c (+ c 1))) + (if (= n (* d d)) (set! c (- c 1))) + ) + ) + ) + c +) + + + +(define (find-kth list) + (define i (list-ref list 0)) + (define j (list-ref list 1)) + (define k (list-ref list 2)) + (define n 0) + (while (> k 0) + (begin + (set! n (+ 1 n)) + (set! k (- k (divisors n i j))) + ) + ) + n +) + +(define (main) + (define line (read-line)) + (define parts) + (if (not (eof-object? line)) + (begin + (display (find-kth (map string->number (string-split line #\ )))) + (newline) + (main) + ) + ) +) + +;; (display (divisors 12 10 10))(newline) + +(main) |
