RogerBW's Blog

Perl Weekly Challenge 51: triplet sums and colourful numbers 07 April 2020

I’ve been doing the Perl Weekly Challenges. The latest involved a lot of nested loops.

Given an array @L of integers. Write a script to find all unique triplets such that a + b + c is same as the given target T. Also make sure a <= b <= c.

Here is wiki page for more information.

Example:

@L = (-25, -10, -7, -3, 2, 4, 8, 10);

One such triplet for target 0 i.e. -10 + 2 + 8 = 0.

A brute-force solution seemed like the simplest approach. One could also hash the inputs and determine whether a suitable third number was present to make the desired sum, but this would involve more complication. (If I had a number list long enough that the iteration time were a concern, I'd use it; this algorithm is something like O(N³).)

@l=sort {$a <=> $b} @l;
my %r;
foreach my $a (0..$#l-2) {
  foreach my $b ($a+1..$#l-1) {
    foreach my $c ($b+1..$#l) {
      if ($l[$a]+$l[$b]+$l[$c]==$t) {
        $r{$l[$a]}{$l[$b]}{$l[$c]}=1;
      }
    }
  }
}

foreach my $d (sort {$a <=> $b} keys %r) {
  foreach my $e (sort {$a <=> $b} keys %{$r{$d}}) {
    foreach my $f (sort {$a <=> $b} keys %{$r{$d}{$e}}) {
      print "$d + $e + $f\n";
    }
  }
}

Perl6 is much the same.

Write a script to display all Colorful Number with 3 digits.

A number can be declare Colorful Number where all the products of consecutive subsets of digit are different.

For example, 263 is a Colorful Number since 2, 6, 3, 2x6, 6x3, 2x6x3 are unique.

There are some obvious optimisations for colourful numbers (no digits 0 or 1, no repeated digits); but most of the numbers that survive that, about one-third, turn out to be colourful, with only a few exceptions (e.g. 236).

foreach my $a (2..9) { # 1?? will never be colourful
  foreach my $b (2..9) { # ?0? and ?1? will never be colourful
    if ($a==$b) {
      next;
    }
    foreach my $c (2..9) { # ??0 and ??1 will never be colourful
      if ($a==$c || $b==$c) {
        next;
      }
      my %p;
      $p{$a}++;
      $p{$b}++;
      $p{$c}++;
      $p{$a*$b}++;
      $p{$b*$c}++;
      $p{$a*$b*$c}++;
      if (max(values %p) < 2) {
        print "$a$b$c\n";
      }
    }
  }
}

One could optimise this slightly by pre-loading and copying %p, and for longer numbers by doing an actual iteration rather than just individual calculations, but again at this scale it's not worth the extra complexity.

Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action aeronautics aikakirja anecdote animation anime army astronomy audio audio tech base commerce battletech beer boardgaming book of the week bookmonth chain of command children chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 cycling dead of winter doctor who documentary drama driving drone ecchi economics espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo geodata gin gurps gurps 101 harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo-nebula reread humour in brief avoid instrumented life kickstarter learn to play leaving earth linux lovecraftiana mecha men with beards museum mystery naval non-fiction one for the brow opera perl perl weekly challenge photography podcast politics powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1