From 0e833cab3416554823901d8a9e1f565a5f5d0bbf Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Wed, 26 Feb 2020 22:46:52 -0500 Subject: ch-1 using sprintf '%b' --- challenge-049/dave-jacoby/perl/ch-1.pl | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 challenge-049/dave-jacoby/perl/ch-1.pl 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. -- cgit