일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- java
- 프랙탈
- python
- 제곱근의뜻
- algeomath
- 시뮬레이션
- 지오지브라
- 작도
- 정오각형
- 큰 수의 법칙
- 삼각함수의그래프
- Geogebra
- 확률실험
- 이항분포
- 수학탐구
- 하합
- 알지오매스
- 오일러
- 프로젝트 오일러
- 큰수의법칙
- 상합
- 몬테카를로
- 리만합
- 피타고라스 정리
- project euler
- 파이썬
- counting sunday
- 재귀함수
- 블록코딩
- 구분구적법
- Today
- Total
이경수 선생님의 수학실험실
Problem 2(Even Fibonacci numbers) 본문
Problem 2(Even Fibonacci numbers)
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 four million, find the sum of the even-valued terms.
In Python:
i=1
limit=4000000
sum=0
def fibonacci(n):
if n==1:
return 1
elif n==2:
return 2
else:
return fibonacci(n-2)+fibonacci(n-1)
while fibonacci(i)<limit:
if fibonacci(i)%2==0:
sum+=fibonacci(i)
i+=1
print(sum)
Run time: 5.832750082015991 seconds
In Java:
package project_euler;
public class Euler2 {
public static void main(String[] args) {
int i = 1;
int limit = 4000000;
int sum = 0;
long startTime = System.currentTimeMillis();
while (fibonacci(i) < limit) {
if (fibonacci(i) % 2 == 0) {
sum += fibonacci(i);
}
i++;
}
long endTime = System.currentTimeMillis();
System.out.println(sum);
System.out.println((double)(endTime - startTime)/(double)1000 +"seconds");
System.out.println((endTime - startTime) +"ms");
}
public static int fibonacci(int n) {
if(n==1) {
return 1;
}else if (n==2) {
return 2;
}
else {
return fibonacci(n-1)+fibonacci(n-2);
}
}
}
Run time: 0.084seconds
solution: 4613732
[from Project Euler: https://projecteuler.net/problem=2]
'Project Euler' 카테고리의 다른 글
Problem 6(Sum square difference) (0) | 2019.02.08 |
---|---|
Problem 5(Smallest multiple) (0) | 2019.02.08 |
Problem 4(Largest palindrome product) (0) | 2019.02.07 |
Problem 3(Largest prime factor) (0) | 2019.02.06 |
Problem 1(Multiples of 3 and 5) (0) | 2019.02.06 |