By Shahed Nooshmand

Perl Weekly Challenge: week 66

Task #1

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.

Everyone knows this one:

sub MAIN(Int $M, Int $N) {
	die "Error: division by zero" unless $N;
	my $m = abs $M;
	my $n = abs $N;
	my $d = 0;
	++$d while ($m -= $n) ≥ 0;
	say ($M < 0 xor $N < 0) ?? -$d - 1 !! $d;


Task #2

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.

Please make sure m > 1 and n > 1.

BONUS: If there are more than one ways to express the given number then print all possible solutions.

Here you go:

raku -e 'say .key ~ .value.comb.map: {(8304, 185, 178, 179, |(8308..8313))».chr[$_]} for (2..$N.sqrt).map({$_ => $N.log: $_}).grep: {.value == .value.floor}'

One could do without the superscripts, of course.