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

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

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center