Beginner Algorithms in Ruby: Sum of Multiples of 3 and 5

  • If we list all of 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.
  • Find the sum of all the multiples of 3 or 5 below 1000.
/* given a limit, collect all the multiples of 3 or 5 up to 1 less than the limit, and return the sum of those multiples*/create a function to collect the multiples that takes a limit as an argument
define data-type/structure for the storing
create a loop for each non-zero number to 1 less than limit
test each one of those numbers against some mathematical evaluation, and if either comes back true, shovel that number into my defined data structure
return that data
create a function that uses the other function to return a sum, that also takes in the limit as an argument
define our sum and set a base, in this case 0
call our first function, and use its return to do some math
return that math
$ruby <file name>.rb
class name class attributes - need to read, not write   create objects asap when given a limit
upon creation, make the limit variable accessible
make our multiples immediately available too by calling a function that will return a data-type that is a collection of our multiples
function to return collection of multiple
make use of our limit, but make sure to go 1 less
do some math, evaluate, what returns true goes into our collection
return that collection
function to add up those multiples
do math and return the sum



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aaron feingold

aaron feingold

web developer making apps for people with appetite