Peter’s blog ✴ Week 351 ✴ 8 December 2025

THE WEEKLY CHALLENGE
Fun with arrays

The Perl Camel

Task 2

Arithmetic progression

You are given an array of numbers. Write a script to return true if the given array can be re-arranged to form an arithmetic progression, otherwise return false. A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.

Examples


Example 1
Input: @num = (1, 3, 5, 7, 9)
Output: true
Already AP with common difference 2.

Example 2
Input: @num = (9, 1, 7, 5, 3)
Output: true
The given array re-arranged like (1, 3, 5, 7,
   9) with common difference 2.

Example 3
Input: @num = (1, 2, 4, 8, 16)
Output: false
This is geometric progression and not arithmetic 
   progression.

Example 4
Input: @num = (5, -1, 3, 1, -3)
Output: true
The given array re-arranged like (-3, -1, 1, 3,
   5) with common difference 2.

Example 5
Input: @num = (1.5, 3, 0, 4.5, 6)
Output: true
The given array re-arranged like (0, 1.5, 3, 4.5,
   6) with common difference 1.5.

Analysis

As with task 1, I have taken the view that an arithmetic progression requires at least 3 elements.

That said, I do the obvious and sort the array, and then check that the increments between successive elements are the same.

This will always result in a positive increment, even for input such as (-1, -2, -3, -4) which will get sorted to (-4, -3, -2, -1) with an increment of 1.

Perl Weekly’s review

from Perl Weekly issue 751

This is a well-structured, practical implementation with good documentation and error handling. Peter demonstrates solid Perl programming practices while making reasonable design decisions based on their interpretation of the problem requirements.

Try it 

Try running the script with any input:



example: 13, 25, 37, 49

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2025-12-08
use utf8;     # Week 351 - task 2 - Arithmetic progression
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

arithmetic_progression(9, 1, 7, 5, 3);
arithmetic_progression(-1, -2, -3, -4);
arithmetic_progression(-4, -3, -2, -1);
arithmetic_progression(1e6, 3e6, 5e6, 7e6, 9e6);
arithmetic_progression(111);
arithmetic_progression(111, 222);
arithmetic_progression(111, 222, 333);
arithmetic_progression(11/7, 14/7, 17/7, 29/7);

sub arithmetic_progression {
    
    my (@sorted, $increment, $j);
    
    # initialise
    say qq[\nInput:  (] . join(', ', @_) . ')';
    
    # need at lleast 3 elements
    if (@_ < 3) {
        say qq[Output: indeterminate - array too short];
        return;
    }
        
    # check for arithmetic progression
    @sorted = sort {$a <=> $b} @_;
    $increment = $sorted[1] - $sorted[0];
    
    for $j (1 .. @sorted - 2) {
        next if $sorted[$j + 1] = $sorted[$j] + $increment;
        say qq[Output: false as $sorted[$j + 1] - $sorted[$j] != $increment];
        return;
    }
    say qq[Output: true with increment of $increment];

}

13 lines of code

Output from script


Input:  (9, 1, 7, 5, 3)
Output: true with increment of 2

Input:  (-1, -2, -3, -4)
Output: true with increment of 1

Input:  (-4, -3, -2, -1)
Output: true with increment of 1

Input:  (1000000, 3000000, 5000000, 7000000, 9000000)
Output: true with increment of 2000000

Input:  (111)
Output: indeterminate - array too short

Input:  (111, 222)
Output: indeterminate - array too short

Input:  (111, 222, 333)
Output: true with increment of 111

Input:  (1.57142857142857, 2, 2.42857142857143,
   4.14285714285714)
Output: true with increment of 0.428571428571429

 

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