How to resolve the algorithm Kaprekar numbers step by step in the JavaScript programming language
How to resolve the algorithm Kaprekar numbers step by step in the JavaScript 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 JavaScript programming language
The provided JavaScript code defines a function, isKaprekar
, that checks whether a given number is a Kaprekar number and then uses it in another function, kaprekar
, to find and print Kaprekar numbers within a specified range and in different number bases. Here's a detailed explanation:
-
isKaprekar(n, bs)
function:-
This function checks whether a given positive integer
n
is a Kaprekar number in the specified basebs
(defaulting to base 10 if not provided). -
It calculates the square of
n
and converts it to a string in the specified base. -
It then iterates over each split point in the squared string and checks if the string can be split into two positive integers
a
andb
such thata + b
equals the original numbern
. If such a split is found, it returnstrue
, indicating thatn
is a Kaprekar number in the given base.
-
-
kaprekar(s, e, bs, pbs)
function:-
This function finds and prints Kaprekar numbers within a specified range
[s, e]
, in the specified basebs
(defaulting to base 10), and prints them in a different basepbs
(defaulting to base 10). -
It iterates over each number
n
in the range[s, e]
and checks ifn
is a Kaprekar number using theisKaprekar
function. -
If
n
is a Kaprekar number, it prints the string representation ofn
in the specified print basepbs
.
-
-
Usage of the functions:
-
The code uses the
kaprekar
function to find and print Kaprekar numbers in different ranges and bases:-
kaprekar(1, 99)
: Finds and prints Kaprekar numbers between 1 and 99 in base 10 and prints them in uppercase base 10. -
kaprekar(1, 255, 16)
: Finds and prints Kaprekar numbers between 1 and 255 in base 16 and prints them in uppercase base 10. -
kaprekar(1, 255, 16, 16)
: Finds and prints Kaprekar numbers between 1 and 255 in base 16 and prints them in uppercase base 16. -
kaprekar(1, 288, 17, 17)
: Finds and prints Kaprekar numbers between 1 and 288 in base 17 and prints them in uppercase base 17.
-
-
Source code in the javascript programming language
function isKaprekar( n, bs ) {
if ( n < 1 ) return false
if ( n == 1 ) return true
bs = bs || 10
var s = (n * n).toString(bs)
for (var i=1, e=s.length; i<e; i+=1) {
var a = parseInt(s.substr(0, i), bs)
var b = parseInt(s.substr(i), bs)
if (b && a + b == n) return true
}
return false
}
function isKaprekar( n, bs ) {
if ( n < 1 ) return false
if ( n == 1 ) return true
bs = bs || 10
for (var a=n*n, b=0, s=1; a; s*=bs) {
b += a%bs*s
a = Math.floor(a/bs)
if (b && a + b == n) return true
}
return false
}
function kaprekar( s, e, bs, pbs ) {
bs = bs || 10; pbs = pbs || 10
const toString = n => n.toString(pbs).toUpperCase()
document.write('start:',toString(s), ' end:',toString(e), ' base:',bs, ' printBase:',pbs, '<br>' )
for (var k=0, n=s; n<=e; n+=1) if (isKaprekar(n, bs)) k+=1, document.write(toString(n), ' ')
document.write('<br>found ', k, ' numbers<br><br>')
}
kaprekar( 1, 99 )
kaprekar( 1, 255, 16)
kaprekar( 1, 255, 16, 16)
kaprekar( 1, 288, 17, 17)
You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the J programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the OCaml programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean/Calculate Pi step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Magic squares of doubly even order step by step in the 11l programming language
You may also check:How to resolve the algorithm Parametric polymorphism step by step in the Go programming language