aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-168/luca-ferrari/postgresql/ch-1.plperl40
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-168/luca-ferrari/postgresql/ch-1.plperl b/challenge-168/luca-ferrari/postgresql/ch-1.plperl
new file mode 100644
index 0000000000..585589ec97
--- /dev/null
+++ b/challenge-168/luca-ferrari/postgresql/ch-1.plperl
@@ -0,0 +1,40 @@
+-- Perl Weekly Challenge 168
+-- Task 1
+
+CREATE SCHEMA IF NOT EXISTS pwc168;
+
+CREATE OR REPLACE FUNCTION
+pwc168.task1_plperl( int )
+RETURNS SETOF bigint
+AS $CODE$
+ my ( $limit ) = @_;
+ $limit //= 13;
+ my @perrin = (3, 0, 2);
+ my $seen = {};
+
+ my $is_prime = sub {
+ my ( $number ) = @_;
+
+ for ( 2 .. $number - 1 ) {
+ return undef if $number % $_ == 0;
+ }
+
+ return 1;
+ };
+
+ while ( $limit > 0 ) {
+ my $current = $perrin[ -2 ] + $perrin[ -3 ];
+ elog( DEBUG, "Limit $n and current is $current" );
+ push @perrin, $current;
+ next if ! $is_prime->( $current );
+ next if $seen->{ $current };
+
+ # found!
+ $seen->{ $current }++;
+ return_next( $current );
+ $limit--;
+ }
+
+return undef;
+$CODE$
+LANGUAGE plperl;