blob: 580ce698b90be01b11f0b321cead862b35abee35 (
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
-- Perl Weekly Challenge 169
-- Task 2
CREATE SCHEMA IF NOT EXISTS pwc169;
CREATE OR REPLACE FUNCTION
pwc169.task2_plperl( int )
RETURNS SETOF int
AS $CODE$
use Math::BigInt;
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;
};
my $min = sub {
my $found = shift @_;
for ( @_ ) {
$found = $_ if $_ < $found;
}
return $found;
};
my $is_achille = sub {
my ($number) = @_;
my $bag = {};
for ( $compute_factors->( $number ) ) {
$bag->{ $_ }++;
}
return $min->( values( %$bag ) ) >= 2 && Math::BigInt::bgcd( values( %$bag ) )->numify == 1;
};
for ( 1 .. 999999 ) {
if ( $is_achille->( $_ ) ) {
$limit--;
return_next( $_ );
}
last if ! $limit;
}
return undef;
$CODE$
LANGUAGE plperlu;
|