blob: 7acfb04260e863e8b6a7e7c91bb591eeaa2fc3de (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#! /usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use List::Util qw/sum/;
use Getopt::Long;
my $verbose = 0;
GetOptions("verbose" => \$verbose);
my $N = $ARGV[0] // die "Specify a positive integer";
die "Not a positive integer" unless $N =~ /^[1-9]\d*$/;
my $ones = sum(split(//, sprintf('%b', $N)));
if ($verbose)
{
say ": Initial value: $N (binary: ", sprintf('%b', $N) , ")";
say ": - Number of one bits: $ones";
}
while ($N += 1)
{
say ": Candidate $N (binary: ", sprintf('%b', $N), ")" if $verbose;
if (sum(split(//, sprintf('%b', $N))) == $ones)
{
say $N;
exit;
}
}
|