Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Finding the sum of even Fibonacci numbers less than or equal to given limit

# Find the sum of all the even-valued  
# terms in the Fibonacci sequence which  
# do not exceed given limit. 
  
# Returns sum of even Fibonacci numbers which 
# are less than or equal to given limit. 
def evenFibSum(limit) : 
    if (limit < 2) : 
        return 0
  
    # Initialize first two even prime numbers 
    # and their sum 
    ef1 = 0
    ef2 = 2
    sm= ef1 + ef2 
      
    # calculating sum of even Fibonacci value 
    while (ef2 <= limit) : 
  
        # get next even value of Fibonacci  
        # sequence 
        ef3 = 4 * ef2 + ef1 
  
        # If we go beyond limit, we break loop 
        if (ef3 > limit) : 
            break
  
        # Move to next even number and update 
        # sum 
        ef1 = ef2 
        ef2 = ef3 
        sm = sm + ef2 
      
    return sm 
  
# Driver code 
limit = 400
print(evenFibSum(limit)) 
  
# This code is contributed by Nikita Tiwari.
Comment

PREVIOUS NEXT
Code Example
Python :: dynamo python templete 
Python :: python afficher hello world 
Python :: ignore module import log in python 
Python :: typingclub hack python 
Python :: python function to check list element ratio with total data 
Python :: how to recurse a function 
Python :: how to remove trackback on python when ctrl c 
Python :: anaconda create new environment 
Python :: iterating over 2d array python 
Python :: find record in mongodb with mongodb object id python 
Python :: python diffie hellman 
Python :: python similar strings 
Python :: Mean Kurtosis of all rows pandas 
Python :: place a widget in a specific position in tkinter 
Python :: orderd dictionary pop vs del 
Python :: python convert twitter id to date 
Python :: numpy empty array 
Python :: split every character python 
Python :: rotate x labels in plots, matplotlib 
Python :: gdscript top-down 2d movement 
Python :: python make button do more than one command 
Python :: how to get total number of rows in listbox tkinter 
Python :: python sort list of lists by second element 
Python :: pydotprint 
Python :: python get home path 
Python :: show image with ratio opencv python 
Python :: python encrypt password 
Python :: find matches between two lists python 
Python :: Access the Response Methods and Attributes in python Show Status Code 
Python :: how to change cursor on hover of button in tkinter 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =