FOR FREE YEAR SOLVED

Disarium Number between a range

Back to Programming

Description

Before seeing the program please follow this program check Disarium Number.

A number is said to be disarium number if sum of its digits powered with their position is same as the original number

 

For example, if the number is 135, then the sum of the digits powered with their position can be calculated as: 

11+32+53 = 1+9+125 = 135, this is equal to the original number. Therefore, 135 is a disarium number.

If the number is 125, then the sum of the digits powered with their position is:

11+22+53 = 1+4+125 = 130, this is not equal to the original number. Therefore, 125 is not a disarium number.

 

For this program, the ranges are taken as input. For each number between the ranges, the number of digits of the number is counted and according to the position the digits are powered and added. After completing, the sum of the power of the digits are compared with the original number, if both are same, then the number is displayed as output, otherwise not.

Algorithm

INPUT: The ranges
OUTPUT: The disarium numbers between the range
PROCESS:
Step 1: [Taking the inputs]
	Read m, n [The ranges]
Step 2: [Finding the disarium numbers]
	For i=m to n repeat
		Set d<-0
Set s<-0
		[Counting the number of digits of the given number]
		Set tmp<-i
		While tmp>0 repeat
			Set d<-d+1
			Set tmp<-tmp/10
		[End of ‘while’ loop]
		[Finding the sum of the power of digits]
		Set tmp<-i
		While tmp>0 repeat
			[Power is calculated according the position of the digits]
			Set s<-s+(tmp mod 10)d
			Set tmp<-tmp/10
			Set d<-d-1
		[End of ‘while’ loop]
		[Checking the condition for disarium number]
		If s=i then
			Print i
		[End of ‘if’]
	[End of ‘for’ loop]
Step 3: Stop.

Code

Time Complexity:

for(i=m;i<=n;i++)------------------------------------------O(p)

                {              //counting the number of digits of the given number

                                tmp=i;

                                d=0;s=0;

                                while(tmp>0)-------------------------------------O(n) [‘n’ is the number of digits]

                                {              d++;

                                                tmp/=10;   }

                                //finding the sum of the power of digits

                                tmp=i;

                                while(tmp>0)-------------------------------------O(n) [‘n’ is the number of digits]

                                {                //power is calculated according the position

                                                //of the digits

                                                s=s+pow(tmp%10,d);

                                                tmp/=10;

                                                d--;

                                }

                                //checking the condition for disarium number

                                if(s==i)-----------------------------------------------O(1)

                                                printf("%d ",i);     }

The time complexity of this program is O(p*n) where ‘p’ is the numbers between the ranges and ‘n’ is the number of digits of each number between the ranges.

 

Space Complexity:

The space complexity of this program is O(1) as it requires a constant number of spaces to execute for any given input.