How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Emacs Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Emacs Lisp programming language

Table of Contents

Problem Statement

The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n. Show output for n = 1000. This is is the same as Project Euler problem 1. Extra credit: do this efficiently for n = 1e20 or higher.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Emacs Lisp programming language

Source code in the emacs programming language

(defun sum-3-5 (n)
  (let ((sum 0))
    (dotimes (x n)
      (when (or (zerop (% x 3)) (zerop (% x 5)))
        (setq sum (+ sum x))))
    sum))


(defun sum-3-5 (n)
  (apply #'+ (seq-filter
	      (lambda (x) (or (zerop (% x 3) ) (zerop (% x 5))))
	      (number-sequence 1 (- n 1)))))


  

You may also check:How to resolve the algorithm Loops/Break step by step in the Python programming language
You may also check:How to resolve the algorithm Prime decomposition step by step in the Ruby programming language
You may also check:How to resolve the algorithm Loops/While step by step in the jq programming language
You may also check:How to resolve the algorithm Chaocipher step by step in the Perl programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the PL/I programming language