diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2020-02-26 22:46:52 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2020-02-26 22:46:52 -0500 |
| commit | 0e833cab3416554823901d8a9e1f565a5f5d0bbf (patch) | |
| tree | 15109e8203732f740ef78e214f2da7a75fd62812 | |
| parent | 33a2c6bb6d665e167128aa6a92a005808e3a3a18 (diff) | |
| download | perlweeklychallenge-club-0e833cab3416554823901d8a9e1f565a5f5d0bbf.tar.gz perlweeklychallenge-club-0e833cab3416554823901d8a9e1f565a5f5d0bbf.tar.bz2 perlweeklychallenge-club-0e833cab3416554823901d8a9e1f565a5f5d0bbf.zip | |
ch-1
using sprintf '%b'
| -rw-r--r-- | challenge-049/dave-jacoby/perl/ch-1.pl | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-049/dave-jacoby/perl/ch-1.pl b/challenge-049/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..1d0ca0cc81 --- /dev/null +++ b/challenge-049/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use utf8; +use feature qw{ postderef say signatures state switch }; +no warnings + qw{ experimental::postderef experimental::smartmatch experimental::signatures }; + +use Carp; + +my $number = int $ARGV[0]; +croak 'Not a positive number' unless $number > 0; + +my $smallest = smallest_multiple( $number); + +say qq{The smallest multiple of $number containing just 0 and 1 is $smallest}; +exit; + + +sub smallest_multiple( $n ) { + my $dec = 1; + while ( 1 ) { + my $bin = sprintf '%b', $dec; + return $bin if $bin % $n == 0 ; + $dec++; + } +} + +__DATA__ +Smallest Multiple + +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. + +For example: +For given number 55, the smallest multiple is 110 +consisting of digits 0 and 1. |
