Table of contents
1.
Introduction
2.
Disarium Number
3.
Examples
4.
Implementation 
4.1.
C++
4.2.
Java
4.3.
Python
5.
Time & Space Complexity
5.1.
Time Complexity
5.2.
Space Complexity
6.
Frequently Asked Questions
6.1.
Can negative numbers be disarium numbers?
6.2.
Is 0 a disarium number?
6.3.
Are there any even disarium numbers?
7.
Conclusion
Last Updated: Aug 3, 2024
Easy

Disarium Number in Java

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A disarium number is a special type of number where the sum of its digits powered with their respective position is equal to the original number itself. In simpler terms, if you take each digit of a disarium number, raise it to the power of its position, & then add up all these values, you will get back the same disarium number you started with. These unique numbers have interesting mathematical properties that make them interesting to study. 

Disarium Number in Java

In this article, we will learn what disarium numbers are, look at some examples, & learn how to check if a number is a disarium number or not. At the end, we will discuss their space and time complexities also.

Disarium Number

A disarium number is a number that satisfies a specific property based on its digits & their positions. To understand what makes a number a disarium number, let's discuss it step by step:

1. Consider each digit of the number separately.
 

2. Raise each digit to the power of its position in the number, starting from left to right. The leftmost digit has position 1, the next digit has position 2, & so on.
 

3. Add up all the resulting values from step 2.
 

4. If the sum obtained in step 3 is equal to the original number, then it is a disarium number.


Let's look at an example to explain this concept. Consider the number 175:

- The leftmost digit is 1, so we raise it to the power of its position (1^1 = 1).
 

- The next digit is 7, so we raise it to the power of its position (7^2 = 49).
 

- The last digit is 5, so we raise it to the power of its position (5^3 = 125).
 

- Now, we add up these values: 1 + 49 + 125 = 175.


Since the sum is equal to the original number, 175 is a disarium number.

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++
  • Java
  • Python

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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass