829e38d1f825

Fuck a `LOOP`
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 10 Feb 2017 21:11:38 +0000
parents a66997c0fad3
children 32aa6dc56935
branches/tags (none)
files src/euler.lisp src/primes.lisp vendor/make-quickutils.lisp vendor/quickutils.lisp

Changes

--- a/src/euler.lisp	Fri Feb 10 20:26:42 2017 +0000
+++ b/src/euler.lisp	Fri Feb 10 21:11:38 2017 +0000
@@ -10,7 +10,7 @@
   (let ((s (format nil "~D" n)))
     (string= s (reverse s))))
 
-(defun palindrome-p (n)
+(defun palindromep (n)
   "Return whether `n` is a palindrome (in base 10)."
   (assert (>= n 0) (n) "~A must be a non-negative integer" n)
   ;; All even-length base-10 palindromes are divisible by 11, so we can shortcut
@@ -26,20 +26,9 @@
           (not (dividesp n 11))) nil)
     (t (definitely-palindrome-p n))))
 
-(defun range (from below)
-  (loop :for i :from from :below below
-        :collect i))
-
-(defun square (n)
-  (* n n))
-
-
-(defun random-exclusive (min max)
-  "Return an integer in the range (`min`, `max`)."
-  (+ 1 min (random (- max min 1))))
-(defun dividesp (n divisor)
-  "Return whether `n` is evenly divisible by `divisor`."
-  (zerop (mod n divisor)))
+(defun sum (sequence)
+  (iterate (for n :in-whatever sequence)
+           (sum n)))
 
 
 ;;;; Problems -----------------------------------------------------------------
@@ -48,10 +37,10 @@
   ;; we get 3, 5, 6 and 9. The sum of these multiples is 23.
   ;;
   ;; Find the sum of all the multiples of 3 or 5 below 1000.
-  (loop :for i :from 1 :below 1000
-        :when (or (dividesp i 3)
-                  (dividesp i 5))
-        :sum i))
+  (iterate (for i :from 1 :below 1000)
+           (when (or (dividesp i 3)
+                     (dividesp i 5))
+             (sum i))))
 
 (defun problem-2 ()
   ;; Each new term in the Fibonacci sequence is generated by adding the previous
@@ -61,12 +50,13 @@
   ;;
   ;; By considering the terms in the Fibonacci sequence whose values do not
   ;; exceed four million, find the sum of the even-valued terms.
-  (loop :with p = 0
-        :with n = 1
-        :while (<= n 4000000)
-        :when (evenp n) :sum n
-        :do (psetf p n
-                   n (+ p n))))
+  (iterate (with a = 0)
+           (with b = 1)
+           (while (<= b 4000000))
+           (when (evenp b)
+             (sum b))
+           (psetf a b
+                  b (+ a b))))
 
 (defun problem-3 ()
   ;; The prime factors of 13195 are 5, 7, 13 and 29.
@@ -79,13 +69,11 @@
   ;; from the product of two 2-digit numbers is 9009 = 91 × 99.
   ;;
   ;; Find the largest palindrome made from the product of two 3-digit numbers.
-  (let ((result (list)))
-    (loop :for i :from 0 :to 999
-          :do (loop :for j :from 0 :to 999
-                    :for product = (* i j)
-                    :when (palindrome-p product)
-                    :do (push product result)))
-    (apply #'max result)))
+  (iterate (for-nested ((i :from 0 :to 999)
+                        (j :from 0 :to 999)))
+           (for product = (* i j))
+           (when (palindromep product)
+             (maximize product))))
 
 (defun problem-5 ()
   ;; 2520 is the smallest number that can be divided by each of the numbers from
@@ -93,7 +81,7 @@
   ;;
   ;; What is the smallest positive number that is evenly divisible by all of the
   ;; numbers from 1 to 20?
-  (let ((divisors (range 11 21)))
+  (iterate
     ;; all numbers are divisible by 1 and we can skip checking everything <= 10
     ;; because:
     ;;
@@ -106,18 +94,17 @@
     ;; anything divisible by 16 is automatically divisible by 8
     ;; anything divisible by 18 is automatically divisible by 9
     ;; anything divisible by 20 is automatically divisible by 10
-    (loop :for i
-          :from 20 :by 20 ; it must be divisible by 20
-          :when (every (lambda (n) (dividesp i n))
-                       divisors)
-          :return i)))
+    (with divisors = (range 11 21))
+    (for i :from 20 :by 20) ; it must be divisible by 20
+    (finding i :such-that (every (lambda (n) (dividesp i n))
+                                 divisors))))
 
 (defun problem-6 ()
   ;; The sum of the squares of the first ten natural numbers is,
-  ;;   1^2 + 2^2 + ... + 10^2 = 385
+  ;;   1² + 2² + ... + 10² = 385
   ;;
   ;; The square of the sum of the first ten natural numbers is,
-  ;;   (1 + 2 + ... + 10)^2 = 55^2 = 3025
+  ;;   (1 + 2 + ... + 10)² = 55² = 3025
   ;;
   ;; Hence the difference between the sum of the squares of the first ten
   ;; natural numbers and the square of the sum is 3025 − 385 = 2640.
@@ -125,37 +112,55 @@
   ;; Find the difference between the sum of the squares of the first one hundred
   ;; natural numbers and the square of the sum.
   (flet ((sum-of-squares (to)
-           (loop :for i :from 1 :to to
-                 :sum (square i)))
+           (sum (range 1 (1+ to) :key #'square)))
          (square-of-sum (to)
-           (square (loop :for i :from 1 :to to
-                         :sum i))))
+           (square (sum (range 1 (1+ to))))))
     (abs (- (sum-of-squares 100) ; apparently it wants the absolute value
             (square-of-sum 100)))))
 
 (defun problem-7 ()
+  ;; By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see
+  ;; that the 6th prime is 13.
+  ;;
+  ;; What is the 10 001st prime number?
   (nth-prime 10001))
 
 (defun problem-8 ()
- (let ((digits (map 'list #'digit-char-p
-                    "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450")))
-   (loop :for window :in (n-grams 13 digits)
-         :maximize (apply #'* window))))
+  ;; The four adjacent digits in the 1000-digit number that have the greatest
+  ;; product are 9 × 9 × 8 × 9 = 5832.
+  ;;
+  ;; Find the thirteen adjacent digits in the 1000-digit number that have the
+  ;; greatest product. What is the value of this product?
+  (let ((digits (map 'list #'digit-char-p
+                     "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450")))
+    (iterate (for window :in (n-grams 13 digits))
+             (maximize (apply #'* window)))))
 
 (defun problem-9 ()
+  ;; A Pythagorean triplet is a set of three natural numbers, a < b < c, for
+  ;; which:
+  ;;
+  ;;   a² + b² = c²
+  ;;
+  ;; For example, 3² + 4² = 9 + 16 = 25 = 5².
+  ;;
+  ;; There exists exactly one Pythagorean triplet for which a + b + c = 1000.
+  ;; Find the product abc.
   (flet ((pythagorean-triplet-p (a b c)
            (= (+ (square a) (square b))
               (square c))))
-    (block search
-      (loop :for c :from 998 :downto 1 ; they must add up to 1000, so C can be at most 998
-            :do (loop :for a :from (- 999 c) :downto 1 ; A can be at most 999 - C (to leave 1 for B)
-                      :for b = (- 1000 c a)
-                      :when (pythagorean-triplet-p a b c)
-                      :do (return-from search (* a b c)))))))
+    ;; They must add up to 1000, so C can be at most 998.
+    ;; A can be at most 999 - C (to leave 1 for B).
+    (iterate (for c :from 998 :downto 1)
+             (iterate (for a :from (- 999 c) :downto 1)
+                      (for b = (- 1000 c a))
+                      (when (pythagorean-triplet-p a b c)
+                        (return-from problem-9 (* a b c)))))))
 
 (defun problem-10 ()
-  (loop :for p :in (primes-below 2000000)
-        :sum p))
+  ;; The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
+  ;; Find the sum of all the primes below two million.
+  (sum (primes-below 2000000)))
 
 
 ;;;; Tests --------------------------------------------------------------------
--- a/src/primes.lisp	Fri Feb 10 20:26:42 2017 +0000
+++ b/src/primes.lisp	Fri Feb 10 21:11:38 2017 +0000
@@ -71,7 +71,7 @@
   (flet ((fermat-check (a)
            (= (expmod a n n) a)))
     (loop :repeat tests
-          :when (not (fermat-check (random-exclusive 0 n)))
+          :when (not (fermat-check (random-range-exclusive 0 n)))
           :do (return nil)
           :finally (return t))))
 
@@ -112,7 +112,7 @@
                               :when (= y (1- n))
                               :do (return t))))))
            (loop :repeat k
-                 :for a = (random-exclusive 1 (1- n))
+                 :for a = (random-range-exclusive 1 (1- n))
                  :always (strong-liar-p a)))))))
 
 (defun brute-force-prime-p (n)
--- a/vendor/make-quickutils.lisp	Fri Feb 10 20:26:42 2017 +0000
+++ b/vendor/make-quickutils.lisp	Fri Feb 10 21:11:38 2017 +0000
@@ -5,10 +5,11 @@
   :utilities '(
 
                :define-constant
+               :ensure-boolean
+               :n-grams
+               :range
                :switch
-               :ensure-boolean
                :with-gensyms
-               :n-grams
 
                )
   :package "EULER.QUICKUTILS")
--- a/vendor/quickutils.lisp	Fri Feb 10 20:26:42 2017 +0000
+++ b/vendor/quickutils.lisp	Fri Feb 10 21:11:38 2017 +0000
@@ -2,7 +2,7 @@
 ;;;; See http://quickutil.org for details.
 
 ;;;; To regenerate:
-;;;; (qtlc:save-utils-as "quickutils.lisp" :utilities '(:DEFINE-CONSTANT :SWITCH :ENSURE-BOOLEAN :WITH-GENSYMS :N-GRAMS) :ensure-package T :package "EULER.QUICKUTILS")
+;;;; (qtlc:save-utils-as "quickutils.lisp" :utilities '(:DEFINE-CONSTANT :ENSURE-BOOLEAN :N-GRAMS :RANGE :SWITCH :WITH-GENSYMS) :ensure-package T :package "EULER.QUICKUTILS")
 
 (eval-when (:compile-toplevel :load-toplevel :execute)
   (unless (find-package "EULER.QUICKUTILS")
@@ -13,9 +13,10 @@
 (in-package "EULER.QUICKUTILS")
 
 (when (boundp '*utilities*)
-  (setf *utilities* (union *utilities* '(:DEFINE-CONSTANT :STRING-DESIGNATOR
+  (setf *utilities* (union *utilities* '(:DEFINE-CONSTANT :ENSURE-BOOLEAN :TAKE
+                                         :N-GRAMS :RANGE :STRING-DESIGNATOR
                                          :WITH-GENSYMS :EXTRACT-FUNCTION-NAME
-                                         :SWITCH :ENSURE-BOOLEAN :TAKE :N-GRAMS))))
+                                         :SWITCH))))
 
   (defun %reevaluate-constant (name value test)
     (if (not (boundp name))
@@ -54,6 +55,40 @@
        ,@(when documentation `(,documentation))))
   
 
+  (defun ensure-boolean (x)
+    "Convert `x` into a Boolean value."
+    (and x t))
+  
+
+  (defun take (n sequence)
+    "Take the first `n` elements from `sequence`."
+    (subseq sequence 0 n))
+  
+
+  (defun n-grams (n sequence)
+    "Find all `n`-grams of the sequence `sequence`."
+    (assert (and (plusp n)
+                 (<= n (length sequence))))
+    
+    (etypecase sequence
+      ;; Lists
+      (list (loop :repeat (1+ (- (length sequence) n))
+                  :for seq :on sequence
+                  :collect (take n seq)))
+      
+      ;; General sequences
+      (sequence (loop :for i :to (- (length sequence) n)
+                      :collect (subseq sequence i (+ i n))))))
+  
+
+  (defun range (start end &key (step 1) (key 'identity))
+    "Return the list of numbers `n` such that `start <= n < end` and
+`n = start + k*step` for suitable integers `k`. If a function `key` is
+provided, then apply it to each number."
+    (assert (<= start end))
+    (loop :for i :from start :below end :by step :collecting (funcall key i)))
+  
+
   (deftype string-designator ()
     "A string designator type. A string designator is either a string, a symbol,
 or a character."
@@ -147,34 +182,8 @@
     "Like `switch`, but signals a continuable error if no key matches."
     (generate-switch-body whole object clauses test key '(cerror "Return NIL from CSWITCH.")))
   
-
-  (defun ensure-boolean (x)
-    "Convert `x` into a Boolean value."
-    (and x t))
-  
-
-  (defun take (n sequence)
-    "Take the first `n` elements from `sequence`."
-    (subseq sequence 0 n))
-  
-
-  (defun n-grams (n sequence)
-    "Find all `n`-grams of the sequence `sequence`."
-    (assert (and (plusp n)
-                 (<= n (length sequence))))
-    
-    (etypecase sequence
-      ;; Lists
-      (list (loop :repeat (1+ (- (length sequence) n))
-                  :for seq :on sequence
-                  :collect (take n seq)))
-      
-      ;; General sequences
-      (sequence (loop :for i :to (- (length sequence) n)
-                      :collect (subseq sequence i (+ i n))))))
-  
 (eval-when (:compile-toplevel :load-toplevel :execute)
-  (export '(define-constant switch eswitch cswitch ensure-boolean with-gensyms
-            with-unique-names n-grams)))
+  (export '(define-constant ensure-boolean n-grams range switch eswitch cswitch
+            with-gensyms with-unique-names)))
 
 ;;;; END OF quickutils.lisp ;;;;