Peter
Peter Campbell Smith

Pandigitals and term counts

Weekly challenge 134 — 11 October 2021

Week 134: 11 Oct 2021

Task 1

Task — Pandigital numbers

Write a script to generate first 5 Pandigital Numbers in base 10. Wikipedia says: A pandigital number is an integer that in a given base has among its significant digits each digit used in the base at least once.

Examples


Example 1
1023456789 is the first pandigital number base 10.

Analysis

The first such number is 1023456789 as we can't start with 0 which isn't 'significant'.

The simplest way to find the next 4 is simply to test every subsequent integer until all 5 are found. Finding the first 1000 pandigitals using this method completes in a few seconds.

Script


#!/usr/bin/perl

# Peter Campbell Smith - 2021-10-13
# PWC 134 task 1

use v5.20;
use warnings;

my ($test, $count, $limit);

$limit = 20;        # how many do we want
$count = 0;        # how many have we found

# start with the known first one and test subsequent integers
say qq[\nPandigital numbers:];
$test = 1023456789;
while (1) {
    if ($test =~ m|0| and $test =~ m|1| and $test =~ m|2| and $test =~ m|3| and $test =~ m|4|
        and $test =~ m|5| and $test =~ m|6| and $test =~ m|7| and $test =~ m|8| and $test =~ m|9|) {
        say qq{pd[} . ++$count . qq{] = $test};
        last if $count == $limit;
    }
    $test ++;
}

Output


Pandigital numbers:
pd[1] = 1023456789
pd[2] = 1023456798
pd[3] = 1023456879
pd[4] = 1023456897
pd[5] = 1023456978
pd[6] = 1023456987
pd[7] = 1023457689
pd[8] = 1023457698
pd[9] = 1023457869
pd[10] = 1023457896
pd[11] = 1023457968
pd[12] = 1023457986
pd[13] = 1023458679
pd[14] = 1023458697
pd[15] = 1023458769
pd[16] = 1023458796
pd[17] = 1023458967
pd[18] = 1023458976
pd[19] = 1023459678
pd[20] = 1023459687

 

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