mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-19 19:03:02 +08:00
snake_case all the things
This commit is contained in:
26
project_euler/problem_02/sol1.py
Normal file
26
project_euler/problem_02/sol1.py
Normal file
@ -0,0 +1,26 @@
|
||||
'''
|
||||
Problem:
|
||||
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2,
|
||||
the first 10 terms will be:
|
||||
1,2,3,5,8,13,21,34,55,89,..
|
||||
By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.
|
||||
e.g. for n=10, we have {2,8}, sum is 10.
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
n = int(raw_input().strip())
|
||||
i=1
|
||||
j=2
|
||||
sum=0
|
||||
while(j<=n):
|
||||
if((j&1)==0): #can also use (j%2==0)
|
||||
sum+=j
|
||||
temp=i
|
||||
i=j
|
||||
j=temp+i
|
||||
print(sum)
|
12
project_euler/problem_02/sol2.py
Normal file
12
project_euler/problem_02/sol2.py
Normal file
@ -0,0 +1,12 @@
|
||||
def fib(n):
|
||||
a, b, s = 0, 1, 0
|
||||
while b < n:
|
||||
if b % 2 == 0 and b < n: s += b
|
||||
a, b = b, a+b
|
||||
ls.append(s)
|
||||
|
||||
T = int(input().strip())
|
||||
ls = []
|
||||
for _ in range(T):
|
||||
fib(int(input().strip()))
|
||||
print(ls, sep = '\n')
|
20
project_euler/problem_02/sol3.py
Normal file
20
project_euler/problem_02/sol3.py
Normal file
@ -0,0 +1,20 @@
|
||||
'''
|
||||
Problem:
|
||||
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
|
||||
0,1,1,2,3,5,8,13,21,34,55,89,..
|
||||
Every third term from 0 is even So using this I have written a simple code
|
||||
By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.
|
||||
e.g. for n=10, we have {2,8}, sum is 10.
|
||||
'''
|
||||
"""Python 3"""
|
||||
n = int(raw_input())
|
||||
a=0
|
||||
b=2
|
||||
count=0
|
||||
while 4*b+a<n:
|
||||
c=4*b+a
|
||||
a=b
|
||||
b=c
|
||||
count=count+a
|
||||
print(count+b)
|
||||
|
Reference in New Issue
Block a user