예제 #1
0
void factor(mpz_t N)
{
	int res;
	char *str;
	mpz_t divisor;
	mpz_t next;

	mpz_init(divisor);
	mpz_init(next);


	if (mpz_cmp(N, one) == 0)
		return;

	res = mpz_probab_prime_p(N, 10);
	if (res) {
		str = mpz_to_str(N);
		if (!str)
			return;

		printf(" %s", str);
		free(str);
		return;
	}

	rho(divisor, N);

	factor(divisor);
	mpz_tdiv_q(next, N, divisor);
	factor(next);
}
예제 #2
0
static void find_factors(mpz_t base)
{
	char *str;
	int res;
	mpz_t i;
	mpz_t half;
	mpz_t two;

	mpz_init_set_str(two, "2", 10);
	mpz_init_set_str(i, "2", 10);
	mpz_init(half);
	mpz_cdiv_q(half, base, two);

	str = mpz_to_str(base);
	if (!str)
		return;

	/*
	 * We simply return the prime number itself if the base is prime.
	 * (We use the GMP probabilistic function with 10 repetitions).
	 */
	res = mpz_probab_prime_p(base, 10);
	if (res) {
		printf("%s is a prime number\n", str);
		free(str);
		return;
	}

	printf("Trial: prime factors for %s are:", str);
	free(str);
	do {
		if (mpz_divisible_p(base, i) && verify_is_prime(i)) {
			str = mpz_to_str(i);
			if (!str)
				return;
			printf(" %s", str);
			free(str);
		}

		mpz_nextprime(i, i);
	} while (mpz_cmp(i, half) <= 0);
	printf("\n");
}
예제 #3
0
int main(int argc, const char *argv[])
{
	int res;
	char *str;
	mpz_t largenum;

	if (argc <= 0) {
		fprintf(stderr, "usage: %s\n", argv[0]);
		exit(EXIT_SUCCESS);
	}

	fprintf(stderr, "setting pitch\n");
	orient_range.orient.pitch   = 180;
	orient_range.pitch_range    = 10;

	fprintf(stderr, "setting roll\n");
	orient_range.orient.roll    = 0;
	orient_range.roll_range     = 10;

	fprintf(stderr, "setting azimuth\n");
	orient_range.orient.azimuth = 0;
	orient_range.azimuth_range  = 0;

	mpz_init_set_str(one, "1", 10);
	mpz_init_set_str(two, "2", 10);

	gmp_randinit_default(randstate);

	sleep(1);

	if (syscall(__NR_orientlock_read, &orient_range) == -1) {
		fprintf(stderr, "error: Unable to obtain lock\n");
		exit(EXIT_FAILURE);
	}

	fp = fopen("./integer", "r");

	if ((fgets(integer, INTEGER_BUF_SIZE, fp)) == NULL) {
			fprintf(stderr, "error: Unable to read from file");
			exit(EXIT_FAILURE);
	}
	chomp_line(integer);

	mpz_init(largenum);
	mpz_init_set_str(largenum, integer, 10);

	str = mpz_to_str(largenum);
	if (!str)
		return EXIT_FAILURE;

	/*
	 * We simply return the prime number itself if the base is prime.
	 * (We use the GMP probabilistic function with 10 repetitions).
	 */
	res = mpz_probab_prime_p(largenum, 10);
	if (res) {
		printf("%s is a prime number\n", str);
		free(str);
		mpz_add(largenum, largenum, one);
	}

	printf("Prime factors for %s are:  ", str);
	free(str);

	factor(largenum);
	printf("\n");

	fclose(fp);

	if (syscall(__NR_orientunlock_read, &orient_range) == -1) {
		fprintf(stderr, "error: Unable to unlock\n");
		exit(EXIT_FAILURE);
	}

	return EXIT_SUCCESS;
}