15,942,964 members
0.00/5 (No votes)
See more:
Python
```n1 = 1
n2 = 2
sum = 0
for i in range(1,4000000):
nth = n1 + n2
n1 = n2
n2 = nth
if n1%2 == 0:
sum += n1
elif n2%2 == 0:
sum += n2
print("the even terms sum is",sum)```

this is my approach. I use pycharm but in pycharm the run window prints nothing.I used an online ide and I was getting this:
"Time Limit Exceeded"

I am a beginner so please explain it in a little detail

What I have tried:

```n1 = 1
n2 = 2
sum = 0
for i in range(1,4000000):
nth = n1 + n2
n1 = n2
n2 = nth
if n1%2 == 0:
sum += n1
elif n2%2 == 0:
sum += n2
print("the even terms sum is",sum)```
Posted
Updated 2-Jul-19 10:05am

## Solution 3

I'll give you a quick 2nd answer to your follow-up comment.

The steps you want to take are:
1. move for loop into a function that takes min and max loop values
2. return the sum
3. call the function in an outer loop
4. each time the outer loop runs, print the sum value so you can see process running (with output) even though it will take a long time for the entire program to run.

I'm no Python wiz and this is untested code, but it'll look something like the following:
Loop will run 1-10, 11-20, 21-30, etc. to max (1000)
Python
```loopMultiplier = 0;
localSum = 0;
// this is the outer loop that calls the function doFib() multiple times
// each time thru the outer loop you will get a print so you can see it running
for x in range (1, 1000)
localSum += doFib((loopMultiplier *x) +1, loopMultiplier * x)
loopMultiplier += 10
print("the even terms sum is ",localSum)

def doFib(minVal, maxVal) :
n1 = 1
n2 = 2
sum = 0
for i in range(minVal,maxVal):
nth = n1 + n2
n1 = n2
n2 = nth
if n1%2 == 0:
sum += n1
elif n2%2 == 0:
sum += n2
return sum```

## Solution 1

It's doesn't do any printing until the for loop is done.
The print statement only runs after the for loop has iterated through 4 million times.

You are getting the
`"Time Limit Exceeded"`
message because it's taking a long time.

Have you tried a smaller number in the for loop to see if you get something?

Try:
Python
`for i in range(1,200):`

Comments
Member 14517556 1-Jul-19 23:50pm
Yeah! It worked but can we not use such big numbers. Is there any other method to do so. If you know one please share. Thank you for your answer

## Solution 2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Pete O'Hanlon 10 Greg Utas 10 Member 7658329 5
 Pete O'Hanlon 865 OriginalGriff 800 Richard Deeming 715 merano99 345 Dave Kreskowiak 305

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900