-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime_number.cpp
More file actions
38 lines (30 loc) · 829 Bytes
/
prime_number.cpp
File metadata and controls
38 lines (30 loc) · 829 Bytes
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
#include <nan.h>
#include <cmath>
#include <cstdlib>
NAN_METHOD(IsPrime) {
if (!info[0]->IsNumber()) {
Nan::ThrowTypeError("argument must be a number!");
return;
}
int number = (int) info[0]->NumberValue();
if (number < 3) {
info.GetReturnValue().Set(Nan::False());
return;
}
if(number % 2 == 0 || number % 3 ==0){
info.GetReturnValue().Set(Nan::False());
return;
}
int step = 4, m = std::abs(sqrt(number) + 1);
for(int i = 5; i < m; step = 6-step, i += step) {
if (number % i == 0) {
info.GetReturnValue().Set(Nan::False());
return;
}
}
info.GetReturnValue().Set(Nan::True());
}
NAN_MODULE_INIT(Initialize) {
NAN_EXPORT(target, IsPrime);
}
NODE_MODULE(addon, Initialize);