Last active
August 29, 2015 14:10
-
-
Save amoshyc/d820e5c27d269adddcf8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int primes[52*20]; | |
void init_primes() { | |
int i, j; | |
for(i=0; i<52*20; i++) | |
primes[i] = 1; | |
primes[0] = 0; | |
primes[1] = 0; | |
primes[2] = 1; | |
for(i=4; i<52*20; i+=2) | |
primes[i] = 0; | |
for(i=3; i<52*20; i+=2) | |
if (primes[i] == 1) | |
for(j=i*i; j<52*20; j+=i) | |
primes[j] = 0; | |
} | |
int main() { | |
init_primes(); | |
char input[21]; | |
while (scanf("%s", input) != EOF) { | |
int value = 0; | |
int len = strlen(input); | |
int i; | |
for(i=0; i<len; i++) { | |
if ('a' <= input[i] && input[i] <= 'z') | |
value += (int) (input[i] - 'a' + 1); | |
else | |
value += (int) (input[i] - 'A' + 27); | |
} | |
if (value == 1 || primes[value]) | |
printf("It is a prime word.\n"); | |
else | |
printf("It is not a prime word.\n"); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int primes[500]; | |
int p_idx = 0; | |
int is_prime(const int N) { | |
int i; | |
for (i=0; i<p_idx && primes[i]*primes[i] <= N; i++) | |
if (N % primes[i] == 0) | |
return 0; | |
return 1; | |
} | |
void init_primes() { | |
primes[0] = 2; | |
primes[1] = 3; | |
primes[2] = 5; | |
p_idx = 3; | |
int i; | |
for (i=7; i < 52*20; i+=2) | |
if (is_prime(i)) | |
primes[p_idx++] = i; | |
} | |
int binary_search(const int n) { | |
int top = p_idx-1; | |
int down = 0; | |
while (down <= top) { | |
int mid = (top + down) / 2; | |
if (primes[mid] == n) | |
return 1; | |
else if (n < primes[mid]) | |
top = mid-1; | |
else | |
down = mid+1; | |
} | |
return 0; | |
} | |
int main() { | |
init_primes(); | |
char input[21]; | |
while (scanf("%s", input) != EOF) { | |
int value = 0; | |
int len = strlen(input); | |
int i; | |
for(i=0; i<len; i++) { | |
if ('a' <= input[i] && input[i] <= 'z') | |
value += (int) (input[i] - 'a' + 1); | |
else | |
value += (int) (input[i] - 'A' + 27); | |
} | |
if (value == 1 || binary_search(value)) | |
printf("It is a prime word.\n"); | |
else | |
printf("It is not a prime word.\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment