Count Unary Numbers in a Range
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++
#include <iostream>
using namespace std;
bool isUnary( int n)
{
if (n == 1 || n == 7)
return true ;
else if (n / 10 == 0)
return false ;
int x, sum = 0;
while (n != 0) {
x = n % 10;
sum = sum + x * x;
n = n / 10;
}
isUnary(sum);
}
int countUnary( int a, int b)
{
int count = 0;
for ( int i = a; i <= b; i++) {
if (isUnary(i) == 1)
count++;
}
return count;
}
int main()
{
int a = 1000, b = 1099;
cout << countUnary(a, b);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isUnary( int n)
{
if (n == 1 || n == 7 )
return true ;
else if (n / 10 == 0 )
return false ;
int x, sum = 0 ;
while (n != 0 ) {
x = n % 10 ;
sum = sum + x * x;
n = n / 10 ;
}
return isUnary(sum);
}
static int countUnary( int a, int b)
{
int count = 0 ;
for ( int i = a; i <= b; i++) {
if (isUnary(i) == true )
count++;
}
return count;
}
public static void main (String[] args) {
int a = 1000 , b = 1099 ;
System.out.println (countUnary(a, b));
}
}
|
Python3
def isUnary(n):
if (n = = 1 or n = = 7 ):
return True
elif ( int (n / 10 ) = = 0 ):
return False
sum = 0
while (n ! = 0 ):
x = n % 10
sum = sum + x * x
n = int (n / 10 )
return isUnary( sum )
def countUnary(a, b):
count = 0
for i in range (a, b + 1 , 1 ):
if (isUnary(i) = = 1 ):
count + = 1
return count
if __name__ = = '__main__' :
a = 1000
b = 1099
print (countUnary(a, b))
|
C#
using System;
public class GFG {
static bool isUnary( int n)
{
/// Base case. Note that if we repeat
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);
}
static int countUnary( int a, int b)
{
int count = 0;
for ( int i = a; i <= b; i++) {
if (isUnary(i) == true )
count++;
}
return count;
}
public static void Main () {
int a = 1000, b = 1099;
Console.WriteLine(countUnary(a, b));
}
}
|
Javascript
<script>
function isUnary(n)
{
if (n == 1 || n == 7)
return true ;
else if (parseInt(n / 10, 10) == 0)
return false ;
let x, sum = 0;
while (n != 0) {
x = n % 10;
sum = sum + x * x;
n = parseInt(n / 10, 10);
}
return isUnary(sum);
}
function countUnary(a, b)
{
let count = 0;
for (let i = a; i <= b; i++) {
if (isUnary(i) == true )
count++;
}
return count;
}
let a = 1000, b = 1099;
document.write(countUnary(a, b));
</script>
|
Last Updated :
26 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...