diff options
| -rw-r--r-- | challenge-116/wanderdoc/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-116/wanderdoc/perl/ch-2.pl | 38 |
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-116/wanderdoc/perl/ch-1.pl b/challenge-116/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..4ea790bc53 --- /dev/null +++ b/challenge-116/wanderdoc/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a number $N >= 10. Write a script to split the given number such that the difference between two consecutive numbers is always 1 and it shouldn’t have leading 0. +Print the given number if it impossible to split the number. + +Example Input: $N = 1234 Output: 1,2,3,4 +Input: $N = 91011 Output: 9,10,11 +Input: $N = 10203 Output: 10203 as it is impossible to split satisfying the conditions. +=cut + + + + + + +sub find_sequence +{ + my $int = $_[0]; + my $length = length($int); + for my $i ( 1 .. $length / 2 ) + { + my $copy = $int; + + my @numbers; + $numbers[0] = substr($copy, 0, $i); + $copy = substr($copy, $i); + for my $j ( $i .. $i + 1 ) + { + while ( substr($copy, 0, $j) - 1 == $numbers[-1] and substr($copy, 0, 1) != 0 ) + { + push @numbers, substr($copy, 0, $j); + $copy = substr($copy, $j); + return join(",",@numbers) unless length($copy); + } + } + + + } + return $int; +} + + +print find_sequence($_), $/ for (12345, 91011, 10203, 100101102, 9899100102);
\ No newline at end of file diff --git a/challenge-116/wanderdoc/perl/ch-2.pl b/challenge-116/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..4388a566cc --- /dev/null +++ b/challenge-116/wanderdoc/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a number $N >= 10. Write a script to find out if the given number $N is such that sum of squares of all digits is a perfect square. Print 1 if it is otherwise 0. +Example +Input: $N = 34 Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2 +Input: $N = 50 Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2 +Input: $N = 52 Output: 0 as 5^2 + 2^2 => 25 + 4 => 29 +=cut + + + + + +use List::Util qw(sum); + +sub is_perfect_square +{ + my $num = $_[0]; + my $square_root = sqrt($num); + return $square_root == int($square_root) ? 1 : 0; +} + + +sub sum_of_squares +{ + my $int = $_[0]; + my @digits = split(//,$int); + my $sum_of_squares = sum(map $_ * $_, @digits); + return $sum_of_squares; +} + +for my $num ( 34, 50, 52, 679, 5311 ) +{ + print $num, ': ', is_perfect_square(sum_of_squares($num)), $/; +}
\ No newline at end of file |
