给定一个正整数n,请编写一个函数solution,求该斐波拉契数列截至第n项时,所有的偶数只和。
示例:
solution(5) == 2,因为数列值是:0,1,1,2,3,5,其中偶数只和为0+2=2。
题目难度:简单
题目来源:codewar:sumFibs
def solution(n: int) -> int:
pass
assert solution(5) == 2
assert solution(9) == 44
assert solution(10) == 44
给定一个正整数n,请编写一个函数solution,求该斐波拉契数列截至第n项时,所有的偶数只和。
示例:
solution(5) == 2,因为数列值是:0,1,1,2,3,5,其中偶数只和为0+2=2。
题目难度:简单
题目来源:codewar:sumFibs
def solution(n: int) -> int:
pass
assert solution(5) == 2
assert solution(9) == 44
assert solution(10) == 44
def solution(n: int) -> int:
f_list = [0, 1]
o_list = []
for _ in range(n-2):
one_num = sum(f_list[-2:])
f_list.append(one_num)
if one_num % 2 == 0:
o_list.append(one_num)
return sum(o_list)
assert solution(5) == 2
assert solution(9) == 10
assert solution(10) == 44
def solution(n: int) → int:
l = [0,1]
list_even = []
i = 3
while i <= n:
temp_num = sum(l[-2:])
l.append(temp_num)
if temp_num%2 == 0:
list_even.append(temp_num)
i += 1
return sum(list_even)
assert solution(1) == 0
assert solution(5) == 2
assert solution(9) == 10
assert solution(10) == 44
def solution(n: int) -> int:
l = 0
m = 1
nums = 2
list1 = [0]
while nums <= n:
if nums % 2 == 0:
l = l + m
nums += 1
if l % 2 == 0:
list1.append(l)
if nums % 2 != 0:
m = m + l
nums += 1
if m % 2 == 0:
list1.append(m)
return sum(list1)
assert solution(5) == 2
assert solution(9) == 44
assert solution(10) == 44
def solution(n: int) -> int:
nums = [0,1]
for i in range(1,n):
nums.append(nums[i]+nums[i-1])
return sum([item for item in nums if item%2 ==0])
assert solution(5) == 2
assert solution(9) == 44
assert solution(10) == 44
def sum1(n):
array =[0]*n
su=0
array[0] = 1
array[1] = 1
for i in range(2,n):
array[i] = array[i-1] + array[i-2]
if array[i] % 2 == 0:
su = su + array[i]
return su
def solution(n: int) -> int:
list1 = [0, 1]
res = 0
for i in range(2, n):
tmp = list1[i-1] + list1[i-2]
list1.append(tmp)
for j in list1:
if j % 2 == 0:
res += j
return res
public int solution1(int n){
int res =0;
if (n<2){
return 0;
}
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(0);
arrayList.add(1);
for (int i = 2; i < n; i++) {
int temp = arrayList.get(i-2) +arrayList.get(i-1);
if(temp%2 ==0){
res += temp;
}
arrayList.add(temp);
}
return res;
}
def Fibonacci(n: int):
if n == 0:
return 0
elif n == 1:
return 1
else:
return Fibonacci(n-1) + Fibonacci(n-2)
def sollution_1122(n):
‘’’
给定一个正整数n,请编写一个函数solution,求该斐波拉契数列截至第n项时,所有的偶数只和。
示例:
solution(5) == 2,因为数列值是:0,1,1,2,3,5,其中偶数只和为0+2=2。
斐波那契数列
F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2)
:return:
‘’’
result = 0
num = 0
while num <= n:
data = Fibonacci(num)
if data%2 == 0:
result = result+data
num += 1
return result
def test_solution_1122():
‘’’
0,1,1,2,3,5,8,13,21,34
:return:
‘’’
assert sollution_1122(0) == 0
assert sollution_1122(2) == 0
assert sollution_1122(5) == 2
assert sollution_1122(9) == 44
def solution(n: int) → int:
list1 = [0, 1]
num = 0
if n > 2:
for i in range(2, n + 1):
list1.append(list1[i - 2] + list1[i - 1])
if (list1[i - 2] + list1[i - 1]) % 2 == 0:
num += (list1[i - 2] + list1[i - 1])
return num
assert solution(5) == 2
assert solution(9) == 44
assert solution(10) == 44
求和汇总
def solution(n: int) -> int:
sum = 0
for i in range(n+1):
result = feb(i)
if result%2==0: sum += result
print(sum)
return sum
计算斐波那契
def feb(n: int) -> int:
if n == 0 : return 0
if n == 1 : return 1
return feb(n-1)+feb(n-2)
def solution(n: int) -> int:
data = [0, 1]
for i in range(1, n):
data.append(data[i - 1] + data[i])
result = sum([i for i in data if i % 2 == 0])
return result
assert solution(5) == 2
assert solution(9) == 44
assert solution(10) == 44
def solution(n):
result = 0
for i in range(0, n+1):
# print(Fibonacci(i))
if Fibonacci(i)%2 == 0:
result = result + Fibonacci(i)
# print(result)
return result
def Fibonacci(n):
if n == 0:
return 0
if n == 1:
return 1
return Fibonacci(n-1) + Fibonacci(n-2)
assert solution(5) == 2
assert solution(9) == 44
assert solution(10) == 44