aboutsummaryrefslogtreecommitdiff
path: root/challenge-164/luca-ferrari/postgresql/ch-1.plperl
blob: 0c1d347bf5fc3c3e586ce7491ef96951b85080a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- Perl Weekly Challenge 164
-- Task 1

CREATE SCHEMA IF NOT EXISTS pwc164;

CREATE OR REPLACE FUNCTION
pwc164.task1_plperl( int )
RETURNS SETOF int
AS $CODE$
  my ( $limit ) = @_;

  my $is_prime = sub {
     my ( $number ) = @_;
     for my $i ( 2 .. $number - 1 ) {
         return 0 if $number % $i == 0;
     }

     return 1;
  };

  my $is_palindrome = sub {
     return $_[ 0 ] eq reverse $_[ 0 ];
  };

  for  ( 10 .. $limit ) {
      return_next( $_ ) if $is_prime->( $_ ) && $is_palindrome->( $_ );
  }

  return undef;
$CODE$
LANGUAGE plperl;