Monday, April 20, 2015

UVa 10200 - Prime Time

// UVa 10200 - Prime Time

#include <stdio.h>
#include <math.h>

#define MAX_N 10000

int cumulative_table[MAX_N + 1];

void printPercentage(int part, int total) {
	//UVa judge doesn't accept it without the 1e-9
	printf("%.2lf\n", (part * 100.0) / (double) (total) + 1e-9);
}

int main() {

	for (int i = 0; i <= MAX_N; i++) {
		int possible_prime = i * i + i + 41;

		int possible_prime_root = sqrt(possible_prime);
		bool prime = true;
		for (int j = 3; j <= possible_prime_root; j += 2) {
			if (possible_prime % j == 0) {
				prime = false;
				break;
			}
		}
		if (prime)
			cumulative_table[i] = cumulative_table[i - 1] + 1;
		else
			cumulative_table[i] = cumulative_table[i - 1];
	}

	int a, b;
	while (scanf("%d%d", &a, &b) != EOF) {
		int count;
		if (a > 0)
			count = cumulative_table[b] - cumulative_table[a - 1];
		else
			count = cumulative_table[b] - cumulative_table[a] + 1;

		printPercentage(count, b - a + 1);
	}

	return 0;
}

No comments:

Post a Comment