Project Euler Problems for Learn Programming - Solution 1 (Python & Javascript)

in #javascript8 years ago (edited)

shutterstock-programming.jpg

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

  1. Declare empty array
  2. Select natural numbers below 1000 in for loop
  3. If number divisible without remainder with 3 or 5 add to array
  4. 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

Project Euler - Solution 2

Sort:  

Nice, I love these little coding challenges.

Here's a nice purely functional JavaScript solution to your problem:

[...new Array(1000).keys()].filter(num => {
    return  num % === 0 || num % 5 === 0;
}).reduce((all, current) => all + current):

Thanks for functional JS solution.

Cool that you are doing it with the new destructuring syntax. You created an instance of Array and applied the filter method.

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.

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.036
BTC 107398.10
ETH 4387.14
USDT 1.00
SBD 0.83