aboutsummaryrefslogtreecommitdiff
path: root/challenge-012/paulo-custodio/cpp/ch-1.cpp
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2021-07-04 22:18:36 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2021-07-04 22:18:36 +0800
commit28eabc69c70046cc047e98e2b2987d71b4aae9fa (patch)
treef2fe2351445f68451ec14f21072719bc9db488b0 /challenge-012/paulo-custodio/cpp/ch-1.cpp
parentcdfef3f9404041b4c7936a26532757e63fd4fcef (diff)
parent5f7b76d5b841606c17f177a90397196ebf434c05 (diff)
downloadperlweeklychallenge-club-28eabc69c70046cc047e98e2b2987d71b4aae9fa.tar.gz
perlweeklychallenge-club-28eabc69c70046cc047e98e2b2987d71b4aae9fa.tar.bz2
perlweeklychallenge-club-28eabc69c70046cc047e98e2b2987d71b4aae9fa.zip
week 119
Diffstat (limited to 'challenge-012/paulo-custodio/cpp/ch-1.cpp')
-rw-r--r--challenge-012/paulo-custodio/cpp/ch-1.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-012/paulo-custodio/cpp/ch-1.cpp b/challenge-012/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..b18ecaaa19
--- /dev/null
+++ b/challenge-012/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,50 @@
+/*
+Challenge 012
+
+Challenge #1
+The numbers formed by adding one to the products of the smallest primes are
+called the Euclid Numbers (see wiki). Write a script that finds the smallest
+Euclid Number that is not prime. This challenge was proposed by
+Laurent Rosenfeld.
+*/
+
+#include <iostream>
+using namespace std;
+
+bool is_prime(int n) {
+ if (n <= 1)
+ return false;
+ if (n <= 3)
+ return true;
+ if ((n % 2) == 0 || (n % 3) == 0)
+ return false;
+ for (int i = 5; i * i <= n; i += 6)
+ if ((n % i) == 0 || (n % (i + 2)) == 0)
+ return false;
+ return true;
+}
+
+int next_prime(int n) {
+ if (n <= 1)
+ return 2;
+ do {
+ n++;
+ } while (!is_prime(n));
+ return n;
+}
+
+int next_euclid(void) {
+ static int prime = 1;
+ static int prime_prod = 1;
+
+ prime = next_prime(prime);
+ prime_prod *= prime;
+ return prime_prod + 1;
+}
+
+int main(void) {
+ int euclid;
+ while (is_prime(euclid = next_euclid()))
+ ;
+ cout << euclid << endl;
+}