Project Euler Problem 2 Solution
Project Euler Problem 2 Solution
result = 0 # Variable holding the result
list = [1,2] # List to keep the necessary numbers
i = 2 # Counter
while list[i-1] < 4000000: # A while loop that runs if the last element in the list is less than 4,000,000
list.append(list[i-2] + list[i-1]) # We add the sum of the 2nd from last and last elements in the list.
i += 1 # We increment the counter
for i in list: # We iterate through the list
if i % 2 == 0: # We find the even elements
result += i # We add the even elements to the result
print(result) # We print the result to the screen
In our code, we first created a list and obtained the necessary numbers by adding to this list.
After obtaining the numbers we needed, we traversed the list, found the even numbers, and summed them up.
We then printed the summed numbers to the screen with the print method.
To see the correct answer, click here.

Yorum Gönder