aboutsummaryrefslogtreecommitdiff
path: root/challenge-198/deadmarshal/cpp/ch-2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-198/deadmarshal/cpp/ch-2.cpp')
-rw-r--r--challenge-198/deadmarshal/cpp/ch-2.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-198/deadmarshal/cpp/ch-2.cpp b/challenge-198/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..adfcbd6a20
--- /dev/null
+++ b/challenge-198/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,30 @@
+#include<iostream>
+
+bool is_prime(int n)
+{
+ int i = 5;
+ if((n == 2) || (n == 3)) return true;
+ if((n <= 1) || (n % 2 == 0) || (n % 3 == 0)) return false;
+ while(i * i <= n)
+ {
+ if((n % i == 0) || (n % (i+2) == 0)) return false;
+ i += 6;
+ }
+ return true;
+}
+
+int prime_count(int n)
+{
+ int count = 0;
+ for(int i = 1; i < n; ++i) if(is_prime(i)) count++;
+ return count;
+}
+
+int main()
+{
+ std::cout << prime_count(10) << '\n';
+ std::cout << prime_count(15) << '\n';
+ std::cout << prime_count(1) << '\n';
+ std::cout << prime_count(25) << '\n';
+}
+