Friday, August 21, 2009

Euler Project 47

The first two consecutive numbers to have two distinct prime factors are:

14 = 2 × 7
15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

644 = 2² × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19.

Find the first four consecutive integers to have four distinct primes factors. What is the first of these numbers?


My Solution

(defun factorlist(num)
  (loop for i from 2 to (floor (sqrt num))
          when(zerop (rem num i))
          collect i
          when(zerop (rem num i))
          collect (/ num i)))

(defun  primep (n)
        (cond ((minusp n) 'nil)
                  ((= 2 n) t)
                 ((evenp n) 'nil)
                 ((= 1 n) 'nil)
                 (t (let ((limit (floor (sqrt n))))
                      (do ((i 3 (incf i 2)))
                            ((> i limit) t)
                            (if (= 0 (mod n i)) (return-from primep 'nil)))))))

(defun FourePrimeFactors(l)
      (length (remove-if-not #'primep l)))

(defun Euler47(n)
  (loop for i from 100000 to n
         do(if (and (= 4 (FourePrimeFactors (factorlist i)))
                          (= 4 (FourePrimeFactors (factorlist (+ i 1))))
                          (= 4 (FourePrimeFactors (factorlist (+ i 2))))
                          (= 4 (FourePrimeFactors (factorlist (+ i 3)))))
     (return i))))


No comments:

Post a Comment