Peter’s blog ✴ Week 384 ✴ 27 July 2026

THE WEEKLY CHALLENGE
Bases and bits

The Perl Camel

Task 1

Base n

You are given a number and a base integer. Write a script to convert the given number in the given base integer.

Examples


Example 1
Input: $num = 42, $base = 2
Output: 101010

Example 2
Input: $num = 15642094, $base = 16
Output: EEADEE

Example 3
Input: $num = 493, $base = 8
Output: 755

Example 4
Input: $num = 2228519, $base = 36
Output: 1BRJB
Base 36 uses numbers 0-9 and letters A-Z.

Example 5
Input: $num = 123456789, $base = 64
Output: 7MyqL
Base 64 (using 0-9, A-Z, a-z, and extra symbols like + and /)

Analysis

The work for this is in the while loop.

While $number > 0:

  • calculate $number % $base (% is Perl's modulo operator, returning the remainder when the left hand value is divided by the right hand)
  • Find the digit in $digits representing the remainder
  • Append it to the beginning of $result
  • Subtract it from $number and divide by number by $base
  • ... and repeat until $number is zero.

The string $digits contains 87 characters, so this can cope with any base up to 87. Also, $number must be representable in Perl as an integer, so in most environments no more than 2 ** 63 - 1.

Try it 

Your input:



eg: 4567



eg: 16 (must be in 2 .. 87)

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/384/1

use v5.26;    # The Weekly Challenge - 2026-07-27
use utf8;     # Week 384 - task 1 - Base n
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

base_n(493, 8);
base_n(15642094, 16);
base_n(42, 2);
base_n(1832913631, 87);
base_n(71 ** 9, 71);
base_n(100000, 27);
base_n(100000, 99);
base_n(4283337, 55);

sub base_n {

    my ($base, $number, $digits, $digit, $result);
    
    # initialise
    ($number, $base) = @_;
    say qq[\nInput:  \$number = $number base 10, new \$base = $base];
    $digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
        'abcdefghijklmnopqrstuvwxyzαβγδεζηθικλμνξοπρςστυφχψω';
    if ($base < 2 or $base > length($digits)) {
        say qq[Output: base must be in 2 .. ] . length($digits);
        return;
    }
    $result = '';
    
    # strip digits 1 at a time
    while ($number) {
        $digit = $number % $base;
        $result = substr($digits, $digit, 1) . $result;
        $number = ($number - $digit) / $base;
     }
    say qq[Output: $result base $base];
}

15 lines of code

Output from script


Input:  $number = 493 base 10, new $base = 8
Output: 755 base 8

Input:  $number = 15642094 base 10, new $base = 16
Output: EEADEE base 16

Input:  $number = 42 base 10, new $base = 2
Output: 101010 base 2

Input:  $number = 1832913631 base 10, new $base = 87
Output: Vωdqζ base 87

Input:  $number = 45848500718449031 base 10, new $base = 71
Output: 1000000000 base 71

Input:  $number = 100000 base 10, new $base = 27
Output: 524J base 27

Input:  $number = 100000 base 10, new $base = 99
Output: base must be in 2 .. 87

Input:  $number = 4283337 base 10, new $base = 55
Output: Perl base 55

 

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