TASK #1 › Next Palindrome Number
Task
You are given a positive integer $N
.
Write a script to find out the next Palindrome Number higher than the given integer $N
.
My solution
This task is relatively straight forward. I first check that a positive integer is supplied. I then have a while loop that increases by 1 each time. We have a found a palindrome number where $number == reverse $number
is true.
Examples
$ ./ch-1.pl 1234
1331
$ ./ch-1.pl 999
1001
TASK #2 › Higher Integer Set Bits
Task
You are given a positive integer $N
.
Write a script to find the next higher integer having the same number of 1 bits in binary representation as $N
.
My solution
This is almost identical to the first task, except the match condition is different. We can quickly find out the number of set bits using sprintf('%b', $number) =~ tr/1/1/
(convert to a binary and count the number of 1
s).
Like with the first task, I have a while loop that increases by 1 each time. Once we have found the correct number, I display that value and exit.
Examples
$ ./ch-2.pl 3
5
$ ./ch-2.pl 12
17
Top comments (0)