How to resolve the algorithm Kaprekar numbers step by step in the C# programming language
How to resolve the algorithm Kaprekar numbers step by step in the C# 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 C# programming language
The provided C# code implements a program that finds and prints all Kaprekar numbers less than a given maximum value. Kaprekar numbers are positive integers whose square when written in base-10, can be split into two parts that add up to the original number again. For instance, 45 is a Kaprekar number, because 45 squared is 2025 and 20+25=45.
The program uses a generator function, _kaprekarGenerator, to generate Kaprekar numbers. The generator function starts by yielding 1, which is always a Kaprekar number. It then iterates through all positive integers greater than 1 and tests each one to see if it is a Kaprekar number.
To test whether a number is a Kaprekar number, the generator function first squares the number. It then iterates through all powers of 10 from 10^1 to 10^19, and for each power of 10, it checks whether the remainder of the square when divided by that power of 10 is zero and whether the quotient is equal to the original number. If either of these conditions is not met, then the number is not a Kaprekar number.
If the number is a Kaprekar number, the generator function yields it and moves on to the next number. The program then iterates through the generator function, printing each Kaprekar number that it yields, and counting the total number of Kaprekar numbers that it finds. Finally, the program prints the total number of Kaprekar numbers that it found.
Source code in the csharp programming language
using System;
using System.Collections.Generic;
public class KaprekarNumbers {
/// <summary>
/// The entry point of the program, where the program control starts and ends.
/// </summary>
public static void Main() {
int count = 0;
foreach ( ulong i in _kaprekarGenerator(999999) ) {
Console.WriteLine(i);
count++;
}
Console.WriteLine("There are {0} Kaprekar numbers less than 1000000.", count);
}
/// <summary>
/// Generator function which generates the Kaprekar numbers.
/// </summary>
/// <returns>The generator.</returns>
/// <param name="max">The maximum value of the numbers generated.</param>
private static IEnumerable<ulong> _kaprekarGenerator(ulong max) {
ulong next = 1;
// 1 is always a Kaprekar number.
yield return next;
for ( next = 2; next <= max; next++ ) {
ulong square = next * next;
for ( ulong check = 10; check <= 10000000000000000000; check *= 10 ) {
// Check the square against each power of 10 from 10^1 to 10^19 (highest which can be
// represented by a ulong)
// If the power of 10 to be checked against is greater than or equal to the square, stop checking
if ( square <= check )
break;
// Given a power of 10 as 10^n, the remainder when dividing the square number by that power
// of 10 is equal to the last n digits of the number (starting from the right) and the
// quotient gives the remaining digits.
// If the last n digits are all zeroes, then the remainder will be zero, which is not
// accepted.
ulong r = square % check;
ulong q = (square - r) / check;
if ( r != 0 && q + r == next ) {
yield return next;
break;
}
}
}
}
}
You may also check:How to resolve the algorithm Solve a Holy Knight's tour step by step in the D programming language
You may also check:How to resolve the algorithm Commatizing numbers step by step in the Racket programming language
You may also check:How to resolve the algorithm Character codes step by step in the VBScript programming language
You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the D programming language