2009年3月19日 星期四

Project Euler 002-1.c


1 | /*///-----------------------------------------------------------------
2 | * Project Euler Problem 002
3 | * File Name : pe-002-1.c
4 | * Author : Yen-Chin,Lee
5 | * Email : coldnew.tw@gmail.com
6 | * Create Date : 2009/03/19 16:06:04
7 | * Description : A better way than pe-002.c
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 |
22 | int main( int argc, char **argv)
23 | {
24 | int sum = 0;
25 | int x = 1, y = 1;
26 | int xtmp = 0 , ytmp = 0;
27 |
28 | while (sum < 4000000) {
29 | sum += x + y;
30 | xtmp = x + y*2;
31 | ytmp = x*2 + y*3;
32 | x = xtmp;
33 | y = ytmp;
34 | }
35 | printf("Sum = %d",sum);
36 | return 0;
37 | }
38 |

沒有留言: