Disarium Number

Easy
170.

Disarium Number

Disarium Number is an easy coding problem on math, asked in Infosys, Capgemini and Virtusa interviews.

A disarium number is a number where the sum of its digits, each raised to the power of its 1-based position, equals the number itself.

Given a positive integer n, return true if n is a disarium number.

Example 1

Input
n = 89
Output
true
Explanation
8¹ + 9² = 8 + 81 = 89.

Example 2

Input
n = 135
Output
true
Explanation
1¹ + 3² + 5³ = 1 + 9 + 125 = 135.

Example 3

Input
n = 76
Output
false
Explanation
7¹ + 6² = 43, not 76.

Constraints

  • 1 <= n <= 1000000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} n
 * @return {boolean}
 */
var isDisarium = function(n) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Disarium Number — Solution in Python, Java & C++

Disarium Number solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on math.

Disarium Number is an easy coding problem on math, asked in Infosys, Capgemini and Virtusa interviews.

  • Difficulty: Easy
  • Topics: Math
  • Asked at: Infosys, Capgemini, Virtusa
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

A disarium number is a number where the sum of its digits, each raised to the power of its 1-based position, equals the number itself.

Given a positive integer n, return true if n is a disarium number.

Example 1

Input: n = 89
Output: true
Explanation: 8¹ + 9² = 8 + 81 = 89.

Example 2

Input: n = 135
Output: true
Explanation: 1¹ + 3² + 5³ = 1 + 9 + 125 = 135.

Example 3

Input: n = 76
Output: false
Explanation: 7¹ + 6² = 43, not 76.

Constraints

  • 1 <= n <= 1000000

Hints for Disarium Number

  1. Hint 1

    Positions run left to right starting at 1, so the leftmost digit gets exponent 1.

  2. Hint 2

    That makes the string form convenient: index i carries exponent i + 1.

  3. Hint 3

    Contrast this with an Armstrong number, where every digit shares the same exponent.

How to solve Disarium Number

Positions count from the left starting at 1, so index i of the decimal string carries exponent i + 1. Sum digit^(i+1) and compare.

Approach

  1. Write n as a digit string.
  2. For each index i, add digit[i] ^ (i + 1) to a total.
  3. Return total == n.

Why it works

Unlike an Armstrong number, where every digit shares the same exponent, a disarium number weights each digit by its position; iterating the string left to right lines the exponent up with the position directly.

Complexity

  • Time — O(d)
  • Space — O(d)

Pitfalls

  • Peeling digits from the right gives positions in reverse; either count the digits first or work on the string.
  • 89 = 8¹ + 9² and 175 = 1 + 49 + 125 are handy test values.

Reference solution

Python

def isDisarium(n: int) -> bool:
    s = str(n)
    total = 0
    for i, ch in enumerate(s):
        total += int(ch) ** (i + 1)
    return total == n

Java

public static boolean isDisarium(int n) {
    String s = String.valueOf(n);
    long total = 0;
    for (int i = 0; i < s.length(); i++) {
        long d = s.charAt(i) - '0';
        long p = 1;
        for (int e = 0; e <= i; e++) p *= d;
        total += p;
    }
    return total == n;
}

C++

bool isDisarium(int n) {
    string s = to_string(n);
    long long total = 0;
    for (int i = 0; i < (int) s.size(); i++) {
        long long d = s[i] - '0';
        long long p = 1;
        for (int e = 0; e <= i; e++) p *= d;
        total += p;
    }
    return total == (long long) n;
}

JavaScript

var isDisarium = function(n) {
    const s = String(n);
    let total = 0;
    for (let i = 0; i < s.length; i++) {
        total += Math.pow(s.charCodeAt(i) - 48, i + 1);
    }
    return total === n;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 337 math problems · the whole catalogue