Advertisement
snakerdlk

Desafio Geek - C v2

Aug 25th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. #define BUFFER_SIZE 1024
  7.  
  8. int main(int argc, char **argv){
  9.     if(argc != 2){
  10.         printf("Usage: %s <size of palindrome>\n", argv[0]);
  11.         exit(0);
  12.     }
  13.  
  14.     unsigned size = atoi(argv[1]);
  15.     unsigned halfsize = size/2; //to reduce operations
  16.     if(size < 2){
  17.         printf("Size of palindrome must be greater than 1\n");
  18.         exit(0);
  19.     }
  20.  
  21.     char buffer[BUFFER_SIZE];
  22.     char read[BUFFER_SIZE];
  23.     char *aux = (char *) malloc(sizeof(char) * size);
  24.     unsigned index;
  25.     unsigned length;
  26.     unsigned counter;
  27.     unsigned first, last;
  28.     unsigned long long mult;
  29.     unsigned long long num;
  30.     unsigned long long max;
  31.  
  32.     while(fgets(read, BUFFER_SIZE-size, stdin) != NULL){
  33.         //fread(&buffer[size], BUFFER_SIZE-size, stdin) ?
  34.         snprintf(buffer, BUFFER_SIZE, "%s%s", &buffer[strlen(buffer)-size+1], read);
  35.  
  36.         index = 0;
  37.         length = strlen(buffer)-size;
  38.         while(index < length){
  39.             for(counter = 0; counter < halfsize; counter++){
  40.                 first = index+counter;
  41.                 last = index+ (size-1) -counter;
  42.                 if(buffer[first] != buffer[last]){
  43.                     break;
  44.                 }
  45.             }  
  46.             if(counter == halfsize){
  47.                 //for(counter = 0; counter < size; counter++){
  48.                                 //        aux[counter] = buffer[index+counter];
  49.                                 //}
  50.                                 //aux[counter] = '\0';
  51.                 //num = atol(aux);
  52.                 for(counter = 0, mult = 1, num = 0; counter < size; counter++){
  53.                     //printf("%u - %u - %llu\n", counter, (buffer[index+(size-counter-1)]-48), mult *(buffer[index+(size-counter-1)]-48));
  54.                     num += mult * (buffer[index+(size-counter-1)]-48);
  55.                     mult *= 10;
  56.                 }
  57.  
  58.                 //printf("%s <-> %llu\n",aux, num);
  59.  
  60.  
  61.                 if(num % 2 && num % 3){
  62.                     max = sqrt(num);
  63.                     for (counter = 1; 6*counter+1 < max; counter++){
  64.                         //printf("%u, %u\n", 6*counter+1, 6*counter-1);
  65.                         if(num % (6*counter+1) == 0)break;
  66.                         if(num % (6*counter-1) == 0)break;
  67.                     }
  68.                     if(6*counter+1 > max){
  69.                         printf("%llu\n", num);
  70.                     }
  71.                 }
  72.  
  73.             }
  74.             index++;
  75.         }
  76.     }      
  77.     free(aux);
  78.     return 0;
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement