[ create a new paste ] login | about

Link: http://codepad.org/XHwaC99z    [ raw code | fork ]

C, pasted on Sep 12:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

unsigned int primordial(unsigned int x);
int is_prime(unsigned int x);

int main(int argc, char *argv[]) {
	unsigned int x;
	if(argc < 2) {
		puts("Please specify a primordial.");
		return EXIT_FAILURE;
	}
	
	sscanf(argv[1], "%u", &x);
	
	printf("%u\n", primordial(x));
	return EXIT_SUCCESS;
}

unsigned int primordial(unsigned int x) {
	unsigned int count = 0, product = 1;
	
	while(x > 1) {
		if(is_prime(x))
			product *= x;
		x--;
	}
	return product;
}

int is_prime(unsigned int x) {
	int i = 2;
	if(x < 2) return 0;
	for(; i*i <= x; i++) {
		if(!(x % i))
			return 0;
	}
	
	return 1;
}


Create a new paste based on this one


Comments: