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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Kaprekar numbers step by step in the zkl 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 zkl programming language

Source code in the zkl programming language

fcn isKaprekarB(n,b=10){
   powr:=n*n;
   r:=l:=0; tens:=b;
   while(r
      r = powr % tens;
      l = powr / tens;
      if (r and (l + r == n)) return(True);
      tens *= b;
   }
   return(False);
}

println("Kaprekar number <= 10,000:\n",
   [1..].filter(T(isKaprekarB, fcn(n){ if(n>=10000) Void.Stop else True })));

rc:=Ref(0);
[1 .. 0d1_000_000].pump(rc.inc,isKaprekarB,Void.Filter); // if(filter) rc++ 
rc.value.println(" Kaprekar numbers are less than 1,000,000");  // 54

kbs:=[1..].filter(T(isKaprekarB.fp1(17),
                  fcn(n){ if(n>=0d1_000_000) Void.Stop else True }));
Utils.zipWith(fcn(k,n){ "%3d: %7d == %.17B".fmt(n,k,k).println() },kbs,[1..]);

  

You may also check:How to resolve the algorithm Loops/Downward for step by step in the Nim programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the 11l programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the 11l programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the 360 Assembly programming language