-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10200.cpp
More file actions
79 lines (62 loc) · 1.23 KB
/
10200.cpp
File metadata and controls
79 lines (62 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <iomanip>
#include <math.h>
/* 10200 - Prime Time */
#define ONEPRIME false
using namespace std;
bool isPrime(int x)
{
int i;
if(x == 1)
{
return ONEPRIME;
}
if(x == 2)
{
return true;
}
if(x % 2 == 0)
{
return false;
}
for(i = 3 ; i * i <= x ; i += 2)
{
if(x % i == 0)
{
return false;
}
}
return true;
}
int eulerPrime(int n)
{
return n * n + n + 41;
}
void printPrimePercentage(float primePercentage)
{
cout << fixed << setprecision(2) << round(primePercentage * 100) / 100 << endl;
}
int main()
{
int a, b, primeCount, totalNumbers;
bool primes[10001];
// Calculando todos os números gerados pela fórmula de euler em um intervalo de 0 a 10000
for(int i = 0 ; i <= 10000 ; i++)
{
primes[i] = isPrime(eulerPrime(i));
}
while(cin >> a >> b)
{
primeCount = 0;
totalNumbers = b - a + 1;
for(int i = a ; i <= b ; i++)
{
if(primes[i])
{
primeCount++;
}
}
printPrimePercentage((float) primeCount / (float) totalNumbers * 100);
}
return 0;
}