Examples
Now that we understand what disarium numbers are, let's look at a few more examples :
1. 89 is a disarium number:
- 8^1 = 8
- 9^2 = 81
- 8 + 81 = 89
2. 518 is a disarium number:
- 5^1 = 5
- 1^2 = 1
- 8^3 = 512
- 5 + 1 + 512 = 518
3. 1742 is a disarium number:
- 1^1 = 1
- 7^2 = 49
- 4^3 = 64
- 2^4 = 16
- 1 + 49 + 64 + 16 = 130 (not equal to 1742, so it's not a disarium number)
As we can see from these examples, disarium numbers can have varying lengths & digit combinations. The key is to carefully raise each digit to the power of its position & check if the sum matches the original number.
It's important to note that single-digit numbers are always disarium numbers because the sum of their digits raised to the power of their position will always be equal to the number itself. For example, 5 is a disarium number because 5^1 = 5.
Implementation
C++
#include <iostream>
#include <cmath>
#include <string>
bool isDisarium(int number) {
int originalNumber = number;
int position = std::to_string(number).length();
int sum = 0;
while (number > 0) {
int digit = number % 10;
sum += std::pow(digit, position);
number /= 10;
position--;
}
return sum == originalNumber;
}
int main() {
int number = 175;
if (isDisarium(number)) {
std::cout << number << " is a disarium number." << std::endl;
} else {
std::cout << number << " is not a disarium number." << std::endl;
}
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Java
public class DisariumNumber {
public static boolean isDisarium(int number) {
int originalNumber = number;
int position = String.valueOf(number).length();
int sum = 0;
while (number > 0) {
int digit = number % 10;
sum += Math.pow(digit, position);
number /= 10;
position--;
}
return sum == originalNumber;
}
public static void main(String[] args) {
int number = 175;
if (isDisarium(number)) {
System.out.println(number + " is a disarium number.");
} else {
System.out.println(number + " is not a disarium number.");
}
}
}

You can also try this code with Online Java Compiler
Run Code
Python
def is_disarium(number):
original_number = number
position = len(str(number))
sum = 0
while number > 0:
digit = number % 10
sum += digit ** position
number //= 10
position -= 1
return sum == original_number
# Example usage
number = 175
if is_disarium(number):
print(f"{number} is a disarium number.")
else:
print(f"{number} is not a disarium number.")

You can also try this code with Online Python Compiler
Run Code
Output
175 is a disarium number.
Time & Space Complexity
Time Complexity
The time complexity of the `isDisarium` function depends on the number of digits in the input number. Let's denote the number of digits as `n`.
The main operations in the function are:
- Converting the number to a string and calculating its length, which takes O(n) time.
- Iterating through each digit of the number using a loop, which takes O(n) time.
- Performing constant-time operations like modulo, division, and exponentiation inside the loop, which takes O(1) time for each iteration.
Therefore, the overall time complexity of the `isDisarium` function is O(n), where n is the number of digits in the input number. The time complexity grows linearly with the number of digits.
Space Complexity
The space complexity of the `isDisarium` function is O(1) because it uses only a constant amount of extra space.
The function uses a few variables to store the original number, position, sum, and digit, but the number of variables remains constant regardless of the input size. It does not use any data structures that grow with the input size.
Converting the number to a string to calculate its length does require some additional memory, but the space required is proportional to the number of digits, which is already accounted for in the time complexity.
Therefore, the space complexity of the `isDisarium` function is O(1), which indicates that it uses a constant amount of extra space.
It's important to remember that the time and space complexity analysis assumes that basic arithmetic operations like modulo, division, and exponentiation take constant time. For very large numbers, these operations might have a slightly higher time complexity due to the increased number of bits required to represent the numbers. However, for most practical situations, the time complexity of O(n) and the space complexity of O(1) is valid for the disarium number program.
Frequently Asked Questions
Can negative numbers be disarium numbers?
No, negative numbers cannot be disarium numbers because the concept of disarium numbers is defined only for positive integers.
Is 0 a disarium number?
No, 0 is not a disarium number because 0 raised to any power is still 0, & the sum of its digits (which is 0) is not equal to the original number.
Are there any even disarium numbers?
Yes, there are even disarium numbers. For example, 518 is an even disarium number since 5^1 + 1^2 + 8^3 = 518.
Conclusion
In this article, we discussed the fascinating concept of disarium numbers. We learned that a disarium number is a number where the sum of its digits raised to their respective positions is equal to the original number itself. We looked at different examples to understand how to identify disarium numbers. We also implemented a program in different languages to check if a given number is a disarium number & finally analyzed its time & space complexity.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.