Project Euler in R Problem 1
A recently discovered hobby (read: addiction) of mine is Project Euler. More details can be found at http://www.projecteuler.net but in short it is a collection of mathematical puzzles and challenges that are meant to be solved via computer programming. As an aspiring R coder I jumped on the opportunity to sharpen my programming skills and put my math education to use.
Problem 1 is to find the sum of all multiples of 3 or 5 that are below 1000.
To do it the short way, I simply made a sequence from 1 to 999 and picked all numbers that are 0 (mod 3 or 5). I then placed these values into another variable and summed them up.
1 2 3
Since I don’t really have much coding experience, I decided to attempt this problem in a slightly more complicated way to practice for loops. This script places all multiples of 3 and 5 into two separate numeric vectors that are 999 in length with 0’s in the slots that aren’t multiples. I then took the union of the two sets to avoid overcounting numbers that are multiples of both 3 AND 5, and summed it up. Same answer, but slightly slower. Oh well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22