aboutsummaryrefslogtreecommitdiff
path: root/challenge-047/user-person/perl/ch-2.pl
blob: a501321878050dfb97919574def70283a78b948e (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
#!/usr/bin/env perl

###########################################################################
# script name: ch-2.pl                                                    #
#                                                                         #
# https://perlweeklychallenge.org/blog/perl-weekly-challenge-047/         #
#                                                                         #
# Write a script to print first 20 Gapful Numbers greater than or equal   #
# to 100.                                                                 #
#                                                                         #
# https://oeis.org/A108343                                                #
#                                                                         #
# numbers that are divisible by the number formed by their first and last #
# digit.                                                                  #
#                                                                         #
###########################################################################

use strict;
use warnings;

my $QUANTITY = 20;
my ($first, $last);
my $count = 0;

sub firstDigit {
    my $number = $_[0];
    while ($number >= 10) {
        $number /= 10;
    }
    return int($number);
}

for (my $i = 100; $count < $QUANTITY ; ++$i) {

    $first = firstDigit $i;
    $last  = $i % 10;
    my $formedBy = ($first * 10) + $last;

    if ( $i % $formedBy == 0 ) {
        print "$i ";
        ++$count;
    }
}
print "\n";

__END__
output:
        100 105 108 110 120 121 130 132 135 140 143 150 154 160 165 170 176 180 187 190