How to resolve the algorithm Kaprekar numbers step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

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

Source code in the v programming language

import strconv

fn kaprekar(n u64, base u64) (bool, int) {
    mut order := 0
    if n == 1 {
        return true, -1
    }
 
    nn, mut power := n*n, u64(1)
    for power <= nn {
        power *= base
        order++
    }
 
    power /= base
    order--
    for ; power > 1; power /= base {
        q, r := nn/power, nn%power
        if q >= n {
            return false, -1
        }
 
        if q+r == n {
            return true, order
        }
 
        order--
    }
 
    return false, -1
}
 
fn main() {
    mut max := u64(10000)
    println("Kaprekar numbers < $max:")
    for m := u64(0); m < max; m++ {
        isk, _ := kaprekar(m, 10)
        if isk {
            println("  $m")
        }
    }
 
    // extra credit
    max = u64(1e6)
    mut count := 0
    for m := u64(0); m < max; m++ {
        isk, _ := kaprekar(m, 10)
        if isk {
            count++
        }
    }
    println("\nThere are $count Kaprekar numbers < ${max}.")
 
    // extra extra credit
    base := 17
    max_b := "1000000"
    println("\nKaprekar numbers between 1 and ${max_b}(base ${base}):")
    max, _ = strconv.common_parse_uint2(max_b, base, 64)
    println("\n Base 10  Base ${base}        Square       Split")
    for m := u64(2); m < max; m++ {
        isk, pos := kaprekar(m, u64(base))
        if !isk {
            continue
        }
        sq := strconv.format_uint(m*m, base)
        str := strconv.format_uint(m, base)
        split := sq.len-pos
        println("${m:8}  ${str:7}  ${sq:12}  ${sq[..split]:6} + ${sq[split..]}") // optional extra extra credit
    }
}


  

You may also check:How to resolve the algorithm Yin and yang step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm Append a record to the end of a text file step by step in the AWK programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Knapsack problem/Bounded step by step in the Phix programming language
You may also check:How to resolve the algorithm Cantor set step by step in the Wren programming language