diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-28 22:41:57 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-28 22:41:57 +0100 |
| commit | c36a149320f4d3ba904d2ce48085462db29bfef9 (patch) | |
| tree | 1d19331df10ff3116cff3e5e65d35209a4023660 /challenge-012 | |
| parent | 9f4380849f88644d6e38e5348f7e4ffc32885104 (diff) | |
| download | perlweeklychallenge-club-c36a149320f4d3ba904d2ce48085462db29bfef9.tar.gz perlweeklychallenge-club-c36a149320f4d3ba904d2ce48085462db29bfef9.tar.bz2 perlweeklychallenge-club-c36a149320f4d3ba904d2ce48085462db29bfef9.zip | |
Add C and C++ solutions to challenge 012.1
Diffstat (limited to 'challenge-012')
| -rw-r--r-- | challenge-012/paulo-custodio/c/ch-1.c | 51 | ||||
| -rw-r--r-- | challenge-012/paulo-custodio/cpp/ch-1.cpp | 50 | ||||
| -rw-r--r-- | challenge-012/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-012/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-012/paulo-custodio/test.pl | 39 |
5 files changed, 124 insertions, 36 deletions
diff --git a/challenge-012/paulo-custodio/c/ch-1.c b/challenge-012/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..967109929d --- /dev/null +++ b/challenge-012/paulo-custodio/c/ch-1.c @@ -0,0 +1,51 @@ +/* +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 <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +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())) + ; + printf("%d\n", euclid); +} 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; +} diff --git a/challenge-012/paulo-custodio/t/test-1.yaml b/challenge-012/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..adaf9beab6 --- /dev/null +++ b/challenge-012/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 30031 diff --git a/challenge-012/paulo-custodio/t/test-2.yaml b/challenge-012/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..975c4a1c90 --- /dev/null +++ b/challenge-012/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: / /a/b/c/d /a/b/cd /a/b/cc /a/b/c/d/e + input: + output: /a/b +- setup: + cleanup: + args: ": :a:b:c:d :a:b:cd :a:b:cc :a:b:c:d:e" + input: + output: ":a:b" +- setup: + cleanup: + args: / /b/c/d /a/b/c/d /a/b/cd /a/b/cc /a/b/c/d/e + input: + output: diff --git a/challenge-012/paulo-custodio/test.pl b/challenge-012/paulo-custodio/test.pl index db440a3a8f..ba6c37260b 100644 --- a/challenge-012/paulo-custodio/test.pl +++ b/challenge-012/paulo-custodio/test.pl @@ -1,37 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; +#!/usr/bin/env perl +use Modern::Perl; use Test::More; - -is capture("perl perl/ch-1.pl"), "30031\n"; - -is capture("perl perl/ch-2.pl / ". - " /a/b/c/d ". - " /a/b/cd ". - " /a/b/cc ". - " /a/b/c/d/e "), "/a/b\n"; - -is capture("perl perl/ch-2.pl : ". - " :a:b:c:d ". - " :a:b:cd ". - " :a:b:cc ". - " :a:b:c:d:e "), ":a:b\n"; - -is capture("perl perl/ch-2.pl / ". - " /b/c/d ". - " /a/b/c/d ". - " /a/b/cd ". - " /a/b/cc ". - " /a/b/c/d/e "), "\n"; - -done_testing; - - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \r\t]*\n/\n/g; - return $out; -} +require '../../challenge-001/paulo-custodio/test.pl'; |
