📜  计算范围内的一元数

📅  最后修改于: 2021-05-04 14:46:09             🧑  作者: Mango

给定两个数字A和B,A <= B,任务是查找A和B之间的一元数(包括两个数)。

一元数:考虑数字28。如果我们取其数字的平方和2 * 2 + 8 * 8,则得出68。再次取数字的平方和,得出6 * 6 + 8 * 8 = 100再次这样做,我们得到1 * 1 + 0 * 0 + 0 * 0 =1。任何最终导致为1的此类数字都称为一元数。

例子:

Input : A = 1, B = 10
Output : 3

Input : A = 100, B = 150
Output : 7

这个想法是递归地计算数字的位数的平方和,并且每次向下递归都用计算出的和替换数字。

递归的基本情况将是:

  • 如果总和减少到1或7,则答案为true。
  • 如果将总和减少为1和7以外的一位整数,则答案为假。

下面是此问题的递归实现:

C++
// CPP program to count unary numbers
// in a range
  
#include 
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;
}


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    
}


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


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 
}


输出:
13