2009年3月22日 星期日

Project Euler 004.c


1 |/*///-----------------------------------------------------------------
2 | * Project Euler Problem 004
3 | * File Name : pe-004.c
4 | * Author : Yen-Chin,Lee
5 | * Email : coldnew.tw@gmail.com
6 | * Create Date : 2009/03/22 14:55:59
7 | * Description : This version I use malloc (though I do not need to use it),
8 | * if you don't know how to use malloc() and free(),
9 | * just remove it and add " char buffer[100] " in this file
10 | *
11 | * Problem :
12 | *
13 | * A palindromic number reads the same both ways.
14 | * The largest palindrome made from the product of two 2-digit numbers
15 | * is 9009 = 91 × 99.
16 | *
17 | * Find the largest palindrome made from the product of two 3-digit numbers.
18 | /*///---------------------------- Copyright (C) ,2009 coldnew --------
19 |
20 | #include <stdio.h>
21 | #include <stdlib.h>
22 | #include <string.h>
23 |
24 |
25 | int isPlain(int testNum)
26 | {
27 | char *buffer = (char *)malloc(sizeof(char) * 100);
28 | if (NULL == buffer) {
29 | fprintf(stderr,"Not enough memory\n");
30 | exit(1);
31 | }
32 |
33 | sprintf(buffer,"%d",testNum);
34 |
35 | int a = 0;
36 | int b = strlen(buffer)-1;
37 |
38 | while (a < b && buffer[a] == buffer[b]) {
39 | a++;
40 | b--;
41 | }
42 | free(buffer);
43 |
44 | return (a >= b);
45 | }
46 |
47 | int main( int argc, char **argv)
48 | {
49 | int i,j,k;
50 | int maxNum=0;
51 | for (i = 1;i < 1000;i++) {
52 | for (j = 1;j < 1000; j++) {
53 | k = i*j;
54 | if (k > maxNum && isPlain(k)) {
55 | maxNum = k;
56 | }
57 | }
58 | }
59 | printf("max=%d ",maxNum);
60 |
61 | return 0;
62 | }
63 |

2009年3月21日 星期六

Project Euler 003-1.c


  1 |  /*///-----------------------------------------------------------------
  2 |   *  Project Euler Problem 003
  3 |   *  File Name   : pe-003-1.c
  4 |   *  Author      : Yen-Chin,Lee
  5 |   *  Email       : coldnew.tw@gmail.com
  6 |   *  Create Date : 2009/03/21 19:05:37
  7 |   *  Description : The same way as pe-003.c without use c99 standard
  8 |   *                and stdbool.h
  9 |   *  Compile Opt : gcc pe-003-1.c -o 003-1
 10 |   *  
 11 |   *  Problem :
 12 |   *
 13 |   *  The prime factors of 13195 are 5, 7, 13 and 29.
 14 |   *  What is the largest prime factor of the number 600851475143 ?
 15 |  /*///---------------------------- Copyright (C) ,2009 coldnew --------
 16 |  
 17 |  #include <stdio.h>
 18 |  #include <stdlib.h>
 19 |  
 20 |  short isPrime (unsigned long long testNum)
 21 |  {
 22 |          int i = 2;
 23 |          if (1 == testNum){
 24 |                  return 0;
 25 |          }
 26 |          for(i =2 ; i <= (testNum / 2) ; i++){
 27 |                  if (0 == testNum % i) {
 28 |                          return 0;
 29 |                  } else {
 30 |                          return 1;
 31 |                  }                
 32 |          }
 33 |  }
 34 |  
 35 |  int main( int argc, char **argv)
 36 |  {
 37 |          int i = 2;
 38 |          unsigned long long num = 600851475143;
 39 |          unsigned long long max = 0;
 40 |  
 41 |          for(i = 2 ; (i * i) <= num ; i++){
 42 |                  if (0 ==  (num % i)) {
 43 |                          if (isPrime(i)) {
 44 |                                  if (i > max) {
 45 |                                          max = i;
 46 |                                  }
 47 |                          }
 48 |                  }
 49 |          }
 50 |          printf("The largest prime factor is %llu",max);
 51 |  
 52 |          return 0;
 53 |  }
 54 |  


Project Euler 003.c


  1 |  /*///-----------------------------------------------------------------
  2 |   *  Project Euler Problem 003
  3 |   *  File Name   : pe-003.c
  4 |   *  Author      : Yen-Chin,Lee
  5 |   *  Email       : coldnew.tw@gmail.com
  6 |   *  Create Date : 2009/03/21 19:05:37
  7 |   *  Description :
  8 |   *  Compile Opt : gcc pe-003.c -std=c99 -o 003
  9 |   *  Problem :
 10 |   *
 11 |   *  The prime factors of 13195 are 5, 7, 13 and 29.
 12 |   *  What is the largest prime factor of the number 600851475143 ?
 13 |  /*///---------------------------- Copyright (C) ,2009 coldnew --------
 14 |  
 15 |  #include <stdio.h>
 16 |  #include <stdlib.h>
 17 |  #include <stdbool.h>
 18 |  
 19 |  bool isPrime (unsigned long long testNum)
 20 |  {
 21 |          if (1 == testNum){
 22 |                  return false;
 23 |          }
 24 |          for(int i =2 ; i <= (testNum / 2) ; i++){
 25 |                  if (0 == testNum % i) {
 26 |                          return false;
 27 |                  } else {
 28 |                          return true;
 29 |                  }                
 30 |          }
 31 |  }
 32 |  
 33 |  int main( int argc, char **argv)
 34 |  {
 35 |          unsigned long long num = 600851475143;
 36 |          unsigned long long max = 0;
 37 |  
 38 |          for(int i = 2 ; (i * i) <= num ; i++){
 39 |                  if (0 ==  (num % i)) {
 40 |                          if (isPrime(i)) {
 41 |                                  if (i > max) {
 42 |                                          max = i;
 43 |                                  }
 44 |                          }
 45 |                  }
 46 |          }
 47 |          printf("The largest prime factor is %llu",max);
 48 |  
 49 |          return 0;
 50 |  }
 51 |  


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 |

Project Euler 002.c


  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 |  }