Peter
Peter Campbell Smith

Permutable and
reversible

Weekly challenge 176 — 1 August 2022

Week 176 - 1 Aug 2022

Task 1

Task — Permuted multiples

Write a script to find the smallest positive integer x such that x, 2x, 3x, 4x, 5x and 6x are permuted multiples of each other.

For example, the integers 125874 and 251748 are permutated multiples of each other as 251784 = 2 x 125874 and also both have the same digits but in different order.

Analysis

My immediate thought was that the answer is 142857, which I imagine most of us recognise as the repeating decimal sequence in 1/7 (and 2/7, 3/7 ... 6/7). So I could take a punt on a valid solution being: say 142857;

But is there a smaller answer? I spent some time thinking whether I could prove that there isn't, but failed. So I wrote a short script to test it, and indeed that is the right answer.

I also ran the script up to a million and didn't find another answer, so perhaps it is unique?

Script


#!/usr/bin/perl

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

use v5.28;
use utf8;
use warnings;

my ($j, $j_sorted, $j_length, $k, $jk, );

# loop from 10 to <big>
J: for $j (10 .. 1000000) {
    
    # sort the digits of $j and get its length
    $j_sorted = sorted($j);
    $j_length = length($j);
    
    # test 2x, 3x, 4x, 5x, 6x products
    for $k (2 .. 6) {
        $jk = $j * $k;
        
        # $j is no good if the product has more digits than $j, or they aren't the same digits
        next J if (length($jk) > $j_length or sorted($jk) ne $j_sorted);
    }
    
    # and if all that passes, we have the answer
    say qq[Output: $j];
    exit;
}

sub sorted {
    
    # sorts the characters of a string
    return join('', sort(split(//, $_[0])));
}
    

Output


Output: 142857