# Challenge 261 tasks: Summing Up Two Short Challenge Elements **Challenge 261 solutions in Perl by Matthias Muth** This week's challenges are of the shorter type. So I am trying to keep the description short as well. ## Task 1: Element Digit Sum > You are given an array of integers, @ints.
> Write a script to evaluate the absolute difference between element and digit sum of the given array.
>
> Example 1
> Input: @ints = (1,2,3,45)
> Output: 36
> Element Sum: 1 + 2 + 3 + 45 = 51
> Digit Sum: 1 + 2 + 3 + 4 + 5 = 15
> Absolute Difference: | 51 - 15 | = 36
>
> Example 2
> Input: @ints = (1,12,3)
> Output: 9
> Element Sum: 1 + 12 + 3 = 16
> Digit Sum: 1 + 1 + 2 + 3 = 7
> Absolute Difference: | 16 - 7 | = 9
>
> Example 3
> Input: @ints = (1,2,3,4)
> Output: 0
> Element Sum: 1 + 2 + 3 + 4 = 10
> Digit Sum: 1 + 2 + 3 + 4 = 10
> Absolute Difference: | 10 - 10 | = 0
>
> Example 4
> Input: @ints = (236, 416, 336, 350)
> Output: 1296
Perl's implicit type conversions make it simple to split up the numbers into digits. All we have to do then is to compute the difference of the sum of the numbers and the sum of the digits (using `sum` from `List::Util`), and return the absolute value using the builtin `abs` function. Nice and short. ```perl use v5.36; use List::Util qw( sum ); sub element_digit_sum( @ints ) { return abs( sum( @ints ) - sum( map split( "", $_ ), @ints ) ); } ``` ## Task 2: Multiply by Two > You are given an array of integers, @ints and an integer \$start.
> Write a script to do the followings:
> a) Look for \$start in the array @ints, if found multiply the number by 2
> b) If not found stop the process otherwise repeat
>
> In the end return the final value.
>
> Example 1
> Input: @ints = (5,3,6,1,12) and \$start = 3
> Output: 24
> Step 1: 3 is in the array so 3 x 2 = 6
> Step 2: 6 is in the array so 6 x 2 = 12
> Step 3: 12 is in the array so 12 x 2 = 24
> 24 is not found in the array so return 24.
>
> Example 2
> Input: @ints = (1,2,4,3) and \$start = 1
> Output: 8
> Step 1: 1 is in the array so 1 x 2 = 2
> Step 2: 2 is in the array so 2 x 2 = 4
> Step 3: 4 is in the array so 4 x 2 = 8
> 8 is not found in the array so return 8.
>
> Example 3
> Input: @ints = (5,6,7) and \$start = 2
> Output: 2
> 2 is not found in the array so return 2.
Whenever I read 'exists', or 'find in an array', the words 'USE A HASH' start blinking in front of my inner eye. So I obey.
One of several possible Perl idioms to create a lookup hash containing each number as a key, then multiplying by two in a loop as long as the current product exists in that hash, and then returning the product. Nice and short as well. ```perl use v5.36; sub multiply_by_two( $ints, $start ) { my %set_of_ints = map { ( $_ => 1 ) } $ints->@*; my $product = $start; $product *= 2 while exists $set_of_ints{$product}; return $product; } ``` #### **Thank you for the challenge!**