Peter’s blog ✴ Week 400 ✴ 16 November 2026

THE WEEKLY CHALLENGE
A gap in my genes

The Perl Camel

Task 1

Largest interval

You are given a positive integer.

Write a script that generates a sequence of integers from 1 to the given positive integer, filtering out any numbers that contain repeating digits (e.g., 11, 99, 100, and 101 are excluded) and return the biggest jump.

Examples


Example 1
Input: $int = 20
Output: 2
Biggest jump between 10 and 12.

Example 2
Input: $int = 110
Output: 4
Biggest jump between 98 and 102.

Example 3
Input: $int = 500
Output: 11
Biggest jump between:
a) 109 and 120 => 11
b) 219 and 230 => 11
c) 329 and 340 => 11
d) 439 and 450 => 11

Example 4
Input: $int = 2026
Output: 105
Biggest jump between 1098 and 1203.

Example 5
Input: $int = 10000
Output: 105
The largest gap is 105, between 1098 and 1203. And again between 8796
   and 8901.

Analysis

The only real decision in this challenge is how to identify a number with a repeating digit quickly. I settled on:

$i =~ m|(\d).*\1|

which handles the range 1 - 1,000,000 within a couple of seconds.

If we were called on to handle much larger numbers I would look for a more sophisticated loop: for example jumping from 11,000 to 12,000 because all the intervening numbers start with 11.

I struggle to think of a real-life use case for this!

Try it 

Your input:



eg: 12345 - between 1 and 1e7

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2026-11-16
use utf8;     # Week 400 - task 1 - Largest interval
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

largest_interval(110);
largest_interval(1100);
largest_interval(11100);
largest_interval(1000000);

sub largest_interval {
    
    my ($integer, $best_jump, $before, $i, $this_jump, $explain);
    
    # initialise
    $integer = shift;
    $best_jump = 0;
    $before = 1;
    $explain = '';
    
    # loop over possible integers
    for $i (1 .. $integer) {
        
        # skip if repeated digit
        next if $i =~ m|(\d).*\1|;
        
        # inspect this jump
        $this_jump = $i - $before;
        
        # new winner
        if ($this_jump > $best_jump) {
            $explain = qq[$this_jump: [$before, $i]];
            $best_jump = $this_jump
            
        # equal to winner
        } elsif ($this_jump == $best_jump) {
            $explain .= qq[, [$before, $i]];
        }
        $before = $i;
    }
    
    say qq[\nInput:  $integer];
    say qq[Output: jump = $explain];
}

16 lines of code

Output from script


Input:  110
Output: jump = 4: [98, 102]

Input:  1100
Output: jump = 36: [987, 1023]

Input:  11100
Output: jump = 358: [9876, 10234]

Input:  1000000
Output: jump = 10469: [109876, 120345], [879654, 890123]

 

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