How to resolve the algorithm Kaprekar numbers step by step in the PowerShell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Kaprekar numbers step by step in the PowerShell 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 PowerShell programming language
Source code in the powershell programming language
function Test-Kaprekar ([int]$Number)
{
if ($Number -eq 1)
{
return $true
}
[int64]$a = $Number * $Number
[int64]$b = 10
while ($b -lt $a)
{
[int64]$remainder = $a % $b
[int64]$quotient = ($a - $remainder) / $b
if ($remainder -gt 0 -and $remainder + $quotient -eq $Number)
{
return $true
}
$b *= 10
}
return $false
}
"Kaprekar numbers less than 10,000:"
1..10000 | ForEach-Object {if (Test-Kaprekar -Number $_) {"{0,6}" -f $_}} | Format-Wide {$_} -Column 17 -Force
"Kaprekar numbers less than 1,000,000:"
1..1000000 | ForEach-Object {if (Test-Kaprekar -Number $_) {"{0,6}" -f $_}} | Format-Wide {$_} -Column 18 -Force
You may also check:How to resolve the algorithm Casting out nines step by step in the Scala programming language
You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the C programming language
You may also check:How to resolve the algorithm Search in paragraph's text step by step in the Phix programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Ternary logic step by step in the BASIC256 programming language