From c7c23ae7f9c37b6e670ec29b9d7bf1c169aff410 Mon Sep 17 00:00:00 2001 From: KiritoTRw <3021577574@qq.com> Date: Thu, 11 Aug 2016 09:07:22 +0800 Subject: [PATCH] Create isPrime.cpp --- .ACM-Templates/isPrime.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .ACM-Templates/isPrime.cpp diff --git a/.ACM-Templates/isPrime.cpp b/.ACM-Templates/isPrime.cpp new file mode 100644 index 0000000..04ac238 --- /dev/null +++ b/.ACM-Templates/isPrime.cpp @@ -0,0 +1,20 @@ +//Written by Coffee. 判断素数 +bool isPrime(int num) +{ + if (num == 2 || num == 3) + { + return true; + } + if (num % 6 != 1 && num % 6 != 5) + { + return false; + } + for (int i = 5; i*i <= num; i += 6) + { + if (num % i == 0 || num % (i+2) == 0) + { + return false; + } + } + return true; +}