DEV Community

Simon Green
Simon Green

Posted on

2 2

Weekly Challenge 114

Challenge, My solution

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
Enter fullscreen mode Exit fullscreen mode

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 1s).

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
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

If you found this post useful, please drop a ❤️ or leave a kind comment!

Okay