How to resolve the algorithm Kaprekar numbers step by step in the Seed7 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Kaprekar numbers step by step in the Seed7 programming language

Table of Contents

Problem Statement

A positive integer is a Kaprekar number if: Note that a split resulting in a part consisting purely of 0s is not valid, as 0 is not considered positive.

10000 (1002) splitting from left to right:

Generate and show all Kaprekar numbers less than 10,000.

Optionally, count (and report the count of) how many Kaprekar numbers are less than 1,000,000.

The concept of Kaprekar numbers is not limited to base 10 (i.e. decimal numbers); if you can, show that Kaprekar numbers exist in other bases too.

For this purpose, do the following:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Kaprekar numbers step by step in the Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";
  include "bigint.s7i";

const func bigInteger: kaprekar (in bigInteger: n, in bigInteger: base) is func
  result
    var bigInteger: kaprekar is 0_;
  local
    var bigInteger: nn is 0_;
    var bigInteger: r is 0_;
    var bigInteger: powerOfBase is 1_;
  begin
    nn := n ** 2;
    while powerOfBase < n do
      powerOfBase *:= base;
    end while;
    if n = powerOfBase then
      kaprekar := bigInteger conv ord(n = 1_);
    else
      r := nn rem powerOfBase;
      while r < n do
        if nn div powerOfBase + r = n then
          kaprekar := powerOfBase;
          r := n;
        else
	  powerOfBase *:= base;
          r := nn rem powerOfBase;
        end if;
      end while;
    end if;
  end func;

const proc: main is func
  local
    var bigInteger: aNumber is 0_;
    var integer: count is 0;
    var bigInteger: powerOfBase is 1_;
    const integer: base is 17;
  begin
    writeln("base 10:");
    for aNumber range 1_ to 1000000_ do
      if kaprekar(aNumber, 10_) <> 0_ then
        incr(count);
        writeln(count lpad 3 <& ": " <& aNumber);
      end if;
    end for;
    writeln;
    writeln("base " <& base <& ":");
    writeln("  1: 1");
    count := 1;
    for aNumber range 2_ to 1000000_ do
      powerOfBase := kaprekar(aNumber, bigInteger conv base);
      if powerOfBase <> 0_ then
        incr(count);
        write(count lpad 3 <& ": " <& aNumber);
        write(" \t" <& aNumber radix base);
        write("\t"  <& aNumber ** 2 radix base);
        write("\t"  <& aNumber ** 2 mdiv powerOfBase radix base);
        write(" + " <& aNumber ** 2 mod powerOfBase radix base);
        writeln;
      end if;
    end for;
  end func;

  

You may also check:How to resolve the algorithm Thue-Morse step by step in the XLISP programming language
You may also check:How to resolve the algorithm Word wrap step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Unix/ls step by step in the Scala programming language
You may also check:How to resolve the algorithm Function definition step by step in the bc programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the BASIC256 programming language