-
Notifications
You must be signed in to change notification settings - Fork 2
/
10200.cpp
executable file
·124 lines (105 loc) · 2 KB
/
10200.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <iomanip>
using namespace std ;
#include <cmath>
#define Max_b 10000
#define allowAllocate 1230
class Prime
{
public :
Prime() ;
long long int value ;
long long int square ;
};
Prime::Prime()
{
value = 0 ;
square = 0 ;
}
Prime *prime ;
int numPrimes = 0 ;
void calculatePrime( long int upperLimit )
{
int numAllocate = 0 ;
int i = 0 ; // loop counter
long long int j = 0 ; // loop counter
int k = 0 ; // loop counter
numAllocate = allowAllocate ;
prime = new Prime[ numAllocate + 1 ] ;
prime[ 1 ].value = 2 ;
prime[ 1 ].square = 4 ;
prime[ 2 ].value = 3 ;
prime[ 2 ].square = 9 ;
numPrimes = 2 ;
for( i = 3 ; prime[ i - 1 ].value < upperLimit && i <= numAllocate ; i++ )
{
// find next prime.
for( j = prime[ i - 1 ].value + 2 ; j <= upperLimit ; j += 2 )
{
for( k = 1 ; prime[ k ].square < j ; k++ )
{
if( j % prime[ k ].value == 0 )
{
break ;
}
}
if( prime[ k ].square > j )
{
numPrimes++ ;
prime[ i ].value = j ;
prime[ i ].square = j * j ;
break ;
}
}
if( j > upperLimit )
{
break ;
}
}
}
int main()
{
int a = 0 ;
int b = 0 ;
long long formula = 0 ;
double primeProduced[ Max_b + 1 ] = { 0 } ;
int i = 0 ;
int j = 0 ;
calculatePrime( 10010 ) ;
cout.setf( ios::fixed ) ;
cout.precision( 2 ) ;
formula = 41 ;
primeProduced[ 0 ] = 1 ;
for( i = 1 ; i <= Max_b ; i++ )
{
formula += i * 2 ;
for( j = 1 ; prime[ j ].square < formula ; j++ )
{
if( formula % prime[ j ].value == 0 )
{
break ;
}
}
if( prime[ j ].square > formula )
{
primeProduced[ i ] = primeProduced[ i - 1 ] + 1 ;
}
else
{
primeProduced[ i ] = primeProduced[ i - 1 ] ;
}
}
while( cin >> a >> b )
{
if( a == 0 )
{
cout << primeProduced[ b ] * 100.0 / ( b - a + 1 ) + 0.001 << endl ;
}
else
{
cout << ( primeProduced[ b ] - primeProduced[ a - 1 ] ) * 100.0
/ ( b - a + 1 ) + 1.e-9 << endl ;
}
}
return 0 ;
}