42598a2717eb

Problem 52
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 27 Feb 2017 01:34:12 +0000
parents 82da42a326e4
children cb5c5132c0b8
branches/tags (none)
files src/euler.lisp

Changes

diff -r 82da42a326e4 -r 42598a2717eb src/euler.lisp
--- a/src/euler.lisp	Mon Feb 27 01:22:47 2017 +0000
+++ b/src/euler.lisp	Mon Feb 27 01:34:12 2017 +0000
@@ -326,6 +326,14 @@
   (1+ (- (char-code (char-upcase char)) (char-code #\A))))
 
 
+(defun set-equal (list1 list2 &rest args)
+  (null (apply #'set-exclusive-or list1 list2 args)))
+
+(defun orderless-equal (list1 list2 &key (sort-predicate #'<))
+  (equal (sort (copy-seq list1) sort-predicate)
+         (sort (copy-seq list2) sort-predicate)))
+
+
 ;;;; Problems -----------------------------------------------------------------
 (defun problem-1 ()
   ;; If we list all the natural numbers below 10 that are multiples of 3 or 5,
@@ -1428,6 +1436,19 @@
 
 
 
+(defun problem-52 ()
+  ;; It can be seen that the number, 125874, and its double, 251748, contain
+  ;; exactly the same digits, but in a different order.
+  ;;
+  ;; Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x,
+  ;; contain the same digits.
+  (iterate (for i :from 1)
+           (for digits = (digits i))
+           (finding i :such-that
+                    (every (lambda (n)
+                             (orderless-equal digits (digits (* n i))))
+                           '(2 3 4 5 6)))))
+
 (defun problem-56 ()
   ;; A googol (10^100) is a massive number: one followed by one-hundred zeros;
   ;; 100^100 is almost unimaginably large: one followed by two-hundred zeros.
@@ -1530,6 +1551,7 @@
 (test p47 (is (= 134043 (problem-47))))
 (test p48 (is (= 9110846700 (problem-48))))
 
+(test p52 (is (= 142857 (problem-52))))
 (test p56 (is (= 972 (problem-56))))
 (test p74 (is (= 402 (problem-74))))