Robbie Hatley's Solutions To The Weekly Challenge #241
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:
This week (2023-10-29 through 2023-11-04) is weekly challenge #241.
Task 241-1 is as follows:
PROBLEM DESCRIPTION: Task 1: Arithmetic Triplets Submitted by: Mohammad S Anwar Given an array "nums" of 3-or-more integers in increasing order, and a positive integer "diff", write a script to find the number of unique "Arithmetic Triplets", where an "Arithmetic Triplet" is a trio of numbers from nums which satisfies these rules: a) i < j < k b) nums[j] - nums[i] == diff c) nums[k] - nums[j] == diff Example 1: Input: @nums = (0, 1, 4, 6, 7, 10), $diff = 3 Output: 2 Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3. Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3. Example 2: Input: @nums = (4, 5, 6, 7, 8, 9), $diff = 2 Output: 2 (0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2. (1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.
To solve this problem, I could resort to using CPAN module Math::Combinatorics as I usually do any time a problem involves permutations or combinations, but in this case that seems like overkill to me, so I'll use triple-nested three-part loops instead. I'll check to make sure all array elements are integers, but I won't enforce the restriction that they be in increasing order, because there's no need: it wouldn't make my program malfunction, and subsequences of non-monotonic sequences can still be arithmetic triplets. For example, (17, -32, 53, -34, 47, -36, 14) contains the arithmetic triplet (-32, -34, -36) with period -2.
Robbie Hatley's Solution to The Weekly Challenge 241-1
Task 241-2 is as follows:
Task 2: Prime Order Submitted by: Mohammad S Anwar Given an array of unique positive integers greater than 2, write a script to sort them in ascending order of the count of their prime factors, tie-breaking by ascending value. Example 1: Input: @int = (11, 8, 27, 4) Output: (11, 4, 8, 27)) Prime factors of 11 => 11 Prime factors of 4 => 2, 2 Prime factors of 8 => 2, 2, 2 Prime factors of 27 => 3, 3, 3
To solve this problem requires finding all prime factors of any given positive integer > 2. Fortunately, I know a way to do that which doesn't require finding any prime numbers at all! The algorithm is like this:
- First, find-and-divide-out all copies of 2 from number "$x".
- Set variable "$divisor" to 3.
- Loop steps 3,4,5 while $x is greater than 1.
- Find-and-divide-out all copies of $divisor from $x.
- Increase $divisor by 2.
That works because none of the divisors of $x we find will ever be non-prime, because all non-prime numbers have prime divisors, all of which are lesser than themselves, so THOSE divisors will already have been found-and-divided-out from $x.
Robbie Hatley's Solution to The Weekly Challenge 241-2
That's it for 241; see you on 242!
Comments
Post a Comment