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 |  


沒有留言: