Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #define BUFFER_SIZE 1024
- int main(int argc, char **argv){
- if(argc != 2){
- printf("Usage: %s <size of palindrome>\n", argv[0]);
- exit(0);
- }
- unsigned size = atoi(argv[1]);
- unsigned halfsize = size/2; //to reduce operations
- if(size < 2){
- printf("Size of palindrome must be greater than 1\n");
- exit(0);
- }
- char buffer[BUFFER_SIZE];
- char read[BUFFER_SIZE];
- char *aux = (char *) malloc(sizeof(char) * size);
- unsigned index;
- unsigned length;
- unsigned counter;
- unsigned first, last;
- unsigned long long mult;
- unsigned long long num;
- unsigned long long max;
- while(fgets(read, BUFFER_SIZE-size, stdin) != NULL){
- //fread(&buffer[size], BUFFER_SIZE-size, stdin) ?
- snprintf(buffer, BUFFER_SIZE, "%s%s", &buffer[strlen(buffer)-size+1], read);
- index = 0;
- length = strlen(buffer)-size;
- while(index < length){
- for(counter = 0; counter < halfsize; counter++){
- first = index+counter;
- last = index+ (size-1) -counter;
- if(buffer[first] != buffer[last]){
- break;
- }
- }
- if(counter == halfsize){
- //for(counter = 0; counter < size; counter++){
- // aux[counter] = buffer[index+counter];
- //}
- //aux[counter] = '\0';
- //num = atol(aux);
- for(counter = 0, mult = 1, num = 0; counter < size; counter++){
- //printf("%u - %u - %llu\n", counter, (buffer[index+(size-counter-1)]-48), mult *(buffer[index+(size-counter-1)]-48));
- num += mult * (buffer[index+(size-counter-1)]-48);
- mult *= 10;
- }
- //printf("%s <-> %llu\n",aux, num);
- if(num % 2 && num % 3){
- max = sqrt(num);
- for (counter = 1; 6*counter+1 < max; counter++){
- //printf("%u, %u\n", 6*counter+1, 6*counter-1);
- if(num % (6*counter+1) == 0)break;
- if(num % (6*counter-1) == 0)break;
- }
- if(6*counter+1 > max){
- printf("%llu\n", num);
- }
- }
- }
- index++;
- }
- }
- free(aux);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement