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

Published on 12 May 2024 09:40 PM
#Go

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

The code implements the Kaprekar conjecture in Go. The Kaprekar conjecture states that for all numbers except 1, if you square the number and then split the digits in the square into two parts, one part containing the most significant digits and the other containing the least significant digits, the sum of the two parts equals the original number.

The code is structured as follows:

  1. The kaprekar function takes a number n and a base base as input and returns a boolean indicating if n is a Kaprekar number and an integer indicating the position of the split.

  2. The main function firstly computes all the Kaprekar numbers lower than 10000 and prints them, then computes the number of Kaprekar numbers lower than 1e6 and prints it, and finally computes all the Kaprekar numbers between 1 and 1000000 in base 17 and prints them.

package main

import (
   "fmt"
   "strconv"
)

func kaprekar(n uint64, base uint64) (bool, int) {
   order := 0
   if n == 1 {
       return true, -1
   }

   nn, power := n*n, uint64(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
}

func main() {
   max := uint64(10000)
   fmt.Printf("Kaprekar numbers < %d:\n", max)
   for m := uint64(0); m < max; m++ {
       if is, _ := kaprekar(m, 10); is {
           fmt.Println("  ", m)
       }
   }

   // extra credit
   max = 1e6
   var count int
   for m := uint64(0); m < max; m++ {
       if is, _ := kaprekar(m, 10); is {
           count++
       }
   }
   fmt.Printf("\nThere are %d Kaprekar numbers < %d.\n", count, max)

   // extra extra credit
   const base = 17
   maxB := "1000000"
   fmt.Printf("\nKaprekar numbers between 1 and %s(base %d):\n", maxB, base)
   max, _ = strconv.ParseUint(maxB, base, 64)
   fmt.Printf("\n Base 10  Base %d        Square       Split\n", base)
   for m := uint64(2); m < max; m++ {
       is, pos := kaprekar(m, base)
       if !is {
           continue
       }
       sq := strconv.FormatUint(m*m, base)
       str := strconv.FormatUint(m, base)
       split := len(sq)-pos
       fmt.Printf("%8d  %7s  %12s  %6s + %s\n", m,
           str, sq, sq[:split], sq[split:]) // optional extra extra credit
   }
}
 

The code is relatively straightforward. The kaprekar function first checks if n is equal to 1, in which case it returns true. If n is not equal to 1, the function computes the square of n, and then computes the number of digits in the square. It then iterates over the digits in the square, starting from the most significant digit, and adds them to the sum. If the sum is equal to n, the function returns true. Otherwise, the function returns false.

The main function first computes all the Kaprekar numbers lower than 10000 and prints them. It then computes the number of Kaprekar numbers lower than 1e6 and prints it. Finally, it computes all the Kaprekar numbers between 1 and 1000000 in base 17 and prints them.

Source code in the go programming language

package main

import (
    "fmt"
    "strconv"
)

func kaprekar(n uint64, base uint64) (bool, int) {
    order := 0
    if n == 1 {
        return true, -1
    }

    nn, power := n*n, uint64(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
}

func main() {
    max := uint64(10000)
    fmt.Printf("Kaprekar numbers < %d:\n", max)
    for m := uint64(0); m < max; m++ {
        if is, _ := kaprekar(m, 10); is {
            fmt.Println("  ", m)
        }
    }

    // extra credit
    max = 1e6
    var count int
    for m := uint64(0); m < max; m++ {
        if is, _ := kaprekar(m, 10); is {
            count++
        }
    }
    fmt.Printf("\nThere are %d Kaprekar numbers < %d.\n", count, max)

    // extra extra credit
    const base = 17
    maxB := "1000000"
    fmt.Printf("\nKaprekar numbers between 1 and %s(base %d):\n", maxB, base)
    max, _ = strconv.ParseUint(maxB, base, 64)
    fmt.Printf("\n Base 10  Base %d        Square       Split\n", base)
    for m := uint64(2); m < max; m++ {
        is, pos := kaprekar(m, base)
        if !is {
            continue
        }
        sq := strconv.FormatUint(m*m, base)
        str := strconv.FormatUint(m, base)
        split := len(sq)-pos
        fmt.Printf("%8d  %7s  %12s  %6s + %s\n", m,
            str, sq, sq[:split], sq[split:]) // optional extra extra credit
    }
}


  

You may also check:How to resolve the algorithm Factorial step by step in the AsciiDots programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the МК-61/52 programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the Scala programming language
You may also check:How to resolve the algorithm Date format step by step in the Pike programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Maxima programming language