How to resolve the algorithm Rhonda numbers step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rhonda numbers step by step in the Nim programming language

Table of Contents

Problem Statement

A positive integer n is said to be a Rhonda number to base b if the product of the base b digits of n is equal to b times the sum of n's prime factors.

These numbers were named by Kevin Brown after an acquaintance of his whose residence number was 25662, a member of the base 10 numbers with this property.

25662 is a Rhonda number to base-10. The prime factorization is 2 × 3 × 7 × 13 × 47; the product of its base-10 digits is equal to the base times the sum of its prime factors: 2 × 5 × 6 × 6 × 2 = 720 = 10 × (2 + 3 + 7 + 13 + 47) Rhonda numbers only exist in bases that are not a prime. Rhonda numbers to base 10 always contain at least 1 digit 5 and always contain at least 1 even digit.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rhonda numbers step by step in the Nim programming language

Source code in the nim programming language

import std/[sequtils, strformat, strutils]

type Base = 2..36

template isEven(n: int): bool = (n and 1) == 0

func isPrime(n: Natural): bool =
  ## Return true if "n" is prime.
  if n < 2: return false
  if n.isEven: return n == 2
  if n mod 3 == 0: return n == 3
  var d = 5
  while d * d <= n:
    if n mod d == 0: return false
    inc d, 2
  return true

func digitProduct(n: Positive; base: Base): int =
  ## Return the product of digits of "n" in given base.
  var n = n.Natural
  result = 1
  while n != 0:
    result *= n mod base
    n = n div base

func primeFactorSum(n: Positive): int =
  ## Return the sum of prime factors of "n".
  var n = n.Natural
  while n.isEven:
    inc result, 2
    n  = n shr 1
  var d = 3
  while d * d <= n:
    while n mod d == 0:
      inc result, d
      n = n div d
    inc d, 2
  if n > 1: inc result, n

func isRhondaNumber(n: Positive; base: Base): bool =
  ## Return true if "n" is a Rhonda number to given base.
  n.digitProduct(base) == base * n.primeFactorSum

const Digits = toSeq('0'..'9') & toSeq('a'..'z')

func toBase(n: Positive; base: Base): string =
  ## Return the string representation of "n" in given base.
  var n = n.Natural
  while true:
    result.add Digits[n mod base]
    n = n div base
    if n == 0: break
  # Reverse the digits.
  for i in 1..(result.len shr 1):
    swap result[i - 1], result[^i]


const N = 10

for base in 2..36:
  if base.isPrime: continue
  echo &"First {N} Rhonda numbers to base {base}:"
  var rhondaList: seq[Positive]
  var n = 1
  var count = 0
  while count < N:
    if n.isRhondaNumber(base):
      rhondaList.add n
      inc count
    inc n
  echo "In base 10: ", rhondaList.join(" ")
  echo &"In base {base}: ", rhondaList.mapIt(it.toBase(base)).join(" ")
  echo()


  

You may also check:How to resolve the algorithm Echo server step by step in the Oz programming language
You may also check:How to resolve the algorithm 100 prisoners step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm User input/Graphical step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Element-wise operations step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Icon and Unicon programming language