From 5f65d62528e942d20397e63c2adeb3bbbb380f5c Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 18 May 2021 09:22:14 +0200 Subject: Solution to task 1 --- challenge-113/jo-37/perl/ch-1.pl | 92 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 challenge-113/jo-37/perl/ch-1.pl diff --git a/challenge-113/jo-37/perl/ch-1.pl b/challenge-113/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..2b9be921d4 --- /dev/null +++ b/challenge-113/jo-37/perl/ch-1.pl @@ -0,0 +1,92 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use experimental qw(signatures postderef); + +our $examples; + +run_tests() if $examples; # does not return + +die < 0 and 10 * d <= n < 10 * (d + 1) the number starts +# with the digit d and thus is a solution itself. +# - For all d > 0 and 10 * (d + 1) <= n there is a number m with +# 10 * d <= m < 10 * (d + 1) starting with d and n - m is a multiple +# of d. Thus n is representable as a sum of numbers that have the +# digit d in their decimal representation. +# - For d = 0 and 100 <= n an analogous consideration is applicable when +# taking d=10 instead. As leading zeros do not count, with the taken +# modification the second digit becomes zero. +# - The remaining cases are n < 10 * d with the modified d. Further +# analysis can be applied to these, e.g checking the special cases +# where d is one, even or five or is already occurring in n. However, +# skipping any refinements and performing a brute force approach on +# this small solution space instead. + +sub rep_int ($n, $d) { + $d ||= 10; + return 1 if $n >= $d * 10; + + # keys are strings, using the numeric values. + my %sum = (0 => 0); + + # All numbers containing the digit $d. + for (my $num = $d; $num <= $n; $num += 10) { + # All sums found so far. + for my $sum (values %sum) { + # New sums arise from the current sum plus multiples of the + # current number. + for (my $new = $sum + $num; $new <= $n; $new += $num) { + return 1 if $new == $n; + $sum{$new} = $new; + } + } + } + + # Not found. + 0; +} + + + + +### Examples and tests + +sub run_tests { + + is rep_int(25, 7), F(), 'example 1'; + is rep_int(24, 7), T(), 'example 2'; + + done_testing; + exit; +} -- cgit