The Weekly Challenge 368

Digit Deletion & Prime Factor Counting!

Original Challenge Link

Task 1: Make it Bigger

"Delete to Dominate: Maximise by Removing One!"

Given a string number and a digit character, we must remove exactly one occurrence of that digit to make the resulting decimal number as large as possible.

The Strategy: Scan from left to right for the first occurrence of the target digit where the next digit is greater. Removing that occurrence lets a bigger digit move into a higher place value. If no digit is followed by a larger one, remove the rightmost occurrence.
Perl Implementation
sub make_it_bigger {
    my ($number, $digit) = @_;
    my @chars = split //, $number;
    my $last_idx = -1;

    for my $i (0 .. $#chars) {
        if ($chars[$i] eq $digit) {
            $last_idx = $i;
            if ($i < $#chars && $chars[$i + 1] gt $digit) {
                splice @chars, $i, 1;
                return join '', @chars;
            }
        }
    }
    splice @chars, $last_idx, 1 if $last_idx >= 0;
    return join '', @chars;
}
Python Implementation
def make_it_bigger(number: str, digit: str) -> str:
    """
    Remove exactly one occurrence of *digit* from *number* to maximise
    the resulting decimal value.
    """
    last_idx = -1
    chars = list(number)

    for i, ch in enumerate(chars):
        if ch == digit:
            last_idx = i
            if i + 1 < len(chars) and chars[i + 1] > digit:
                del chars[i]
                return ''.join(chars)

    if last_idx >= 0:
        del chars[last_idx]
    return ''.join(chars)

Task 2: Big and Little Omega

"Prime Factor Counter: Distinct or Total?"

Given a number and a mode flag: mode 0 computes little omega (count of distinct prime factors), and mode 1 computes big omega (count of all prime factors including duplicates).

The Strategy: Factorise the number using trial division. Return the size of the set of factors for mode 0 (distinct), or the total count of all factors for mode 1.
Perl Implementation
sub factorize {
    my ($n) = @_;
    my @factors;

    for my $d (2 .. $n) {
        last if $d * $d > $n;
        while ($n % $d == 0) {
            push @factors, $d;
            $n /= $d;
        }
    }
    push @factors, $n if $n > 1;
    return @factors;
}

sub omega {
    my ($number, $mode) = @_;
    my @factors = factorize($number);

    if ($mode == 0) {
        # little omega: distinct prime factors
        my %seen;
        $seen{$_}++ for @factors;
        return scalar keys %seen;
    } else {
        # big omega: all prime factors including duplicates
        return scalar @factors;
    }
}
Python Implementation
def factorize(n: int) -> list[int]:
    """Return all prime factors of *n* (including duplicates)."""
    factors: list[int] = []
    d = 2
    while d * d <= n:
        while n % d == 0:
            factors.append(d)
            n //= d
        d += 1
    if n > 1:
        factors.append(n)
    return factors

def omega(number: int, mode: int) -> int:
    factors = factorize(number)
    return len(set(factors)) if mode == 0 else len(factors)