Given two numbers A and B, A<=B, the task is to find the number of unary numbers between A and B, both inclusive.
Unary Number: Consider the number 28. If we take the sum of square of its digits, 2*2 + 8*8, we get 68. Taking the sum of squares of digits again, we get 6*6 + 8*8=100. Doing this again, we get 1*1 + 0*0 + 0*0 = 1. Any such number, which ultimately leads to 1, is called a unary number.
Input : A = 1, B = 10 Output : 3 Input : A = 100, B = 150 Output : 7
The idea is to recursively calculate sum of squares of digits of the number and every time recurring down replace the number with calculated sum.
The base cases of the recursion will be:
- If the sum if reduced to either 1 or 7, then answer is true.
- If the sum if reduced to a single digit integer other than 1 and 7, answer is false.
Below is the recursive implementation of this problem:
- Count numbers with unit digit k in given range
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count of Numbers in a Range where digit d occurs exactly K times
- Count numbers in range such that digits in it and it's product with q are unequal
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Sum of all the prime numbers in a given range
- Count and Print the alphabets having ASCII value not in the range [l, r]
- Count and Print the alphabets having ASCII value in the range [l, r]
- Numbers in a Range with given Digital Root
- Print all Good numbers in given range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.