Peter’s blog ✴ Week 357 ✴ 19 January 2026

THE WEEKLY CHALLENGE
Converging on fractions

The Perl Camel

Task 1

Kaprekar constant

Write a function that takes a 4-digit integer and returns how many iterations are required to reach Kaprekar’s constant (6174). For more information about Kaprekar's Constant please see the Wikipedia page.

Examples


Example 1
Input: $int = 3524
Output: 3
Iteration 1: 5432 - 2345 = 3087
Iteration 2: 8730 - 0378 = 8352
Iteration 3: 8532 - 2358 = 6174

Example 2
Input: $int = 6174
Output: 0

Example 3
Input: $int = 9998
Output: 5
Iteration 1: 9998 - 8999 = 0999
Iteration 2: 9990 - 0999 = 8991
Iteration 3: 9981 - 1899 = 8082
Iteration 4: 8820 - 0288 = 8532
Iteration 5: 8532 - 2358 = 6174

Example 4
Input: $int = 1001
Output: 4
Iteration 1: 1100 - 0011 = 1089
Iteration 2: 9810 - 0189 = 9621
Iteration 3: 9621 - 1269 = 8352
Iteration 4: 8532 - 2358 = 6174

Example 5
Input: $int = 9000
Output: 4
Iteration 1: 9000 - 0009 = 8991
Iteration 2: 9981 - 1899 = 8082
Iteration 3: 8820 - 0288 = 8532
Iteration 4: 8532 - 2358 = 6174

Example 6
Input: $int = 1111
Output: -1
The sequence does not converge on 6174, so return -1.

Analysis

Kaprekar
Dattatreya Ramchandra Kaprekar

This is an interesting constant which I don't remember coming across before. Kaprekar was an Indian recreational mathematician who worked as a school maths teacher. He said of himself: 'A drunkard wants to go on drinking wine to remain in that pleasurable state. The same is the case with me in so far as numbers are concerned.'

I can sympathise with that sentiment.

The challenge is easily solved simply by coding the steps given, the only minor issue being the need to left pad a number with fewer than 4 digits with zeroes.

The literature says that the maximum number of iterations required is 7, and 9875 is an example of one that requires 7 steps.

Perl Weekly’s review

from PW issue 757

A thorough explanation of the solution (both tasks) is provided in the post. The Perl code included is easy to read and closely adheres to the descriptions of each problem. Furthermore, the code has been written such that it handles 'non-convergence' where applicable, with clear and logical outputs as well as analyses of each step helping the reader to learn about the algorithms and their correctness.

Try it 

Try running the script with any input:



example: 2026

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2026-01-19
use utf8;     # Week 357 - task 1 - Kaprekar constant
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

kaprekar_constant(3524);
kaprekar_constant(6174);
kaprekar_constant(9998);
kaprekar_constant(1001);
kaprekar_constant(9000);
kaprekar_constant(1111);

sub kaprekar_constant {
    
    my ($k, $asc, $desc, $n, $explain);
    
    # initialise
    $k = $_[0];
    $explain = qq[$k → ];
    $n = 0;
    
    # repeatedly reverse and subtract
    while ($k != 6174 and $k != 0) {

        # sort digits
        $asc = sprintf('%04d', join('', sort(split('', $k))));
        
        # reverse digits and subtract
        $desc = reverse($asc);
        $k = abs($asc - $desc);
        
        # record
        $n ++;
        $explain .= sprintf('%04d', $k) . ' → ';
    }
    
    say qq[\nInput:  $_[0];];
    say qq[Output: ] . ($k ? (qq[$n ∵ ] . substr($explain, 0, -3)) : -1);
}

13 lines of code

Output from script


Input:  3524;
Output: 3 ∵ 3524 → 3087 → 8352 → 6174

Input:  6174;
Output: 0 ∵ 6174

Input:  9998;
Output: 5 ∵ 9998 → 0999 → 8991 → 8082 → 8532 → 6174

Input:  1001;
Output: 4 ∵ 1001 → 1089 → 9621 → 8352 → 6174

Input:  9000;
Output: 4 ∵ 9000 → 8991 → 8082 → 8532 → 6174

Input:  1111;
Output: -1

 

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