1.el 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. ;;; package --- Summary
  2. ;;; Commentary:
  3. ;; If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
  4. ;; Find the sum of all the multiples of 3 or 5 below 1000.
  5. ;;; Code:
  6. ;; I could perhaps use a variable to be what numbers to divide by. That would mean that the program could change
  7. ;; with time. Perhaps I'll do that when I try to make this better
  8. ;;(defvar numbers '(3 5))
  9. (defun divisible-by-3 (number)
  10. "Is the current NUMBER divisible by 3."
  11. (if (eq (% number 3) 0)
  12. t
  13. nil))
  14. (defun divisible-by-5 (number)
  15. "Is the current NUMBER divisible by 3."
  16. (if (eq (% number 5) 0)
  17. t
  18. nil))
  19. (defvar number 1)
  20. (defvar sum 0)
  21. (while (< number 1000)
  22. ;;if the number is divisible by 5 or 3
  23. (when (or (divisible-by-3 number) (divisible-by-5 number))
  24. (setq sum (+ sum number)))
  25. (setq number (+ number 1)))
  26. (print number)
  27. (print sum)
  28. ;;(provide '1)
  29. ;;(load "~/programming/elisp/projecteuler/1.elc" )
  30. ;;; 1.el ends here