This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)
Task #1 this week is as follows:
Write a script to accept a positive number as command line argument and print the smallest multiple of the given number consists of digits 0 and 1.
Brute Force
It’s easy enough to brute force this problem, by checking every multiple in order:
$_ += $_[0] while /[^10]/;
$_
now contains the answer. Unfortunately, some numbers—particularly multiples of 9—require extremely large multiples. For example, 99 × 1122334455667789 = 111111111111111111 takes several minutes to discover via this method.
We can do better, though.
Continue reading “PWC 049 › Smallest multiple containing only 1 and 0”