Camel
Peter
Peter Campbell Smith

Fun with integers

Weekly challenge 320 — 5 May 2025

Week 320: 5 May 2025

Task 2

Task — Sum difference

You are given an array of positive integers. Write a script to return the absolute difference between the digit sum and the element sum of the given array.

Examples


Example 1
Input: @ints = (1, 23, 4, 5)
Output: 18
Element sum: 1 + 23 + 4 + 5 => 33
Digit sum: 1 + 2 + 3 + 4 + 5 => 15
Absolute difference: | 33 - 15 | => 18

Example 2
Input: @ints = (1, 2, 3, 4, 5)
Output: 0
Element sum: 1 + 2 + 3 + 4 + 5 => 15
Digit sum: 1 + 2 + 3 + 4 + 5 => 15
Absolute difference: | 15 - 15 | => 0

Example 3
Input: @ints = (1, 2, 34)
Output: 27
Element sum: 1 + 2 + 34 => 37
Digit sum: 1 + 2 + 3 + 4 => 10
Absolute difference: | 37 - 10 | => 27

Analysis

Each sum can be done in one line. Summing the elements is easy:

$elements += $_ for @integers;

but summing the digits is a little trickier:

$digits = eval(join('+', split('', join('', @integers))));

This says:

  • Start with, say, (1, 2, 34, 5)
  • Join all the elements together as one long string (12345)
  • Split that into an array of single characters (1, 2, 3, 4, 5)
  • Join that with '+' signs into a string (1+2+3+4+5)
  • eval that string (15)

It isn't necessary to take the absolute value of $elements - $digits because $digits can never exceed $elements. An element which is a single digit will contribute the same to each sum, and one with, say, two digits will contribute 10 times the first digit plus one times the second, which is always more that sum of the individual digits.

Try it 

Try running the script with any input:



example: 1, 23, 456, 7890

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2025-05-05
use utf8;     # Week 320 - task 2 - Sum difference
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

sum_difference(1, 23, 4, 5);
sum_difference(1, 2, 3, 4, 5);
sum_difference(1, 2, 34);

# larger example
my @nums;
push @nums, int(rand(1000)) for 0 .. 99;
sum_difference(@nums);

sub sum_difference {
    
    my (@integers, $elements, $digits);
    @integers = @_;
    
    # make the desired sums
    $elements += $_ for @integers;
    $digits = eval(join('+', split('', join('', @integers))));
    
    say qq[\nInput:  (] . join(', ', @integers) . q[)];
    say qq[Output: ] . ($elements - $digits) . qq[ (elements = $elements, digits = $digits)];
}

Output


Input:  (1, 23, 4, 5)
Output: 18 (elements = 33, digits = 15)

Input:  (1, 2, 3, 4, 5)
Output: 0 (elements = 15, digits = 15)

Input:  (1, 2, 34)
Output: 27 (elements = 37, digits = 10)

Input:  (906, 610, 334, 176, 870, 524, 795, 709, 426, 802,
   108, 307, 409, 603, 480, 460, 708, 653, 856, 792, 690,
   448, 586, 265, 772, 778, 623, 841, 283, 411, 863, 102,
   886, 598, 781, 606, 91, 334, 750, 445, 348, 690, 908,
   434, 964, 748, 227, 95, 633, 496, 639, 539, 726, 108,
   28, 994, 972, 352, 249, 30, 847, 808, 681, 67, 473,
   551, 411, 965, 85, 956, 693, 997, 330, 136, 872, 315,
   441, 932, 202, 697, 465, 467, 743, 525, 674, 564, 613,
   206, 222, 132, 750, 640, 435, 350, 22, 626, 367, 677,
   45, 823)
Output: 52317 (elements = 53666, digits = 1349)

 

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