RogerBW's Blog

The Weekly Challenge 357: Uniquely Kaprekar 25 January 2026

I’ve been doing the Weekly Challenges. The latest involved a converging sequence and a fraction generator. (Note that this ends today.)

Task 1: Kaprekar Constant

Write a function that takes a 4-digit integer and returns how many iterations are required to reach Kaprekar's constant (6174). For more information about Kaprekar's Constant please follow the wikipedia page.

To summarise, x(n+1) is x(n) transformed thus: take all the digits of a four-digit number (including leading zeroes), sort ascending and descending, assemble those into new numbers, and take the difference.

This will end in either 6174 or 0. In the latter case I return -1.

Python:

Convert a list to a number:

def a2n(a):
  t = 0
  for d in a:
    t *= 10
    t += d
  return t

def kaprekarconstant(a):
  ct = 0
  b = a

Start the loop.

  while b != 6174:

If we hit the other attractor, return an error.

    if b == 0:
      return -1

Extract the digits.

    digits = []
    for _ in range(4):
      digits.append(b % 10)
      b //= 10

Sort them, and make a reversed copy.

    digits.sort()
    stigid = digits.copy()
    stigid.reverse()

Calculate the new value.

    b = a2n(stigid) - a2n(digits)
    ct += 1
  return ct

Since this is a pretty quick process (nothing that converges takes more than 7 cycles to do so) I thought I'd also plot a map of the domain. This is in reading order: top left is 0000, top right is 0099, and so on, and there are certainly patterns to be had here..

  • 0 - red
  • 1 - orange
  • 2 - yellow
  • 3 - green
  • 4 - blue
  • 5 - purple
  • 6 - magenta
  • 7 - grey
  • no convergence - black

Task 2: Unique Fraction Generator

Given a positive integer N, generate all unique fractions you can create using integers from 1 to N and follow the rules below:

  • Use numbers 1 through N only (no zero)
  • Create fractions like numerator/denominator
  • List them in ascending order (from smallest to largest)
  • If two fractions have the same value (like 1/2 and 2/4), only show the one with the smallest numerator

So for N=3 I could have nine possibilities (1/1, 2/1, 3/1, 1/2, 2/2, etc.) which after deduplication and sorting would be:

1/3, 1/2, 2/3, 1/1, 3/2, 2/1, 3/1

My goal in this solution was to avoid floating-point divisions, indeed any floating point at all.

My approach is to find a common denominator that could stand in all the possible fractions, and build my list in terms of numerators over that. In this case it would be 6, and I'd generate 6/6, 12/6, 18/6, 3/6, 6/6, etc.

In Rust I can use a BTreeSet, and read off the entries in sorted order. Everything else needs a separate sorting step. Perl:

Library gcd function.

sub gcd($m,$n) {
  while ($n!=0) {
    ($m,$n)=($n,$m % $n);
  }
  return $m;
}

sub uniquefractiongenerator($a) {

Find a common denominator for the factions I'm going to generate. (This could be the GCD of all values from 2 to a but multiplying is just as easy and unlikely to run into trouble at this scale.)

  my $den = 1;
  foreach my $dn (2 .. $a) {
    $den *= $dn;
  }

Generate a set of numerators, one for each conceivable fraction. (Which deals with the deduplication, because 1/1 and 2/2 will both show up here as 6/6.)

  my %f;
  foreach my $d (1 .. $a) {
    my $nd = int($den / $d);
    foreach my $n (1 .. $a) {
      $f{$n * $nd} = 1;
    }
  }
  my @out;

Sort that list.

  foreach my $n (sort {$::a <=> $::b} keys %f) {

Express each value in reduced form.

    my $g = gcd($n, $den);
    my $nn = int($n/$g);
    my $nd = int($den/$g);
    push @out, "$nn/$nd";
  }
  \@out;
}

Full code on github.

Add A Comment

Your Name
Your Email
Your Comment

Note that I will only approve comments that relate to the blog post itself, not ones that relate only to previous comments. This is to ensure that the blog remains outside the scope of the UK's Online Safety Act (2023).

Your submission will be ignored if any field is left blank, but your email address will not be displayed. Comments will be processed through markdown.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 2300ad 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech bayern 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 disaster 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 essen 2024 essen 2025 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 horrorm science fiction hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo 2025 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 openscad opera parody paul temple perl perl weekly challenge photography podcast poetry politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant review 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 talon television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 typst 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