aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2022-06-13 13:04:49 +0200
committerLuca Ferrari <fluca1978@gmail.com>2022-06-13 13:04:49 +0200
commit6c7d3c5db13041ff57c2b2cec74d065df218eb0f (patch)
tree708180fc049354bd04be61f40c2fbd41c984de6e
parent0ffbaf512b67c2fc01297106de3870570783a5c6 (diff)
downloadperlweeklychallenge-club-6c7d3c5db13041ff57c2b2cec74d065df218eb0f.tar.gz
perlweeklychallenge-club-6c7d3c5db13041ff57c2b2cec74d065df218eb0f.tar.bz2
perlweeklychallenge-club-6c7d3c5db13041ff57c2b2cec74d065df218eb0f.zip
Task 1 plperl done
-rw-r--r--challenge-169/luca-ferrari/postgresql/ch-1.plperl54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-169/luca-ferrari/postgresql/ch-1.plperl b/challenge-169/luca-ferrari/postgresql/ch-1.plperl
new file mode 100644
index 0000000000..8610899ba2
--- /dev/null
+++ b/challenge-169/luca-ferrari/postgresql/ch-1.plperl
@@ -0,0 +1,54 @@
+-- Perl Weekly Challenge 169
+-- Task 1
+
+CREATE SCHEMA IF NOT EXISTS pwc169;
+
+CREATE OR REPLACE FUNCTION
+pwc169.task1_plperl( int )
+RETURNS SETOF int
+AS $CODE$
+ my ($limit) = @_;
+
+ my $is_prime = sub {
+ my ($value) = @_;
+
+ for ( 2 .. $value - 1 ) {
+ return 0 if $value % $_ == 0;
+ }
+
+ return 1;
+ };
+
+ my $compute_factors = sub {
+ my ($value) = @_;
+ my @factors;
+
+ for ( 2 .. $value - 1 ) {
+ next if ! $is_prime->( $_ );
+
+ while ( $value % $_ == 0 ) {
+ push @factors, $_;
+ $value /= $_;
+ }
+ }
+
+ return @factors;
+ };
+
+
+ for ( 1 .. 9999999 ) {
+ my @factors = $compute_factors->( $_ );
+ elog( DEBUG, "Number $_ with factors " . join(',', @factors) );
+ next if ( @factors != 2 );
+ next if $factors[ 0 ] == $factors[ 1 ];
+ if ( length( $factors[ 0 ] ) == length( $factors[ 1 ] ) ) {
+ $limit--;
+ return_next( $_ );
+ }
+
+ last if ! $limit;
+ }
+
+ return undef;
+$CODE$
+LANGUAGE plperl;