Given a number N find the sum of all the even valued terms in the Fibonacci sequence less than or equal to N.
The first few terms of Fibonacci Numbers are, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ,… (Even numbers are highlighted).
 
Example 1:
Input:
N = 8
Output:
10
Explanation:
There are two even 
fibonacci numbers less
than equal to 8, 8 and 2 
and 8 + 2 = 10
Example 2:
Input:
N = 89
Output:
44
Explanation:
There are three even
fibonacci numbers less than
equal to 89, 8, 2 and 34 
8 + 2 + 34 = 44
Your Task:
You don't need to read input or print anything. Your task is to complete the function evenFibSum() which takes an integer N as input parameters and returns the sum of all the Fibonacci number less than equal to N.
 
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
 
Constraints:
1 <= N <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3

class Solution:
    def evenFibSum (self, N):

Solutions:
def fib(n, memo={}):
	if n in memo:
		return memo[n]
	elif n < 3:
		memo[n] = 1
	else:
		memo[n] = fib(n - 1) + fib(n - 2)
	return memo[n]

class Solution:

	def evenFibSum(self, N):
		n = 1
		curr_fib_num = 0
		sum = 0
		while curr_fib_num <= N:
			sum += curr_fib_num
			curr_fib_num = fib(3 * n)
			n += 1
		return sum


class Solution:

	def evenFibSum(self, N):
		a = 0
		b = 1
		c = 0
		s = 0
		if N == 0 or N == 1:
			return 0
		for i in range(2, N + 1):
			c = a + b
			if c > N:
				break
			elif c % 2 == 0:
				s = s + c
			a = b
			b = c
		return s


class Solution:

	def evenFibSum(self, N):
		a = 0
		b = 1
		s = 0
		c = 0
		while c < N:
			c = a + b
			a = b
			b = c
			if b % 2 == 0 and b < N:
				s = s + b
		return s


class Solution:

	def evenFibSum(self, N):
		(n1, n2) = (1, 2)
		sum = 2
		while True:
			n3 = n1 + n2
			if n3 > N:
				break
			if n3 % 2 == 0:
				sum += n3
			n1 = n2
			n2 = n3
		return sum


class Solution:

	def evenFibSum(self, N):
		(a, b, c, summ) = (1, 1, 1, 0)
		mod = 10 ** 9 + 7
		while c <= N:
			c = (a + b) % mod
			if N >= c and c & 1 == 0:
				summ += c
			(a, b) = (b, c)
		return summ


class Solution:

	def evenFibSum(self, N):
		(a, b, c, s, i) = (1, 2, 2, 2, 0)
		m = 10 ** 9 + 7
		while N >= c:
			c = a + b
			a = b
			b = c
			if N >= c and c % 2 == 0:
				s += c
			i += 1
		return s


class Solution:

	def evenFibSum(self, N):
		if N < 2:
			return 0
		ef1 = 0
		ef2 = 2
		sm = ef1 + ef2
		while N > 2:
			ef3 = 4 * ef2 + ef1
			if ef3 > N:
				break
			ef1 = ef2
			ef2 = ef3
			sm = sm + ef2
		return sm


class Solution:

	def evenFibSum(self, n):
		k = []
		a = 0
		b = 1
		s = 0
		while a <= n:
			c = a + b
			if a % 2 == 0:
				s += a
			a = b
			b = c
		return s


class Solution:

	def evenFibSum(self, N):
		a = 0
		b = 1
		c = 0
		l = []
		s = 0
		while a <= N:
			if a & 1 == 0 and a <= N:
				s = s + a
			l.append(a)
			c = a + b
			a = b
			b = c
		return s


class Solution:

	def evenFibSum(self, N):
		f = [0, 1]
		arr = []
		for i in range(2, N):
			if f[i - 1] + f[i - 2] <= N:
				f.append(f[i - 1] + f[i - 2])
				if (f[i - 1] + f[i - 2]) % 2 == 0:
					arr.append(f[i - 1] + f[i - 2])
			else:
				break
		return sum(arr)


class Solution:

	def evenFibSum(self, N):
		if N < 2:
			return 0
		ans = 0
		a = 0
		b = 1
		for i in range(1000):
			c = a + b
			if c > N:
				break
			if c % 2 == 0:
				ans += c
			a = b
			b = c
		return ans


class Solution:

	def evenFibSum(self, N):
		ret = []
		ret.append(0)
		ret.append(1)
		summ = 0
		for i in range(2, N + 1):
			ret.append(ret[0] + ret[1])
			if ret[-1] > N:
				break
			if ret[-1] % 2 == 0:
				summ += ret[-1]
			ret.pop(0)
		return summ


class Solution:

	def evenFibSum(self, N):
		fib1 = 0
		fib2 = 2
		sum = fib1 + fib2
		if N <= 2:
			return 0
		while fib2 <= N:
			fib3 = 4 * fib2 + fib1
			if fib3 > N:
				break
			fib1 = fib2
			fib2 = fib3
			sum += fib2
		return sum


class Solution:

	def evenFibSum(self, N):
		sum = 0
		f = [1, 1]
		while f[1] < N:
			v = f[0] + f[1]
			if v % 2 == 0 and v <= N:
				sum += f[0] + f[1]
			(f[0], f[1]) = (f[1], f[0] + f[1])
		return sum


class Solution:

	def evenFibSum(self, N):
		evenSum = 0
		first = 1
		second = 1
		while N >= second:
			third = first + second
			if second % 2 == 0:
				evenSum += second
			first = second
			second = third
		return evenSum


class Solution:

	def evenFibSum(self, n):
		S = 0
		x = 0
		y = 1
		res = 0
		while res <= n:
			res = x + y
			if res % 2 == 0:
				S = S + res
			x = y
			y = res
		if res > n and res % 2 == 0:
			S = S - res
		return S


class Solution:

	def evenFibSum(self, N):
		a = 1
		b = 1
		s = 0
		while b <= N:
			temp = a
			a = b
			b = temp + b
			if b <= N:
				if b % 2 == 0:
					s += b
		return s


class Solution:

	def evenFibSum(self, N):
		(a, b, val) = (1, 1, 0)
		while b <= N:
			if b % 2 == 0:
				val += b
			(a, b) = (b, a + b)
		return val