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.

**Examples:**

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:

## C++

`// CPP program to count unary numbers ` `// in a range ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to check if a number is unary ` `bool` `isUnary(` `int` `n) ` `{ ` ` ` `/// Base case. Note that if we repeat ` ` ` `// above process for 7, we get 1. ` ` ` `if` `(n == 1 || n == 7) ` ` ` `return` `true` `; ` ` ` `else` `if` `(n / 10 == 0) ` ` ` `return` `false` `; ` ` ` ` ` `/// rec case ` ` ` `int` `x, sum = 0; ` ` ` `while` `(n != 0) { ` ` ` `x = n % 10; ` ` ` `sum = sum + x * x; ` ` ` `n = n / 10; ` ` ` `} ` ` ` ` ` `isUnary(sum); ` `} ` ` ` `// Function to count unary numbers ` `// in a range ` `int` `countUnary(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `i = a; i <= b; i++) { ` ` ` `if` `(isUnary(i) == 1) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a = 1000, b = 1099; ` ` ` ` ` `cout << countUnary(a, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java program to count unary numbers ` `// in a range ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to check if a number is unary ` `static` `boolean` `isUnary(` `int` `n) ` `{ ` ` ` `/// Base case. Note that if we repeat ` ` ` `// above process for 7, we get 1. ` ` ` `if` `(n == ` `1` `|| n == ` `7` `) ` ` ` `return` `true` `; ` ` ` `else` `if` `(n / ` `10` `== ` `0` `) ` ` ` `return` `false` `; ` ` ` ` ` `/// rec case ` ` ` `int` `x, sum = ` `0` `; ` ` ` `while` `(n != ` `0` `) { ` ` ` `x = n % ` `10` `; ` ` ` `sum = sum + x * x; ` ` ` `n = n / ` `10` `; ` ` ` `} ` ` ` `return` `isUnary(sum); ` `} ` ` ` `// Function to count unary numbers ` `// in a range ` `static` `int` `countUnary(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = a; i <= b; i++) { ` ` ` `if` `(isUnary(i) == ` `true` `) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` ` ` `int` `a = ` `1000` `, b = ` `1099` `; ` ` ` `System.out.println (countUnary(a, b)); ` ` ` ` ` `} ` `//This code is contributed by ajit ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to count unary numbers ` `# in a range ` ` ` `# Function to check if a number is unary ` `def` `isUnary(n): ` ` ` ` ` `# Base case. Note that if we repeat ` ` ` `# above process for 7, we get 1. ` ` ` `if` `(n ` `=` `=` `1` `or` `n ` `=` `=` `7` `): ` ` ` `return` `True` ` ` `elif` `(` `int` `(n ` `/` `10` `) ` `=` `=` `0` `): ` ` ` `return` `False` ` ` ` ` `# rec case ` ` ` `sum` `=` `0` ` ` `while` `(n !` `=` `0` `): ` ` ` `x ` `=` `n ` `%` `10` ` ` `sum` `=` `sum` `+` `x ` `*` `x ` ` ` `n ` `=` `int` `(n ` `/` `10` `) ` ` ` ` ` `return` `isUnary(` `sum` `) ` ` ` `# Function to count unary numbers ` `# in a range ` `def` `countUnary(a, b): ` ` ` `count ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(a, b ` `+` `1` `, ` `1` `): ` ` ` `if` `(isUnary(i) ` `=` `=` `1` `): ` ` ` `count ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `1000` ` ` `b ` `=` `1099` ` ` ` ` `print` `(countUnary(a, b)) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`//C# program to count unary numbers ` `// in a range ` `using` `System; ` ` ` ` ` `public` `class` `GFG { ` ` ` `// Function to check if a number is unary ` `static` `bool` `isUnary(` `int` `n) ` `{ ` ` ` `/// Base case. Note that if we repeat ` ` ` `// above process for 7, we get 1. ` ` ` `if` `(n == 1 || n == 7) ` ` ` `return` `true` `; ` ` ` `else` `if` `(n / 10 == 0) ` ` ` `return` `false` `; ` ` ` ` ` `/// rec case ` ` ` `int` `x, sum = 0; ` ` ` `while` `(n != 0) { ` ` ` `x = n % 10; ` ` ` `sum = sum + x * x; ` ` ` `n = n / 10; ` ` ` `} ` ` ` `return` `isUnary(sum); ` `} ` ` ` `// Function to count unary numbers ` `// in a range ` `static` `int` `countUnary(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `i = a; i <= b; i++) { ` ` ` `if` `(isUnary(i) == ` `true` `) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` ` ` ` ` `public` `static` `void` `Main () { ` ` ` ` ` ` ` `int` `a = 1000, b = 1099; ` ` ` `Console.WriteLine(countUnary(a, b)); ` ` ` ` ` `} ` `//This code is contributed by 29AjayKumar ` `} ` |

*chevron_right*

*filter_none*

**Output:**

13

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count numbers in range such that digits in it and it's product with q are unequal
- 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 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 in a Range where digit d occurs exactly K times
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Array range queries to count the number of Fibonacci numbers with updates
- Array Range Queries to count Powerful numbers with updates
- Count of numbers with all digits same in a given range
- Range Queries for count of Armstrong numbers in subarray using MO's algorithm
- Count all prime numbers in a given range whose sum of digits is also prime
- Count of distinct Primonacci Numbers in a given range [L, R]
- Count numbers from a given range that contains a given number as the suffix
- Range and Coefficient of range of Array
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.