PERL WEEKLY CHALLENGE – 066

This is my 36th week participating into the weekly challenge.


TASK #1 › Divide Integers

Submitted by: Mohammad S Anwar

You are given two integers $M and $N.

Write a script to divide the given two integers i.e. $M / $N without using multiplication, division and mod operator and return the floor of the result of the division.

Example 1:

Input: $M = 5, $N = 2
Output: 2

Example 2:

Input: $M = -5, $N = 2
Output: -2

Example 3:

Input: $M = -5, $N = -2
Output: 2

Example

Input:
    $N = 2
    $S = 4

Output:
    13, 22, 31, 40

For this challenge, i just used subtraction to divide, like one would use multiplication to add.

Perl 5 solution

#!/usr/bin/perl
# Test: ./ch-1.pl
use Modern::Perl;
use List::Util qw/sum/;
use Test::More;

is (divide(5,2),   2,  'Divide 5 , 2');
is (divide(-5,2), -3,  'Divide -5 , 2');
is (divide(-5,-2), 2,  'Divide -5 , -2');
done_testing();

sub divide {
	my ($M, $N) = @_;
	my $quotient = 0;
	my $negative = 0;

	if ($M < 0 && $N > 0) {
		$negative = 1;
		$M = - $M;
	} elsif ($M > 0 && $N < 0) {
		$negative = 1;
		$N = - $N;
	} elsif ($M < 0 && $N < 0) {
		$M = - $M;
		$N = - $N;
	}

	while ($M > $N) {
		$M = $M - $N;
		$quotient++;
	}

	return ($negative) ?
		- $quotient - 1 : $quotient;
}

Output: perl ./ch-1.pl

ok 1 - Divide 5 , 2
ok 2 - Divide -5 , 2
ok 3 - Divide -5 , -2
1..3

Raku solution

# Test: perl6 ch-1.p6
use Test;

sub MAIN() {
	is divide(5,2),   2,  'Divide 5 , 2';
	is divide(-5,2), -3,  'Divide -5 , 2';
	is divide(-5,-2), 2,  'Divide -5 , -2';
	done-testing;
}

sub divide(Int $M is copy, Int $N is copy) {
	my $quotient = 0;
	my $negative = 0;

	if ($M < 0 && $N > 0) {
		$negative = 1;
		$M = - $M;
	} elsif ($M > 0 && $N < 0) {
		$negative = 1;
		$N = - $N;
	} elsif ($M < 0 && $N < 0) {
		$M = - $M;
		$N = - $N;
	}

	while ($M > $N) {
		$M = $M - $N;
		$quotient++;
	}

	return ($negative) ??
		- $quotient - 1 !! $quotient;
}

Output perl6 ch-1.p6

ok 1 - Divide 5 , 2
ok 2 - Divide -5 , 2
ok 3 - Divide -5 , -2
1..3

TASK #2 › Power Integers

Submitted by: Mohammad S Anwar

You are given an integer $N.

Write a script to check if the given number can be expressed as mn where m and n are positive integers. Otherwise print 0.

Example 1:

For given $N = 9, it should print 32 or 3^2.

Example 2:

For given $N = 45, it should print 0.


For this challenge I just brute forced to solution looking for all possible exponentials for $i ^ $j where (2 < i, j < $N) .

Perl 5 solution

#!/usr/bin/perl
# Test: ./ch-2.pl
use Modern::Perl;
use Test::More;

is (powers(9),   '3^2',      'Power: 9');
is (powers(16),  '2^4, 4^2', 'Power: 16');
is (powers(45),  '0',        'Power: 45');
done_testing();

sub powers {
	my ($N) = @_;
	my @answers;

	for my $i (2 .. $N) {
		for my $j (2 .. $N) {
			if ($i ** $j > $N) {
				last;
			} elsif ($i ** $j == $N) {
				push @answers, $i . '^' . $j;
			}
		}
	}

	return scalar(@answers) ?
		join ', ', @answers : 0;
}

Output ./ch-2.pl

ok 1 - Power: 9
ok 2 - Power: 16
ok 3 - Power: 45
1..3

Raku solution

# Test: perl6 ch-2.p6
use Test;

sub MAIN() {
	is powers(9),   '3^2',      'Power: 9';
	is powers(16),  '2^4, 4^2', 'Power: 16';
	is powers(45),  '0',        'Power: 45';
	done-testing;
}

sub powers(Int $N) {
	my @answers;

	for (2 .. $N) -> $i {
		for (2 .. $N) -> $j {
			if ($i ** $j > $N) {
				last;
			} elsif ($i ** $j == $N) {
				@answers.push($i ~ '^' ~ $j);
			}
		}
	}

	return @answers.elems ??
		@answers.join(", ") !! 0;
}

Output perl6 ch-2.p6

ok 1 - Power: 9
ok 2 - Power: 16
ok 3 - Power: 45
1..3

One thought on “PERL WEEKLY CHALLENGE – 066

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s