-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeventh_Problem.cpp
46 lines (40 loc) · 952 Bytes
/
Seventh_Problem.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
#include <iostream>
using namespace std;
int sqrti(int x)
{
union { float f; int x; } v;
// convert to float
v.f = (float)x;
// fast aprox sqrt
// assumes float is in IEEE 754 single precision format
// assumes int is 32 bits
// b = exponent bias
// m = number of mantissa bits
v.x -= 1 << 23; // subtract 2^m
v.x >>= 1; // divide by 2
v.x += 1 << 29; // add ((b + 1) / 2) * 2^m
// convert to int
return (int)v.f;
}
int main()
{
int position = 10001;
int found = 1;
for(int i=3;i;i+=2){
int root = sqrti(i);
bool isPrime = true;
for(int k=2;k<=root;k++){
if(i%k==0){
isPrime = false;
}
}
if(isPrime){
found+=1;
if(found==position){
cout << "THE prime number on this position is :" << i << endl;
break;
}
}
}
return 0;
}