#!/usr/bin/perl use strict; if (@ARGV < 2) { print STDERR "\nUsage: $0 firstnumber lastnumber\n\n"; } my ($beg, $end) = @ARGV; my ($test, $this, $that); for $test ($beg..$end) { ($this, $that) = factors($test); if ($this ne '') { print "$test = $this * $that\n"; } } #----------------------------------------------------------- sub factors { my ($target) = @_; my ($order, $olist); my ($split, $slist); $olist = orderings($target); for $order (@{$olist}) { #print "$order\n"; $slist = splittings($order); for $split (@{$slist}) { #print "$split->[0] $split->[1]\n"; if ($split->[0] * $split->[1] == $target) { return ($split->[0], $split->[1]); } } } } #----------------------------------------------------------- sub splittings { my ($num) = @_; my (@digits) = split('', $num); my (@list, @useds); while (@digits > 1) { push(@useds, shift(@digits)); push (@list, [join('', @useds), join('', @digits)]); } return \@list; } #----------------------------------------------------------- sub orderings { my ($num) = @_; my (@digits) = split('', $num); my (@list, $sublists, $sub, $this); if (@digits == 1) { return [$digits[0]]; } for (1..@digits) { $this = shift(@digits); $sublists = orderings(join('', @digits)); for $sub (@{$sublists}) { push(@list, "$this$sub"); } push(@digits, $this); } return \@list; }