📜  检查给定数字是否小于其倒数的两倍

📅  最后修改于: 2021-04-22 00:18:15             🧑  作者: Mango

给定整数N,任务是检查它是否是方程2 * reverse(N)– 1 = N的解

例子

天真的方法:最简单的方法是找到给定数字的倒数,并检查它是否满足等式2 * reverse(N)= N +1,然后分别打印“是”或“否”。

下面是上述方法的实现:

C++
// C++ program of the
// above approach
 
#include 
using namespace std;
 
// Iterative function to
// reverse digits of num
int rev(int num)
{
    int rev_num = 0;
 
    // Loop to extract all
    // digits of the number
    while (num > 0) {
        rev_num
            = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}
 
// Function to check if N
// satisfies given equation
bool check(int n)
{
    return 2 * rev(n) == n + 1;
}
 
// Driver Code
int main()
{
    int n = 73;
    if (check(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program of the
// above approach
import java.util.*;
class GFG{
 
// Iterative function to
// reverse digits of num
static int rev(int num)
{
  int rev_num = 0;
 
  // Loop to extract all
  // digits of the number
  while (num > 0)
  {
    rev_num = rev_num * 10 +
              num % 10;
    num = num / 10;
  }
  return rev_num;
}
 
// Function to check if N
// satisfies given equation
static boolean check(int n)
{
  return 2 * rev(n) == n + 1;
}
 
// Driver Code
public static void main(String[] args)
{
  int n = 73;
  if (check(n))
    System.out.print("Yes");
  else
    System.out.print("No");
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program of the above approach
   
# Iterative function to
# reverse digits of num
def rev(num):
      
    rev_num = 0
    
    # Loop to extract all
    # digits of the number
    while (num > 0):
        rev_num = (rev_num * 10 +
                       num % 10)
        num = num // 10
       
    return rev_num
   
# Function to check if N
# satisfies given equation
def check(n):
     
    return (2 * rev(n) == n + 1)
   
# Driver Code
n = 73
 
if (check(n)):
    print("Yes")  
else:
    print("No")
 
# This code is contributed by code_hunt


C#
// C# program of the above approach
using System;
 
class GFG{
 
// Iterative function to
// reverse digits of num
static int rev(int num)
{
    int rev_num = 0;
     
    // Loop to extract all
    // digits of the number
    while (num > 0)
    {
        rev_num = rev_num * 10 +
                      num % 10;
        num = num / 10;
    }
    return rev_num;
}
 
// Function to check if N
// satisfies given equation
static bool check(int n)
{
    return 2 * rev(n) == n + 1;
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 73;
     
    if (check(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Amit Katiyar


Javascript


C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check y is a power of x
bool isPower(int x, int y)
{
    // logarithm function to
    // calculate value
    int res1 = log(y) / log(x);
    double res2 = log(y) / log(x);
 
    // Compare to the result1 or
    // result2 both are equal
    return (res1 == res2);
}
 
// Function to check if N
// satisfies the equation
// 2 * reverse(n) = n + 1
bool check(int n)
{
    int x = (n + 7) / 8;
    if ((n + 7) % 8 == 0
        && isPower(10, x))
        return true;
    else
        return false;
}
 
// Driver Code
int main()
{
    int n = 73;
    if (check(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
   
// Function to check y is a power of x
static boolean isPower(int x, int y)
{
     
    // logarithm function to
    // calculate value
    double res1 = Math.log(y) / Math.log(x);
    double res2 = Math.log(y) / Math.log(x);
   
    // Compare to the result1 or
    // result2 both are equal
    return (res1 == res2);
}
   
// Function to check if N
// satisfies the equation
// 2 * reverse(n) = n + 1
static boolean check(int n)
{
    int x = (n + 7) / 8;
     
    if ((n + 7) % 8 == 0 &&
        isPower(10, x))
        return true;
    else
        return false;
}
   
// Driver Code
public static void main (String[] args)
{
    int n = 73;
     
    if (check(n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
import math
 
# Function to check y is a power of x
def isPower(x, y):
     
    # logarithm function to
    # calculate value
    res1 = math.log(y) // math.log(x)
    res2 = math.log(y) // math.log(x)
   
    # Compare to the result1 or
    # result2 both are equal
    return (res1 == res2)
  
# Function to check if N
# satisfies the equation
# 2 * reverse(n) = n + 1
def check(n):
     
    x = (n + 7) // 8
     
    if ((n + 7) % 8 == 0 and
        isPower(10, x)):
        return True
    else:
        return False
  
# Driver Code
n = 73
 
if (check(n) != 0):
    print("Yes")
else:
    print("No")
 
# This code is contributed by code_hunt


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
   
// Function to check y is a power of x
static bool isPower(int x, int y)
{
     
    // logarithm function to
    // calculate value
    double res1 = Math.Log(y) / Math.Log(x);
    double res2 = Math.Log(y) / Math.Log(x);
   
    // Compare to the result1 or
    // result2 both are equal
    return (res1 == res2);
}
   
// Function to check if N
// satisfies the equation
// 2 * reverse(n) = n + 1
static bool check(int n)
{
    int x = (n + 7) / 8;
     
    if ((n + 7) % 8 == 0 &&
        isPower(10, x))
        return true;
    else
        return false;
}
   
// Driver Code
static public void Main ()
{
    int n = 73;
     
    if (check(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
Yes

高效方法:优化上述方法的主要观察结果是,满足给定方程的数可以表示为:

要检查数字X是否满足上述公式,需要检查数字(X + 7)/ 8是否为10的幂。

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check y is a power of x
bool isPower(int x, int y)
{
    // logarithm function to
    // calculate value
    int res1 = log(y) / log(x);
    double res2 = log(y) / log(x);
 
    // Compare to the result1 or
    // result2 both are equal
    return (res1 == res2);
}
 
// Function to check if N
// satisfies the equation
// 2 * reverse(n) = n + 1
bool check(int n)
{
    int x = (n + 7) / 8;
    if ((n + 7) % 8 == 0
        && isPower(10, x))
        return true;
    else
        return false;
}
 
// Driver Code
int main()
{
    int n = 73;
    if (check(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Java

// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
   
// Function to check y is a power of x
static boolean isPower(int x, int y)
{
     
    // logarithm function to
    // calculate value
    double res1 = Math.log(y) / Math.log(x);
    double res2 = Math.log(y) / Math.log(x);
   
    // Compare to the result1 or
    // result2 both are equal
    return (res1 == res2);
}
   
// Function to check if N
// satisfies the equation
// 2 * reverse(n) = n + 1
static boolean check(int n)
{
    int x = (n + 7) / 8;
     
    if ((n + 7) % 8 == 0 &&
        isPower(10, x))
        return true;
    else
        return false;
}
   
// Driver Code
public static void main (String[] args)
{
    int n = 73;
     
    if (check(n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by code_hunt

Python3

# Python3 program to implement
# the above approach
import math
 
# Function to check y is a power of x
def isPower(x, y):
     
    # logarithm function to
    # calculate value
    res1 = math.log(y) // math.log(x)
    res2 = math.log(y) // math.log(x)
   
    # Compare to the result1 or
    # result2 both are equal
    return (res1 == res2)
  
# Function to check if N
# satisfies the equation
# 2 * reverse(n) = n + 1
def check(n):
     
    x = (n + 7) // 8
     
    if ((n + 7) % 8 == 0 and
        isPower(10, x)):
        return True
    else:
        return False
  
# Driver Code
n = 73
 
if (check(n) != 0):
    print("Yes")
else:
    print("No")
 
# This code is contributed by code_hunt

C#

// C# program to implement
// the above approach
using System;
 
class GFG{
   
// Function to check y is a power of x
static bool isPower(int x, int y)
{
     
    // logarithm function to
    // calculate value
    double res1 = Math.Log(y) / Math.Log(x);
    double res2 = Math.Log(y) / Math.Log(x);
   
    // Compare to the result1 or
    // result2 both are equal
    return (res1 == res2);
}
   
// Function to check if N
// satisfies the equation
// 2 * reverse(n) = n + 1
static bool check(int n)
{
    int x = (n + 7) / 8;
     
    if ((n + 7) % 8 == 0 &&
        isPower(10, x))
        return true;
    else
        return false;
}
   
// Driver Code
static public void Main ()
{
    int n = 73;
     
    if (check(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by code_hunt

Java脚本


输出:
Yes

时间复杂度: O(log N)
辅助空间: O(1)