# RogerBW's Blog

 The Weekly Challenge 176: Reversible Permutations 04 August 2022 I’ve been doing the Weekly Challenges. The latest involved lots of number-string conversions. (Note that this is open until 7 August 2022.) Task 1: Permuted Multiples Write a script to find the smallest integer x such that x, 2x, 3x, 4x, 5x and 6x are permuted multiples of each other. Which the old hand will immediately recognise as `142857`, but let's pretend we don't know that and work it out from first principles. My approach is to build a function which will depermute a number, to either a sorted string or a sorted array. Which of those I use depends on the language: some can't readily compare the contents of arrays, others can't readily sort the contents of strings. (And some can only sort in place rather than returning a sorted copy.) Calculate the value of that function for each candidate, and check that each multiple of the candidate also matches. Kotlin: ``````fun permutable(): Int { var n = 0 while (true) { n += 1 `````` Here's the important bit: `````` val base = n.toString().toCharArray().toList().sorted() `````` so `142857` will become `[ "1", "2", "4", "5", "7", "8" ]`. Then I just compare each multiple, bailing out on a non-match. `````` var q = true for (k in 2..6) { val tt = (n*k).toString().toCharArray().toList().sorted() if (tt != base) { q = false break } } if (q) { return n } } } `````` Task 2: Reversible Numbers Write a script to find out all Reversible Numbers below 100. ``````A number is said to be a reversible if sum of the number and its reverse had only odd digits. `````` So again the crunchy bit is reversing the number, which means converting it to a string, maybe converting that to an array, reversing the elements, and going back up the chain to get a number. At least in the typed languages! While I do get irked by the Perl/Raku approach to typing, it does make this very easy. In Raku here: ``````sub reversible(\$mx) { my @o; for (1..\$mx) -> \$n { `````` That `flip` and sloppy typing together are doing a lot of work, anchored by `+` being a numerical operator (versus `~` for string concatenation). `````` my Int \$t = \$n + flip(\$n); `````` Then we just check for no even digits, which could be a regex but I did it this way instead because it's more portable across the other languages. Check whether the last digit is even; if not, divide by ten dropping remainder. `````` my Bool \$q = True; while (\$t > 0) { if (\$t % 2 == 0) { \$q = False; last; } \$t div= 10; } if (\$q) { @o.push(\$n); } } return @o; } `````` 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 10 per page 20 per page 50 per page 100 per page 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 crystal 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 2021 hugo 2022 hugo 2023 hugo 2024 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