Ejemplo n.º 1
0
void getPrimesUpto(unsigned long n){
	unsigned long p;
	if(n>highestNumberCheckedForPrimality){
		for(p=(primeTop->p)+2; p<=n; p +=2)
			addIfPrime(p);
		highestNumberCheckedForPrimality = n;
	}
}
int main()
{
    vi primes;
    int i;
    primes.push_back(2);
    primes.push_back(3);
    primes.push_back(5);
    for(i = 7; i < 2000; i+=2) {
        addIfPrime(primes, i);
    }
    primes.insert(primes.begin(), 1);

    char c;
    int sum = 0;
    char arr[21] = {0};
    int count = 0;
    while(1) {
        int rc = scanf("%c", &c);
        if(c == '\n' || rc == EOF) {
            if(sum == 0) break;
            //printf("arr %s sum %d\n", arr, sum);
            if(std::binary_search(primes.begin(), primes.end(), sum))
                printf("It is a prime word.\n");
            else
                printf("It is not a prime word.\n");
            if(rc == EOF) break;
            sum = 0;
            count = 0;
            memset(arr, 0, 20);
            continue;
        }

        assert(('a' <= c && c <= 'z' ) || ('A' <= c && c <= 'Z' ));
        sum += c >= 'a' ? c - 'a' + 1 : c - 'A' + 27;
        arr[count++] = c;
    }
    return 0;
}
int main()
{
    vi primes;
    primes.push_back(2);
    primes.push_back(3);
    primes.push_back(5);

    //get all primes less than 2^15
    int i = 7;
    while(i <= MAXN) {
        addIfPrime(primes, i);
        i+=2;
    }
    //for(vi::iterator it = primes.begin(); it != primes.end(); it++) printf("%d ", *it); printf("\n");

    //primes.insert(primes.begin(), 1);
    int num;
    while(scanf("%d\n", &num) != EOF && num != 0){
        int p = getPairs(primes, num);
        printf("%d\n", p);
    }
    return 0;

}