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

Published on 12 May 2024 09:40 PM

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

Source code in the factor programming language

USING: io kernel lists lists.lazy locals math math.functions
math.ranges prettyprint sequences ;

:: kaprekar? ( n -- ? )
    n sq :> sqr
    1 lfrom
    [ 10 swap ^ ] lmap-lazy
    [ n > ] lfilter
    [ sqr swap mod n < ] lwhile
    list>array
    [ 1 - sqr n - swap mod zero? ] any?
    n 1 = or ;

1,000,000 [1,b] [ kaprekar? ] filter dup . length
"Count of Kaprekar numbers <= 1,000,000: " write .


  

You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the R programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the OCaml programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the Erlang programming language
You may also check:How to resolve the algorithm Execute Brain step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Disarium numbers step by step in the Scala programming language