RogerBW's Blog

Perl Weekly Challenge 140: Binary and Tabular 24 November 2021

I’ve been doing the Weekly Challenges. The latest involved an unusual binary encoding and indexing into a multiplication table. (Note that this is open until 28 November 2021.)

Task 1: Add Binary

You are given two decimal-coded binary numbers, $a and $b.

Write a script to simulate the addition of the given binary numbers.

In other words, interpret 101 as 5, 1001 as 9, and add them to make 14, expressed as 1110.

It would have been possible to do this piecewise, taking each digit of each number, but instead I chose to write a converter between this decimal-coded notation and standard integers, particularly once I realised that the converters to and from could use basically the same code.

Raku:

sub cvradix($n,$r,$tf) {
  my $o=0;
  my $nn=$n;
  my $m=1;
  my $ra;
  my $rb;
  if ($tf==0) { # convert to radix-format
    $ra=$r;
    $rb=10;
  } else { # convert from radix-format
    $ra=10;
    $rb=$r;
  }
  while ($nn > 0) {
    $o+=($nn % $ra)*$m;
    $nn=floor($nn/$ra);
    $m*=$rb;
  }
  return $o;
}

Then the function itself is trivially:

sub dcbadd($a,$b) {
  return cvradix(cvradix($a,2,1)+cvradix($b,2,1),2,0);
}

and the other languages all work basically the same way.

Task 2: Multiplication Table

You are given 3 positive integers, $i, $j and $k.

Write a script to print the $kth element in the sorted multiplication table of $i and $j.

I could do various optimising tricks (for example, there's no point in calculating beyond k*k) but the simplest approach seemed to in avoiding prepature optimisation: just work out the actual multiplication table, sort it, and then index into it. Rust, as an example:

fn mtable(i: u32,j: u32,k: usize) -> u32 {
    let mut l=vec![];
    for ix in 1..=i {
        for jx in 1..=j {
            l.push(ix*jx);
        }
    }
    l.sort();
    return l[k-1];
}

Full code on github.

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 advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2022 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript 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 ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge 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