Introduction
There is a problem that shows up in almost every scripting course, coding interview, and beginner tutorial — and for good reason. Finding the sum of the first n natural numbers is one of those problems that looks dead simple on paper but teaches you two completely different ways of thinking about code: the mathematical shortcut, and the iterative grind.
In this post, I want to show you how to solve it in Deluge — Zoho's scripting language. Whether you are brand new to Deluge or just looking for a clean reference, I will walk through both approaches so you actually understand what is going on, not just copy-paste and move on.
Let us start from the beginning.
What Does the Problem Actually Ask?
You are given a positive integer n. Your job is to add up every natural number from 1 all the way up to n and return the total.
Natural numbers are just the counting numbers: 1, 2, 3, 4, and so on. Nothing negative, nothing fractional — just the basics.
Here are a few quick examples so we are on the same page:
| Input (n) | Working | Output |
|---|---|---|
| 1 | 1 | 1 |
| 3 | 1 + 2 + 3 | 6 |
| 4 | 1 + 2 + 3 + 4 | 10 |
Simple enough. Now let us look at the two ways to get there in Deluge.
Method 1: The Formula Approach
The fastest way to get the sum is to skip the loop entirely and use a formula that mathematicians worked out a very long time ago. The story goes that the mathematician Gauss figured this out as a child when his teacher asked him to add all numbers from 1 to 100. Instead of adding them one by one, he noticed something: 1 + 100 = 101, 2 + 99 = 101, 3 + 98 = 101 — and there are 50 such pairs. So the answer is 50 × 101 = 5050.
That same thinking gives us the general formula:
Sum = ( n × (n + 1) ) / 2
For n = 4: (4 × 5) / 2 = 20 / 2 = 10. Done in one step.
Here is how that looks in Deluge:
n = 4; sum = (n * (n + 1)) / 2; info sum; // Output: 10
Three lines. That is all there is to it. Deluge handles the arithmetic directly, and info sum; prints the result to the logs so you can verify it.
Method 2: Using a Loop
Now, Deluge does not have a traditional for (int i = 1; i <= n; i++) style loop like you might have in Java or Python. It works with lists — specifically, it loops over items in a list using for each.
So the trick here is: generate a list with exactly n items, then loop through that list and use the loop index to accumulate the sum. Here is how that works step by step.
leftpad() to create a string with n spaces, then replaceAll() to swap each space with a comma — creating a comma-separated string of n itemstoList()for each, using the loop index (v_Index) as the current number to addHere is the full Deluge code, broken out cleanly with comments:
// Step 1: Set n to the number of natural numbers to sum
n = 4;
// Step 2: Build a comma-separated string with n entries using leftpad
// leftpad(" ", n) creates a string of n spaces
// replaceAll(" ", ",") turns each space into a comma
iterationList = leftpad(" ", n).replaceAll(" ", ",");
// Step 3: Convert the comma-separated string into a Deluge list
l_GeneratedList = iterationList.toList();
// Step 4: Loop through the list, adding the index at each step
sum = 0;
for each v_Index in l_GeneratedList
{
sum = sum + v_Index;
}
info sum;
// Output: 10
The first time I saw this pattern I had to sit with it for a minute. The leftpad trick is a bit of a Deluge idiom — it is the standard way to generate a list of a known length when you do not already have data to iterate over. Once it clicks, you will start seeing it everywhere in Deluge scripts.
How leftpad generates the list
leftpad(" ", 4) produces a string of 4 spaces: " "
.replaceAll(" ", ",") turns that into: ",,,,"
.toList() splits that by comma into a list with 4 entries, each indexed 1 through 4. The loop then adds 1 + 2 + 3 + 4 = 10.
Side-by-Side: Which One Should You Use?
| Formula Method | Loop Method | |
|---|---|---|
| Lines of code | 3 | ~10 |
| Speed | Constant — always one step | Grows with n |
| Flexibility | Sum only | Can do more at each step |
| Readability | Instant once you know the formula | Takes a moment to trace through |
| Best for | Pure calculation, production scripts | Learning iteration, complex logic per step |
Honestly? Learn both. The formula is the one you will use in real Zoho scripts when you just need a number fast. The loop is the one that teaches you how iteration actually works in Deluge, and that skill carries over into far more complex problems.
Quick Test: Try It Yourself
Before you move on, try changing the value of n and verifying the output yourself. Use Zoho's built-in function editor or a sandbox environment:
- Set
n = 10→ expect 55 - Set
n = 100→ expect 5050 (Gauss's famous answer) - Set
n = 1→ expect 1
Both methods should give you the same answer every time. If they do not, something is off with your n value or how the list is being constructed — add an info iterationList; line after the replaceAll to see exactly what string is being generated.
Wrapping Up
Summing the first n natural numbers might be a beginner problem, but the two approaches it teaches you — mathematical shortcutting and iterative thinking — are skills you will reach for constantly in real Deluge development inside Zoho CRM, Creator, or wherever else you are writing scripts.
The formula method is clean, fast, and something you should just memorise at this point. The loop method is worth understanding deeply because the leftpad + toList + for each pattern is something you will reuse in plenty of other Deluge situations where you need to repeat a block of code a set number of times.
Got questions about either approach, or want to see a variation — like summing only odd numbers, or every other number? Drop it in the comments and I will pick it up in a follow-up post.