Posted on by Kalkicode
Code Number

Disarium Number

The problem being addressed is to determine whether a given number is a Disarium number or not. A Disarium number is a number that is equal to the sum of its own digits, each raised to the power of its respective position. This concept is used to classify numbers based on their properties related to their digit positions and values.

Problem Statement and Description

Given a positive integer, the goal is to determine whether it is a Disarium number or not. A Disarium number is one where the sum of the digits, each raised to the power of its respective position, equals the original number. For example, the number 89 is a Disarium number because 8^1 + 9^2 = 89.

Idea to Solve the Problem

To determine if a number is a Disarium number, we need to calculate the sum of its digits, each raised to the power of its respective position. Then, we compare the sum with the original number to decide if it's a Disarium number or not.

Pseudocode

digit_length(number):
    size = 0
    while number != 0:
        number /= 10
        increment size
    return size

is_disarium(number):
    sum = 0
    size = digit_length(number)
    auxiliary = number
    
    while number != 0:
        digit = number % 10
        sum += digit^size
        number /= 10
        decrement size
    
    if sum == auxiliary:
        print auxiliary, "Is Disarium Number"
    else:
        print auxiliary, "Is not Disarium Number"

main():
    #test here

Algorithm Explanation

  1. Create a function digit_length(number) that calculates the number of digits in the given number. It divides the number by 10 in a loop and increments the count until the number becomes 0.
  2. Create a function is_disarium(number) that determines whether a number is a Disarium number or not. It calculates the sum of the digits, each raised to the power of its respective position. Compare the sum with the original number to determine if they are equal.
  3. In the main() function, call is_disarium for different test cases.

Code Solution

//C Program
//Check if a given number is disarium or not
#include <stdio.h>
#include <math.h>
// count the number of digits in number 
int digit_length(int number) 
{ 
  int size = 0; 

  while (number!=0) 
  { 
    number/=10; 

    size++; 
  } 
  return size; 
} 

// Function to check whether a number is disarium or not 
void is_disarium(int number) 
{ 
  //set initial values
  int sum = 0; 

  //Find the length of given number
  int size = digit_length(number); 

  int auxiliary = number;

  while (number!=0) 
  { 
    // Get the last digit 
    int digit = number%10; 

    sum = sum + pow(digit,size); 
     
    //Remove last digit
    number/=10; 

    //Reduce the length size
    size--;
  } 

  if(sum == auxiliary)
  {
    printf("%d Is Disarium Number\n", auxiliary);
  } 
  else
  {
     printf("%d Is not Disarium Number\n", auxiliary);
  }
} 

//Driver code to check if number is disarium or not 
int main() 
{ 
  //Test Case
  is_disarium(89);
  is_disarium(72);
  is_disarium(9);
  return 0; 
} 

Output

89 Is Disarium Number
72 Is not Disarium Number
9 Is Disarium Number
#include<iostream>
#include <math.h>
using namespace std;
/*
 C++ Program
 Check if a given number is disarium or not
*/
class MyNumber {
	public:

		//Count the number of digits in number 
		int digit_length(int number) {
			int size = 0;
			while (number != 0) {
				number /= 10;
				size++;
			}
			return size;
		}
	// Function to check whether a number is disarium or not 
	void is_disarium(int number) {
		//set initial values
		int sum = 0;
		//Find the length of given number
		int size = this->digit_length(number);
		int auxiliary = number;
		while (auxiliary != 0) {
			// Get the last digit 
			int digit = auxiliary % 10;
			sum = sum + pow(digit,size); 
			//Remove last digit
			auxiliary /= 10;
			//Reduce the length size
			size--;
		}
		if (sum == number) {
			cout << number << " Is Disarium Number\n";
		} else {
			cout << number << " Is not Disarium Number\n";
		}
	}
};
int main() {
	MyNumber obj;
	// Test Case
	obj.is_disarium(89);
	obj.is_disarium(72);
	obj.is_disarium(9);
	return 0;
}

Output

89 Is Disarium Number
72 Is not Disarium Number
9 Is Disarium Number
/*
  Java Program
  Check if a given number is disarium or not
*/

public class MyNumber {

  //Count the number of digits in number 
  public int digit_length(int number) 
  { 
    int size = 0; 

    while (number!=0) 
    { 
      number/=10; 

      size++; 
    } 
    return size; 
  } 

  // Function to check whether a number is disarium or not 
  public void is_disarium(int number) 
  { 
    //set initial values
    int sum = 0; 

    //Find the length of given number
    int size = digit_length(number); 

    int auxiliary = number;

    while (auxiliary!=0) 
    { 
      // Get the last digit 
      int digit = auxiliary%10; 

      sum =  sum + (int)Math.pow(digit,size); 
       
      //Remove last digit
      auxiliary/=10; 

      //Reduce the length size
      size--;
    } 

    if(sum == number)
    {
      System.out.print(number+" Is Disarium Number\n");
    } 
    else
    {
      System.out.print(number+" Is not Disarium Number\n");
    }
  } 
  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
    // Test Case

    obj.is_disarium(89);
    obj.is_disarium(72);
    obj.is_disarium(9);
  }
}

Output

89 Is Disarium Number
72 Is not Disarium Number
9 Is Disarium Number
/*
  C# Program
  Check if a given number is disarium or not
*/
using System;
public class MyNumber {

	//Count the number of digits in number 
	public int digit_length(int number) {
		int size = 0;

		while (number != 0) {
			number /= 10;

			size++;
		}
		return size;
	}

	// Function to check whether a number is disarium or not 
	public void is_disarium(int number) {
		//set initial values
		int sum = 0;

		//Find the.Length of given number
		int size = digit_length(number);

		int auxiliary = number;

		while (auxiliary != 0) {
			// Get the last digit 
			int digit = auxiliary % 10;

			sum = sum + (int) Math.Pow(digit, size);

			//Remove last digit
			auxiliary /= 10;

			//Reduce the.Length size
			size--;
		}

		if (sum == number) {
			Console.Write(number + " Is Disarium Number\n");
		} else {
			Console.Write(number + " Is not Disarium Number\n");
		}
	}
	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		// Test Case

		obj.is_disarium(89);
		obj.is_disarium(72);
		obj.is_disarium(9);
	}
}

Output

89 Is Disarium Number
72 Is not Disarium Number
9 Is Disarium Number
# Python 3 Program
# Check if a given number is disarium or not

class MyNumber :
  #Count the number of digits in number 
  def digit_length(self, number) :
    size = 0
    while (number != 0) :
      number = int(number/ 10)
      size += 1
    
    return size
  
  # Function to check whether a number is disarium or not 
  def is_disarium(self, number) :
    #set initial values
    sum_num = 0
    #Find the length of given number
    size = self.digit_length(number)
    auxiliary = number
    while (auxiliary != 0) :
      # Get the last digit 
      digit = auxiliary % 10
      
   
      sum_num = sum_num +  digit**size
 
   
      #Remove last digit
      auxiliary=int(auxiliary / 10)
      #Reduce the length size
      size -= 1
    
    if (sum_num == number) :
      print(number ," Is Disarium Number")
    else :
      print(number ," Is not Disarium Number")
    
  

def main() :
  obj = MyNumber()
  # Test Case
  obj.is_disarium(89)
  obj.is_disarium(72)
  obj.is_disarium(9)


if __name__ == "__main__":
  main()

Output

89 Is Disarium Number
72 Is not Disarium Number
9 Is Disarium Number
# Ruby Program 
#  Check if a given number is disarium or not

class MyNumber 
	#Count the number of digits in number 
	def digit_length(number) 
		size = 0
		while (number != 0) 
			number /= 10
			size += 1
		end
		return size
	end
	# Function to check whether a number is disarium or not 
	def is_disarium(number) 
		#set initial values
		sum = 0
		#Find the length of given number
		size = self.digit_length(number)
		auxiliary = number
		while (auxiliary != 0) 
			# Get the last digit 
			digit = auxiliary % 10
			sum = sum + digit**size
			#Remove last digit
			auxiliary /= 10
			#Reduce the length size
			size -= 1
		end
		if (sum == number) 
			print(number ," Is Disarium Number\n")
		else 
			print(number ," Is not Disarium Number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	# Test Case
	obj.is_disarium(89)
	obj.is_disarium(72)
	obj.is_disarium(9)
end
main()

Output

89 Is Disarium Number
72 Is not Disarium Number
9 Is Disarium Number
/*
 Scala Program
 Check if a given number is disarium or not
*/
class MyNumber {
	//Count the number of digits in number 
	def digit_length(value: Int): Int = {
		var size: Int = 0;
      	var number: Int = value;
		while (number != 0) {
			number /= 10;
			size += 1;
		}
		return size;
	}
	// Function to check whether a number is disarium or not 
	def is_disarium(number: Int): Unit = {
		//set initial values
		var sum: Int = 0;
		//Find the length of given number
		var size: Int = this.digit_length(number);
		var auxiliary: Int = number;
		while (auxiliary != 0) {
			// Get the last digit 
			var digit: Int = auxiliary % 10;
			sum = sum + (scala.math.pow(digit, size)).toInt;
			//Remove last digit
			auxiliary /= 10;
			//Reduce the length size
			size -= 1;
		}
		if (sum == number) {
			print(s"$number Is Disarium Number\n");
		} else {
			print(s"$number Is not Disarium Number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		// Test Case
		obj.is_disarium(89);obj.is_disarium(72);obj.is_disarium(9);
	}
}

Output

89 Is Disarium Number
72 Is not Disarium Number
9 Is Disarium Number
/*
  Swift 4 Program
  Check if a given number is disarium or not
*/
import Foundation
class MyNumber {
	//Count the number of digits in number 
	func digit_length(_ value: Int) -> Int {
		var size: Int = 0;
      	var number: Int = value;
		while (number != 0) {
			number /= 10;
			size += 1;
		}
		return size;
	}
	// Function to check whether a number is disarium or not 
	func is_disarium(_ number: Int) {
		//set initial values
		var sum: Int = 0;
		//Find the length of given number
		var size: Int = self.digit_length(number);
		var auxiliary: Int = number;
      	var digit: Int=0;
		while (auxiliary != 0) {
			// Get the last digit 
			digit = auxiliary % 10;
			sum = sum + Int(pow(Double(digit), Double(size)));
			//Remove last digit
			auxiliary /= 10;
			//Reduce the length size
			size -= 1;
		}
		if (sum == number) {
			print(number ," Is Disarium Number");
		} else {
			print(number ," Is not Disarium Number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	// Test Case
	obj.is_disarium(89);
	obj.is_disarium(72);
	obj.is_disarium(9);
}
main();

Output

89  Is Disarium Number
72  Is not Disarium Number
9  Is Disarium Number
<?php
/*
  Php Program
  Check if a given number is disarium or not
*/
class MyNumber {
	//Count the number of digits in number 

	public 	function digit_length($number) {
		$size = 0;
		while ($number != 0) {
			$number = intval($number / 10);
			$size++;
		}
		return $size;
	}
	// Function to check whether a number is disarium or not 

	public 	function is_disarium($number) {
		//set initial values
		$sum = 0;
		//Find the length of given number
		$size = $this->digit_length($number);
		$auxiliary = $number;
		while ($auxiliary != 0) {
			// Get the last digit 
			$digit = $auxiliary % 10;
			$sum = $sum +pow($digit, $size);
			//Remove last digit
			$auxiliary= intval($auxiliary / 10);
			//Reduce the length size
			$size--;
		}
		if ($sum == $number) {
			echo($number ." Is Disarium Number\n");
		} else {
			echo($number ." Is not Disarium Number\n");
		}
	}
};

function main() {
	$obj = new MyNumber();
	// Test Case

	$obj->is_disarium(89);
	$obj->is_disarium(72);
	$obj->is_disarium(9);
}
main();

Output

89 Is Disarium Number
72 Is not Disarium Number
9 Is Disarium Number
/*
 Node Js Program
 Check if a given number is disarium or not
*/
class MyNumber {
	//Count the number of digits in number 
	digit_length(number) {
		var size = 0;
		while (number != 0) {
			number = parseInt(number / 10);
			size++;
		}
		return size;
	}
	// Function to check whether a number is disarium or not 
	is_disarium(number) {
		//set initial values
		var sum = 0;
		//Find the length of given number
		var size = this.digit_length(number);
		var auxiliary = number;
		while (auxiliary != 0) {
			// Get the last digit 
			var digit = auxiliary % 10;
			sum = sum + parseInt(Math.pow(digit, size));
			//Remove last digit
			auxiliary = parseInt(auxiliary / 10);
			//Reduce the length size
			size--;
		}
		if (sum == number) {
			process.stdout.write(number + " Is Disarium Number\n");
		} else {
			process.stdout.write(number + " Is not Disarium Number\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	// Test Case
	obj.is_disarium(89);
	obj.is_disarium(72);
	obj.is_disarium(9)
}
main();

Output

89 Is Disarium Number
72 Is not Disarium Number
9 Is Disarium Number

Time Complexity Analysis

For a given number n, the digit_length function calculates the number of digits by dividing the number by 10 until it becomes 0. This takes O(log n) time, where n is the number of digits in the input number. The is_disarium function iterates through the digits of the number, performing constant-time operations for each digit. Therefore, the time complexity of the algorithm is O(log n), where n is the value of the input number.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment