Program for Armstrong Numbers in Deluge

Program for Armstrong Numbers in Deluge

Introduction

Some numbers have a strange kind of self-referential beauty to them. You break them apart, do some maths with the pieces, and somehow end up right back where you started. Armstrong numbers — also called narcissistic numbers — are exactly that kind of number, and once you see how they work, they are genuinely hard to forget.

In this post I want to walk through what Armstrong numbers actually are, why the definition trips people up the first time they read it, and then how to write a clean Deluge script inside Zoho that checks whether any given number qualifies. I will break the code down line by line so nothing feels like a black box.


What Exactly Is an Armstrong Number?

Here is the formal definition, and I want to make sure we go slow on this because the wording catches people out.

A number is called an Armstrong number if the sum of each of its digits raised to the power of the total number of digits equals the number itself.

That last part is the key bit that people miss the first time: the power you raise each digit to is not fixed — it changes depending on how many digits the number has. A 3-digit number uses power 3. A 4-digit number uses power 4. And so on.

The Rule:

For a number with digits a, b, c, d... and total digit count n:
an + bn + cn + dn + ... = the original number

Let us make that concrete with the three examples we are going to test.


Walking Through the Examples By Hand

Example 1Is 153 an Armstrong number?

153 has 3 digits, so we use power 3.

1³ + 5³ + 3³ = 1 + 125 + 27 = 153

True — Armstrong Number

Example 2Is 9474 an Armstrong number?

9474 has 4 digits, so we use power 4.

9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 = 9474

True — Armstrong Number

Example 3Is 123 an Armstrong number?

123 has 3 digits, so we use power 3.

1³ + 2³ + 3³ = 1 + 8 + 27 = 36 ≠ 123

False — Not an Armstrong Number

Notice how 9474 works with power 4, not power 3. That is the thing that makes this problem a little more interesting than it first looks — the exponent is not a constant you hardcode, it comes from the number itself.


How We Will Approach This in Deluge

Before we look at any code, it helps to think through the steps we need the script to take:

1 Take the input number as a string so we can easily split it into individual digits
2 Count how many characters (digits) the string has — that count becomes our power
3 Loop through each digit, raise it to that power, and keep a running total
4 Compare the total against the original number — if they match, it is an Armstrong number

One thing worth flagging before we look at the code: Deluge does not have a traditional character-by-character string iterator, so we use a small trick. When you call toList("") on a string with an empty delimiter, Deluge splits the string into a list where each element is one character. That is what lets us loop digit by digit.


The Deluge Script

Here is the complete script with comments throughout so every line is accounted for:

// Input number - stored as a string so we can split it digit by digit
n = "153";

// Count the total number of digits - this becomes our power (exponent)
len = n.length();

// Start with a sum of zero
sum = 0;

// Split the string into individual characters and loop through each one
// toList("") with an empty delimiter splits every character separately
for each digit in n.toList("")
{
    // Convert the character back to a number so we can do maths on it
    num = digit.toNumber();

    // Raise it to the power of the digit count and add to running total
    sum = sum + num.power(len);
}

// Compare the computed sum to the original number
// We convert n back to a number for the comparison
if(sum == n.toNumber())
{
    info "True - Armstrong Number";
}
else
{
    info "False - Not an Armstrong Number";
}

Line-by-Line Breakdown

Let me go through the parts that matter most.

Why store n as a string?

If we stored n as a number from the start, we could not easily iterate over its individual digits. Strings in Deluge can be split character by character using toList(""), but numbers cannot. So we keep it as a string for the loop and convert it back to a number only at the final comparison step.

What does n.length() give us?

For "153" it returns 3. For "9474" it returns 4. This is the exponent we pass into the power() function. The beauty of this approach is that the script works for any number of digits — you do not need separate logic for 3-digit vs. 4-digit numbers.

The toList("") trick

This is the most Deluge-specific part of the whole script. When you call n.toList("") with an empty string as the delimiter, Deluge treats every single character as its own list item. So "153".toList("") becomes a list of three items: ["1", "5", "3"].

From there, for each digit in steps through that list one item at a time, and digit.toNumber() converts the character string back to an integer we can raise to a power.

The num.power(len) call

Deluge has a built-in power() function on numbers. num.power(len) is equivalent to numlen. So for digit 5 with len = 3, this gives us 5³ = 125. We add that to sum on each pass through the loop.


Tracing Through the Script for 153

I find it really helps to step through a specific example manually, especially when there is a loop involved. Let us trace what happens when n = "153":

Loop Pass digit num num.power(3) sum after this pass
1 "1" 1 1 1
2 "5" 5 125 126
3 "3" 3 27 153 ✔

Final sum = 153, which matches n.toNumber() = 153, so the script prints True — Armstrong Number.


Try It With Different Inputs

Swap out the value of n at the top of the script and run it in Zoho's function editor or a sandbox environment to verify each case:

Input Digits (n) Working Result
1 1 1¹ = 1 True
153 3 1 + 125 + 27 = 153 True
370 3 27 + 343 + 0 = 370 True
9474 4 6561 + 256 + 2401 + 256 = 9474 True
123 3 1 + 8 + 27 = 36 ≠ 123 False

370 is a fun one to try. Zero raised to any power is still zero, so that digit contributes nothing to the sum — but the number still qualifies as an Armstrong number because 27 + 343 + 0 = 370.


Mistakes People Make With This Problem

A few things trip people up when they first write this themselves:

Using a fixed power instead of len

If you hardcode power(3), your script works for 3-digit numbers but gives wrong answers for anything else. Always derive the power from n.length() dynamically.

Forgetting to convert digit back to a number

After splitting the string, each item in the list is still a string character. Calling .power() directly on a string character will throw a type error. Always call digit.toNumber() first.

Comparing sum to a string instead of a number

At the end, sum is an integer but n is a string. sum == n will always be false because of the type mismatch. Use n.toNumber() on the right side of the comparison.


Wrapping Up

Armstrong numbers are one of those problems that look like a puzzle but actually teach you a handful of genuinely useful scripting concepts — string-to-list conversion, dynamic exponents, type handling, and loop accumulation. In Deluge specifically, the toList("") trick for splitting a string character by character is something you will use again in all sorts of other problems, so it is worth really understanding here rather than just copying the pattern.

If you want to take this further, try modifying the script to loop through a range of numbers and print out all the Armstrong numbers below 10,000. There are only a handful of them, and finding them all is a satisfying little exercise. If you get stuck or want to compare approaches, drop a comment below.

Post a Comment