Peter’s blog ✴ Week 395 ✴ 12 October 2026

THE WEEKLY CHALLENGE
Complex words

The Perl Camel

Task 1

Complex number

You are given two complex numbers as strings. Write a script to return the multiplication of the given complex numbers, using the formula:

(a + bi) x (c + di) = (ac - bd) + (ad + bc)i

Examples


Example 1
Input: $num1 = '1+1i', $num2 = '1+1i'
Output: '0+2i'

Example 2
Input: $num1 = '1+(-1)i', $num2 = '1+(-1)i'
Output: '0+(-2)i'

Example 3
Input: $num1 = '0+3i', $num2 = '0+4i'
Output: '-12+0i'

Example 4
Input: $num1 = '3+4i', $num2 = '3+(-4)i'
Output: '25+0i'

Example 5
Input: $num1 = '50+(-20)i', $num2 = '-10+30i'
Output: '100+1700i'

Analysis

Relatively few scientists deal daily with complex numbers, although I was once one of them. There were then debates in the community about how to write complex numbers, especially when the real or complex components are negative or zero. I think the informal convention used most often are that in running text one writes, for example:

5 or 5 + 4i or -5 or -5 + 4i or -5 - 4i or 4i or -4i

Some prefer to omit the spaces around the middle '+' or '-'.

For columnar text, it's common to make the divider between the real and imaginary parts always a '+', so that the columns line up:

 1.234 +   5.678i
12.345 + -16.789i

I don't think I've ever seen the format of Example 2 – 0+(-2)i – used, where I would have simply written -2i or maybe 0 - 2i if I was particularly drawing attention to the real part being zero.

So my solution:

  • accepts the example inputs or any of those shown above, and
  • outputs using the 123 + 456i or 123 + -456i conventions.

It also accepts coefficients with decimal fractions such as 12.3 + 45.6i, which are common in scientific work but perhaps less so in mathematics.

As regards the coding, note that I have extracted $aa and $bb by simply looking for an optional minus sign followed by any string of digits and '.'s, and ignoring brackets, spaces and plus signs. That is over-permissive in that it would allow, for example, more than one decimal point, but of course in production code I would always sanity-check the inputs.

Also, I have, perhaps over-cautiously, used $aa and $bb rather than $a and $b because of their special meaning in sort.

Try it 

Your input:



eg: 4 + 5i



eg: 6 - 7i

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2026-10-12
use utf8;     # Week 395 - task 1 - Complex number
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

complex_number('1 + 1i', '1 + 1i');
complex_number('1+(-1)i', '1+(-1)i');
complex_number('0+3i', '0+4i');
complex_number('3+4i', '3+(-4)i');
complex_number('50+(-20)i', '-10+30i');
complex_number('0.1 + 0.2i', '0.3 - 0.4i');

sub complex_number {
    
    my ($aa, $bb, $cc, $dd, $prod);
        
    # extract the 4 coefficients
    ($aa, $bb) = $_[0] =~ m|(-?[\d\.]+).*?(-?[\d\.]+)|;
    ($cc, $dd) = $_[1] =~ m|(-?[\d\.]+).*?(-?[\d\.]+)|;
    
    # calculate the product
    $prod = ($aa * $cc - $bb * $dd) . ' + ' .
            ($aa * $dd + $bb * $cc) . 'i';
    
    say qq[\nInput:  ($_[0]) × ($_[1])];
    say qq[Output: $prod];
}

8 lines of code

Output from script


Input:  (1 + 1i) × (1 + 1i)
Output: 0 + 2i

Input:  (1+(-1)i) × (1+(-1)i)
Output: 0 + -2i

Input:  (0+3i) × (0+4i)
Output: -12 + 0i

Input:  (3+4i) × (3+(-4)i)
Output: 25 + 0i

Input:  (50+(-20)i) × (-10+30i)
Output: 100 + 1700i

Input:  (0.1 + 0.2i) × (0.3 - 0.4i)
Output: -0.05 + 0.1i

 

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