1 | /*///-----------------------------------------------------------------
2 | * Project Euler Problem 002
3 | * File Name : pe-002.c
4 | * Author : Yen-Chin,Lee
5 | * Email : coldnew.tw@gmail.com
6 | * Create Date : 2009/03/19 15:54:41
7 | * Description :
8 | *
9 | * Problem :
10 | *
11 | * Each new term in the Fibonacci sequence is generated by adding
12 | * the previous two terms.
13 | * By starting with 1 and 2, the first 10 terms will be:
14 | * 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
15 | * Find the sum of all the even-valued terms in the sequence
16 | * which do not exceed four million.
17 | /*///---------------------------- Copyright (C) ,2009 coldnew --------
18 |
19 | #include <stdio.h>
20 | #include <stdlib.h>
21 | int Fib(int x)
22 | {
23 | if (1 == x) return 1;
24 | if (2 == x) return 2;
25 | else return Fib(x-1) + Fib(x-2);
26 |
27 | }
28 |
29 | int main( int argc, char **argv)
30 | {
31 | int sum = 0;
32 | int i=1;
33 |
34 | while (1) {
35 | if (0 == Fib(i) % 2) {
36 | if (sum > 4000000) {
37 | printf("Sum = %d",sum);
38 | break;
39 | } else {
40 | sum += Fib(i);
41 | }
42 | }
43 | i++;
44 | }
45 | return 0;
46 | }
2009年3月19日 星期四
Project Euler 002.c
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言