Hey SteemIt community. Did you heard about Project Euler? If you don't know please review this website: Project Euler
What is Project Euler
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.
The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.
Problem 1
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. Find the sum of all the multiples of 3 or 5 below 1000.
Steps
- Declare empty array
- Select natural numbers below 1000 in for loop
- If number divisible without remainder with 3 or 5 add to array
- Print sum of values in array
Solution (Python)
def find_sum(number)
numbers = []
for i in range(1,number):
if i%3 == 0 or i%5 == 0:
numbers.append(i)
print(sum(numbers))
find_sum(1000)
Solution (Javascript)
function findSum(number) {
numbers = [];
for (i = 1; i < number; i++) {
if (i % 3 == 0 || i % 5 == 0) {
numbers.push(i);
}
}
console.log(numbers.reduce((a, b) => a + b, 0))
}
findSum(1000)
Result: 233168
Nice, I love these little coding challenges.
Here's a nice purely functional JavaScript solution to your problem:
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for functional JS solution.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Cool that you are doing it with the new destructuring syntax. You created an instance of Array and applied the filter method.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Yeah I'm a big fan of some of those new ES6 features. It's so easy to turn all kinds of iterables into arrays and approaching them functionally.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit