• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "detector.h"
17 
18 #include <iostream>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <sys/stat.h>
22 
23 using namespace std;
24 namespace DetectorTest {
25     const int HALF = 2;
26     const int START_INDEX = 3;
27 }
28 
IsPrime(int n)29 bool IsPrime(int n)
30 {
31     if (n <= 1) {
32         return false;
33     }
34 
35     if (!(n % DetectorTest::HALF)) {
36         return n == DetectorTest::HALF;
37     }
38 
39     for (int i = DetectorTest::START_INDEX;; i += DetectorTest::HALF) {
40         if (i > (n / i)) {
41             break;
42         }
43         if (!(n % i)) {
44             return false;
45         }
46     }
47 
48     return true;
49 }
50 
FileExist(const char * fileName)51 bool FileExist(const char* fileName)
52 {
53     if (fileName == nullptr) {
54         return false;
55     }
56     struct stat myStat;
57     return (!stat(fileName, &myStat));
58 }
59