How to resolve the algorithm Disarium numbers step by step in the Tcl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Disarium numbers step by step in the Tcl programming language

Table of Contents

Problem Statement

A Disarium number is an integer where the sum of each digit raised to the power of its position in the number, is equal to the number.

135 is a Disarium number: 11 + 32 + 53 == 1 + 9 + 125 == 135 There are a finite number of Disarium numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Disarium numbers step by step in the Tcl programming language

Source code in the tcl programming language

proc is_disarium {num} {
    set n num
    set sum 0
    set i 1
    set ch 1
    foreach char [split $num {}] {
        scan $char %d ch
        set sum [ expr ($sum + $ch ** $i)]
        incr i
    }
    return [ expr $num == $sum ? 1 : 0]
}
set i 0
set count 0
while { $count < 19 } {
    if [ is_disarium $i ] {
        puts -nonewline  "${i} "
        incr count
    }
    incr i
}
puts ""


  

You may also check:How to resolve the algorithm Josephus problem step by step in the R programming language
You may also check:How to resolve the algorithm O'Halloran numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Lua programming language
You may also check:How to resolve the algorithm Longest string challenge step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Mouse position step by step in the Processing programming language