diff options
| -rw-r--r-- | challenge-008/alicia-bielsa/perl5/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-008/alicia-bielsa/perl5/ch-2.pl | 46 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-008/alicia-bielsa/perl5/ch-1.pl b/challenge-008/alicia-bielsa/perl5/ch-1.pl new file mode 100644 index 0000000000..52e9d0fc26 --- /dev/null +++ b/challenge-008/alicia-bielsa/perl5/ch-1.pl @@ -0,0 +1,32 @@ +#Write a script that computes the first five perfect numbers. +#A perfect number is an integer that is the sum of its positive proper divisors +#(all divisors except itself). + +use strict; +use warnings; + +my @aPrimeNumbers = getPrimeNumbers(5); + +foreach my $primeNumber (@aPrimeNumbers){ + print 2 ** ($primeNumber -1) * ( 2 ** $primeNumber -1 )."\n"; +} + + +sub getPrimeNumbers { + my $totalNumbers = shift; + my @aPrimes = (); + my $limit = 1000; + foreach my $number ( 2..$limit ){ + my $isPrime = 1; + foreach my $i ( 2..$number-1){ + if ($number % $i == 0 ){ + $isPrime = 0; + } + } + if ( $isPrime ){ + push ( @aPrimes , $number ); + } + last if (scalar(@aPrimes) == $totalNumbers); + } + return @aPrimes; +} diff --git a/challenge-008/alicia-bielsa/perl5/ch-2.pl b/challenge-008/alicia-bielsa/perl5/ch-2.pl new file mode 100644 index 0000000000..60c363d7d5 --- /dev/null +++ b/challenge-008/alicia-bielsa/perl5/ch-2.pl @@ -0,0 +1,46 @@ +#Write a function, ‘center’, whose argument is a list of strings, +#which will be lines of text. +#The function should insert spaces at the beginning of the lines of text +#so that if they were printed, the text would be centered, and return the modified lines. +use strict; +use warnings; + + +#my @aLinesCentered = center("This", "is", "a test of the", "center function"); + +# we generate some strings with a character of our choice to draw a diamond +my @aStrings =(); +my $character = '*'; +my $count = 2; +while ($count <= 20){ + push (@aStrings, $character x $count); + $count += 2; +} +while ($count >= 2){ + push (@aStrings, $character x $count); + $count -= 2; +} + + + +my @aLinesCentered = center(@aStrings); + + +foreach my $line (@aLinesCentered){ + print "$line\n"; +} + + +sub center { + my @aLinesToCenter = @_; + + my @aLinesCentered = (); + + my $maxLength = length((sort {length($b) <=> length ($a)} @aLinesToCenter)[0]); + + foreach my $lineToCenter (@aLinesToCenter){ + push (@aLinesCentered, (' ' x ( abs ( length($lineToCenter)/2 - $maxLength /2 ))).$lineToCenter); + } + + return @aLinesCentered; +}
\ No newline at end of file |
