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

Published on 12 May 2024 09:40 PM

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

Source code in the nim programming language

import strutils, sequtils
 
proc k(n: int): bool =
  let n2 = $(n.int64 * n)
  for i in 0 .. n2.high:
    let a = if i > 0: parseBiggestInt n2[0 ..< i] else: 0
    let b = parseBiggestInt n2[i .. n2.high]
    if b > 0 and a + b == n:
      return true
 
echo toSeq(1..10_000).filter(k)
echo len toSeq(1..1_000_000).filter(k)


  

You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the Raven programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the EDSAC order code programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Maple programming language