Skip to content

Instantly share code, notes, and snippets.

@misterhat
Last active August 29, 2015 14:02
Show Gist options
  • Save misterhat/15abab1dcbc6b6a94def to your computer and use it in GitHub Desktop.
Save misterhat/15abab1dcbc6b6a94def to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
const int sizes[] = { 9, 99, 999, 9999, 99999, 999999, 9999999 };
int get_places(int number) {
for (int i = 0; i < 6; i++) {
if (number <= sizes[i]) {
return i + 1;
}
}
return -1;
}
bool is_prime(int number) {
if (number % 2 == 0) {
return false;
}
for (int i = 3; i < ((int) sqrt(number)) + 1; i += 2) {
if (number % i == 0) {
return false;
}
}
return true;
}
bool is_circ_prime(int number) {
if (!is_prime(number)) {
return false;
}
/*int places = log10(number) + 1;*/
int places = get_places(number);
for (int i = 0; i < places; i++) {
if (i > 0 && !is_prime(number)) {
return false;
}
number = (number / 10) + ((number % 10) * pow(10, places - 1));
}
return true;
}
int main(int argc, char** args) {
int count = 1;
for (int i = 3; i < 1000000; i += 2) {
if (is_circ_prime(i)) {
count++;
}
}
printf("%d\n", count);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment