예제 #1
0
int main()
{int j;
    sieve_prime();
	for(j=0;j<100;j++)
    printf("%I64d\n", p[j]);
    printf("%d", ip[101]);
//    getch();
    return 0;
}
예제 #2
0
int main()
{
	unsigned long k,n;
	sieve_prime();
	while(scanf("%lu",&n)!=EOF)
	{
		printf("(%lu, %lu)\n",q[n],w[n]);
	}
	return 0;
}
예제 #3
0
파일: prime.c 프로젝트: xiongyw/xiongyw
int main()
{

    prime_t next_prime_index = 0;
    FILE *fp;

    /* a value 0 means the prime number at that index is not yet determined */
    g_prime = (prime_t *) calloc(TOTAL_PRIME_NUMBER, sizeof(prime_t));
    if (!g_prime) {
        printf("ERROR: can not malloc array to store prime numbers, abort!\n");
        return -1;
    }

    /* try to read the db into ram first */
    fp = fopen(PRIME_DB_FILE_NAME, "r");
    if (fp == NULL) {
        printf("INFO: %s does not exist.\n", PRIME_DB_FILE_NAME);
    } else {
        if (1 != fread(&next_prime_index, sizeof(prime_t), 1, fp)) {
            printf("ERROR: can not read prime count %lld in %s\n", next_prime_index, PRIME_DB_FILE_NAME);
            next_prime_index = 0;
        } else {
            printf("INFO: %lld of prime numbers are stored in '%s'. reading it...\n", next_prime_index, PRIME_DB_FILE_NAME);
            if (next_prime_index >= TOTAL_PRIME_NUMBER)
                next_prime_index = TOTAL_PRIME_NUMBER;
            if (next_prime_index != fread(g_prime, sizeof(prime_t), next_prime_index, fp)) {
                printf("ERROR: reading db file '%s' failed. ignore the db content.", PRIME_DB_FILE_NAME);
                next_prime_index = 0;
            }
        }
        fclose(fp);
    }

    printf("INFO: this session starts to find the prime number with index %lld...\n", next_prime_index);

    do {
        next_prime_index = sieve_prime(next_prime_index, g_prime);
    } while (next_prime_index < TOTAL_PRIME_NUMBER);

    save_prime_db(next_prime_index);

    if (next_prime_index > TOTAL_PRIME_NUMBER)
        next_prime_index = TOTAL_PRIME_NUMBER;

#if (0)
    /* print the primes to STDOUT */
    print_prime_db(next_prime_index);
#endif

#if (0)
    /* test: cubic sum and factorization */
    for (i = 0; i < 100; i++) {
        factor_node *tmp, *root;
        prime_t sum = g_prime[i] * g_prime[i] * g_prime[i] + g_prime[i + 1] * g_prime[i + 1] * g_prime[i + 1];
        prime_t num_primes = 0; /* how many different prime factors */
        prime_t exp_sum = 0;    /* sum of exponent of each prime factor */
        prime_t num_factors = 1;        /* number of factors */

        printf("%4d(%d)^3 + %4d(%d)^3 = %d = ", g_prime[i], i, g_prime[i + 1], i + 1, sum);

        tmp = root = factorize(sum);

        /* print the factorized result */
        while (tmp) {
            num_primes++;
            exp_sum += tmp->exponent;
            num_factors *= (tmp->exponent + 1);
            printf("%4d(%d)^%d ", g_prime[tmp->prime_index], tmp->prime_index, tmp->exponent);
            tmp = tmp->next;
        }
        printf(" => %3d, %2d, %3d\n", num_primes, exp_sum, num_factors);

        free_factor_list(root);
    }
#endif

#if (0)
    /* test: number of prime factors and any different factors */
    for (i = 20000; i < 100000; i++) {
        factor_node *tmp, *root;
        prime_t num_primes = 0; /* how many different prime factors */
        prime_t exp_sum = 0;    /* sum of exponent of each prime factor */
        prime_t num_factors = 1;        /* number of factors */
        double loglog;

        printf("%5d =  ", i);

        tmp = root = factorize(i);

        /* print the factorized result */
        while (tmp) {
            num_primes++;
            exp_sum += tmp->exponent;
            num_factors *= (tmp->exponent + 1);
            /* printf("%4d(%d)^%d ", g_prime[tmp->prime_index], tmp->prime_index, tmp->exponent); */
            tmp = tmp->next;
        }
        /* printf(" => prime=%d loglog(%d)=%f, factor=%3d, log(f)=%f\n", num_primes, i, log(log(i)), num_factors, log(num_factors)); */
        loglog = log(log(i));
        printf(" => prime=%lld loglog(%d)=%f, delta=%f\n", num_primes, i, loglog, num_primes - loglog);

        free_factor_list(root);
    }

#endif

#if (0)
    /* test: get gcd */
    for (i = 1000; i < 2000; i++) {
        prime_t b = 210;
        int steps = 0;
        prime_t gcd = get_gcd_2(i, b, NULL, NULL, &steps);
        /* printf("<==(%4d, %4d)=%4d, step=%2d (%f)\n", i, b, gcd, steps, 2*log(b/2.));  */
    }

    for (i = 0; i < 10; i++) {
        prime_t gcd = get_gcd_n(i, g_prime);
        printf("get_gcd_n(%d..)=%d\n", i, gcd);
    }
#endif

#if (0)
    /* test: factorization for numbers in one modulus */
    for (i = 1; i < 100; i++) {
        int mod = 12;           /* modulus */
        int res = 1;            /* residue */
        prime_t num_primes = 0; /* how many different prime factors */
        prime_t exp_sum = 0;    /* sum of exponent of each prime factor */
        prime_t prime_index_sum = 0;    /* index sum of prime factors */
        prime_t num_factors = 1;

        printf("\n%3d*%d+%d=%5d=", i, mod, res, mod * i + res);
        factor_node *tmp = factorize(mod * i + res);
        while (tmp) {
            num_primes++;
            exp_sum += tmp->exponent;
            prime_index_sum += tmp->prime_index;
            num_factors *= (tmp->exponent + 1);
            printf("%d^%d ", g_prime[tmp->prime_index], tmp->exponent);
            tmp = tmp->next;
        }
        printf("num_prime=%d exp_sum=%d, idx_sum=%d num_fac=%d", num_primes, exp_sum, prime_index_sum, num_factors);
        free_factor_list(tmp);
    }
    printf("\n");
#endif

#if (0)
    /* test: n1+n2=? */

    for (i = 2; i < 10000; i++) {
        for (j = 2; j < 1000; j++) {
            if (i == j)
                continue;
            factor_node *n1 = factorize(i);
            factor_node *n2 = factorize(j);
            factor_node *tmp;
            printf("\ni=%d, j=%d { ", i, j);
            tmp = n1;
            while (tmp) {
                printf("%d,", tmp->prime_index);
                tmp = tmp->next;
            }
            printf("|");
            tmp = n2;
            while (tmp) {
                printf("%d", tmp->prime_index);
                if (s_is_index_found(n1, tmp->prime_index))
                    printf("$");
                printf(",");
                tmp = tmp->next;
            }
            printf("}={");

            factor_node *n3 = factorize(i + j);
            tmp = n3;
            while (tmp) {
                printf("%d", tmp->prime_index);
                if (s_is_index_found(n1, tmp->prime_index))
                    printf("#");
                if (s_is_index_found(n2, tmp->prime_index))
                    printf("@");
                printf(",");
                tmp = tmp->next;
            }
            printf("}");

            free_factor_list(n1);
            free_factor_list(n2);
            free_factor_list(n3);

        }
    }
#endif

#if (0)
    /* test cesaro's theorem: prob(a_|_b)=6/(pi^2) */

    for (i = 1, j = 0; i < 100000; i++) {
        int a, b, gcd;

        a = random();
        b = random();
        gcd = get_gcd_2(a, b, NULL, NULL, NULL);
        if (gcd == 1) {
            j++;
            printf("%d (a=%12d, b=%12d)=1: %f\n", i, a, b, j * 1.0 / i);
        } else {
            printf("%d (a=%12d, b=%12d)#1: %f\n", i, a, b, j * 1.0 / i);
        }

    }
#endif

#if (0)
    /* test: prob(a_|_b_|_c)=0.28519.. */

    for (i = 1, j = 0; i < 100000; i++) {
        int a, b, c, ab, ac, bc;

        a = random();
        b = random();
        c = random();
        ab = get_gcd_2(a, b, NULL, NULL, NULL);
        bc = get_gcd_2(b, c, NULL, NULL, NULL);
        ac = get_gcd_2(a, c, NULL, NULL, NULL);
        if (ab == 1 && ac == 1 && bc == 1) {
            j++;
            printf("%d (%12d, %12d, %12d)=1: %f\n", i, a, b, c, j * 1.0 / i);
        } else {
            printf("%d (%12d, %12d, %12d)#1: %f\n", i, a, b, c, j * 1.0 / i);
        }
    }
#endif

#if (0)
    /* test: prob((a,b,c)=1)=0.83247=1/zeta(3)
       i.e.: prob(3)=1/zeta(3)
     */

    for (i = 1, j = 0; i < 100000; i++) {
        int a[3], gcd;

        a[0] = random();
        a[1] = random();
        a[2] = random();
        gcd = get_gcd_n(3, a);
        if (gcd == 1) {
            j++;
            printf("%d (%12d, %12d, %12d)=1: %f\n", i, a[0], a[1], a[2], j * 1.0 / i);
        } else {
            printf("%d (%12d, %12d, %12d)#1\n", i, a[0], a[1], a[2]);
        }
    }
#endif

#if (0)
    /* test: prob(4)=1/zeta(4)=90/pi^4=0.9239
       generally we have prob(n)=1/zeta(n), n>1      */

    for (i = 1, j = 0; i < 100000; i++) {
        int a[4], gcd;

        a[0] = random();
        a[1] = random();
        a[2] = random();
        a[3] = random();
        gcd = get_gcd_n(4, a);
        if (gcd == 1) {
            j++;
            printf("%d (%12d, %12d, %12d, %12d)=1: %f\n", i, a[0], a[1], a[2], a[3], j * 1.0 / i);
        } else {
            printf("%d (%12d, %12d, %12d, %12d)#1\n", i, a[0], a[1], a[2], a[3]);
        }
    }
#endif

#if (0)
    /* test: prob(5)=1/zeta(5)=1/1.0369277   */

    for (i = 1, j = 0; i < 100000; i++) {
        int a[5], gcd;

        a[0] = random();
        a[1] = random();
        a[2] = random();
        a[3] = random();
        a[4] = random();
        gcd = get_gcd_n(5, a);
        if (gcd == 1) {
            j++;
            printf("%d (%12d, %12d, %12d, %12d...)=1: %f\n", i, a[0], a[1], a[2], a[3], j * 1.0 / i);
        } else {
            printf("%d (%12d, %12d, %12d, %12d...)#1\n", i, a[0], a[1], a[2], a[3]);
        }
    }
#endif

#if (0)
    /* test: FP(n) denotes the total number of primes appeared in the factorization 
       of the first n members of the fibonacci series.
       we define F(1)=2 and F(2)=3. 
       thus FP(1)=1, FP(2)=2, FP(3)=3 (2,3,5), FP(4)=3 (2,3,5)...
       this is to see if there are any patterns in FP(n).
     */
    {
        int p_idx[100000];      /* store index of primes found */
        int i, j, n = 2, fpn = 2;       /* n and number of FP(n) */
        int f1 = 2, f2 = 3, f3; /* f1+f2=f3 */
        factor_node *root = NULL;

        p_idx[0] = 0;
        p_idx[1] = 1;

        /* let's start the iteration */
        for (i = 3; i < 50000; i++) {

            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;            /* for next loop */

            if (root) {
                free_factor_list(root);
                root = NULL;
            }

            root = factorize(f3);
            if (!root) {
                printf("ERROR: factorize(%d) failed.\n", f3);
                return (1);
            }

            while (root) {

                int prime_index = root->prime_index;
                int is_exist = 0;

                for (j = 0; j < fpn; j++) {
                    if (p_idx[j] == prime_index) {
                        is_exist = 1;
                        break;
                    }
                }
                if (!is_exist) {
                    p_idx[fpn] = prime_index;
                    fpn++;
                }

                root = (factor_node *) (root->next);
            }

            printf("f(%d)=%d, FP(%d)=%d, %f\n", i, f3, i, fpn, fpn * 1.0 / i);

        }

        if (root)
            free_factor_list(root);
    }

#endif

    free(g_prime);

    return 0;
}