Peter
Peter Campbell Smith

Permutable and
reversible

Weekly challenge 176 — 1 August 2022

Week 176: 1 Aug 2022

Task 2

Task — Reversible numbers

Write a script to find out all Reversible Numbers below 100. A number is said to be a reversible if sum of the number and its reverse have only odd digits.

Examples


Example 1:
36 is reversible number as 36 + 63 = 99 ie all digits 
are odd.

Example 2:
17 is not reversible as 17 + 71 = 88, none of the digits 
is odd.

Analysis

We are looking for a number written as AB such that AB + BA has only odd digits.

The sum is 10A + B + 10B + A, which is 11 (A + B). No single digit number will qualify, so all we need to do is look from 10 to 99 checking that 11 (A + B) doesn't contain [02468].

Script


#!/usr/bin/perl

# Peter Campbell Smith - 2022-08-01
# PWC 176 task 2

use v5.28;
use utf8;
use warnings;

my ($j, $result);

# loop over all 2-digit numbers
for $j (10 .. 99) {
    $j =~ m|(\d)(\d)|;
    
    # check sum of digits * 11
    $result .= "$j, " unless (11 * ($1 + $2)) =~ m|[02468]|;
}

# output
$result = substr($result, 0, -2);
say qq[\nOutput:];
say $1 while $result =~ m|(.{0,36})|g;

Output


Output:
10, 12, 14, 16, 18, 21, 23, 25, 27, 
30, 32, 34, 36, 41, 43, 45, 50, 52, 
54, 61, 63, 70, 72, 81, 90


 

Any content of this website which has been created by Peter Campbell Smith is in the public domain