Skip to content

Instantly share code, notes, and snippets.

@burhanloey
Last active July 16, 2017 15:57
Show Gist options
  • Save burhanloey/e4601a4e941d7dbf8474dd145a50db01 to your computer and use it in GitHub Desktop.
Save burhanloey/e4601a4e941d7dbf8474dd145a50db01 to your computer and use it in GitHub Desktop.
Problem 23: Brute force method (took around 80 seconds, < 40 seconds if limit set to 20161)
(ql:quickload :cl-arrows)
(ql:quickload :lparallel)
(defpackage :euler-playground
(:use :cl :lparallel :cl-arrows))
(in-package :euler-playground)
(defconstant +limit+ 28123)
(defvar *abundant-numbers* '())
(setf *kernel* (make-kernel 4))
(defun divisor (x)
(loop for i from 1 to x
for j = (/ x i)
while (<= i j)
when (integerp j)
collect i into result
and collect j into result
finally
(return (remove x (remove-duplicates result)))))
(defun sum (list-of-num)
(reduce #'+ list-of-num))
(defun abundantp (x)
(if *abundant-numbers*
(member x *abundant-numbers*)
(> (sum (divisor x)) x)))
(defun find-abundant-numbers ()
(setf *abundant-numbers* (loop for i from 1 to +limit+
when (abundantp i)
collect i)))
(defun sum-of-two-abundant-p (x)
(let* ((abundant-numbers (if *abundant-numbers*
*abundant-numbers*
(find-abundant-numbers)))
(abundant-pair (dolist (num abundant-numbers)
(when (and (> x num)
(abundantp (- x num)))
(return x)))))
(when (not abundant-pair)
x)))
(defun solve ()
(let ((candidates (loop for i from 1 to +limit+
collect i)))
(->> candidates
(pmapcar #'sum-of-two-abundant-p)
(premove nil)
(preduce #'+))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment