top of page

A bit of Python for Project Euler

Nov 2

1 min read

1

14

0



Project Euler is a series of mathematical questions that need the accompaniment of computer programming to solve.


It is permissible to share the solution to the first 100 questions but none thereafter.


The first question asks for the sum of the numbers which are divisible by 3 or 5 under 1000.

The code solution is here (in python):

list_x = list(range(1,1000))
list_new = []

#print(list_x)

for integer in list_x:

    if integer % 5 == 0 or integer % 3 == 0:

        list_new.append(integer)

print(list_new)

answer = sum(list_new)

print(answer)

Just a simple for loop, with two modulus conditions and a summation is all it takes to complete the first question.




The second question asks to find the sum of the even numbers in the Fibonacci sequence under the value of 4 million.


The code solution is here (in python):


list_x = []
pre_1 = 0
pre_2 = 1
i = 0
fnx = 1
goal = 4000000
while fnx < goal:
    fnx = pre_1 + pre_2
    pre_1 = pre_2
    pre_2 = fnx
    if fnx % 2 == 0:
        list_x.append(fnx)

y = sum(list_x)

print(y)

We run a while loop to generate the Fibonacci sequence, write the even numbers to a list and compute the sum of those numbers.

Nov 2

1 min read

1

14

0

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page