📜  亚当数

📅  最后修改于: 2021-05-06 08:06:11             🧑  作者: Mango

给定数字n,编写程序检查给定数字是否为亚当编号。
亚当数是一个被反转的数字,该数字的平方和被反转的数字的平方应该是彼此相反的数字。最多1000的亚当数是:0、1、2、3、11、12、13、21、22、31、101、102、103、111、112、113、121、122、201、202、211、212, 221、301、311。

例子 :

Input : 12
Output : Adam Number
Explanation: 
Square of 12 = 144
Reverse of 12 = 21
Square of 21 = 441
Now Square of 12 and square of reverse of 12 are 
reverse of each other. Therefore 12 is Adam number.

Input : 14
Output : Not a Adam Number

查找亚当编号的步骤:

Step 1:  Find square of number. 
Step 2:  Find reverse of number.
Step 3:  Find square of number in Step 2.
Step 4:  Find reverse of number in Step 3.
Step 5:  Find whether number obtained in Step 4 and Step 1 are equal or not.
C/C++
// CPP program to check Adam Number
#include 
using namespace std;
  
// To reverse Digits of numbers
int reverseDigits(int num)
{
    int rev = 0;
    while (num > 0)
    {
        rev = rev * 10 + num % 10;
        num /= 10;
    }
    return rev;
}
  
// To square number
int square(int num)
{
    return (num * num);
}
  
// To check Adam Number
bool checkAdamNumber(int num)
{
    // Square first number and square
    // reverse digits of second number 
    int a = square(num);
    int b = square(reverseDigits(num));
      
    // If reverse of b equals a then given
    // number is Adam number
    if (a == reverseDigits(b))
    return true;
    return false;        
}
  
// Driver program to test above functions
int main()
{
    int num = 12;
      
    if (checkAdamNumber(num))
    cout << "Adam Number";
    else
    cout << "Not a Adam Number";    
      
    return 0;
}


Java
// Java program to check Adam Number
public class Main {
    // To reverse Digits of numbers
    static int reverseDigits(int num)
    {
        int rev = 0;
        while (num > 0)
        {
            rev = rev * 10 + num % 10;
            num /= 10;
        }
        return rev;
    }
  
    // To square number
    static int square(int num)
    {
        return (num * num);
    }
  
    // To check Adam Number
    static boolean checkAdamNumber(int num)
    {
        // Square first number and square
        // reverse digits of second number 
        int a = square(num);
        int b = square(reverseDigits(num));
          
        // If reverse of b equals a then given
        // number is Adam number
        if (a == reverseDigits(b))
        return true;
        return false;        
    }
  
    // Driver program to test above functions
    public static void main(String[] args)
    {
        int num = 12;
          
        if (checkAdamNumber(num))
        System.out.println("Adam Number");
        else
        System.out.println("Not a Adam Number");    
    }
  
}


Python
# Python program to check Adam Number
# To reverse Digits of numbers
def reverseDigits(num) :
    rev = 0
    while (num > 0) :
        rev = rev * 10 + num % 10
        num /= 10
          
    return rev
  
# To square number
def square(num) :
    return (num * num)
      
# To check Adam Number
def checkAdamNumber(num) :
      
    # Square first number and square
    # reverse digits of second number 
    a = square(num)
    b = square(reverseDigits(num))
          
    # If reverse of b equals a then given
    # number is Adam number
    if (a == reverseDigits(b)) :
        return True
    else :
        return False
  
# Driver program to test above functions
num = 13
if (checkAdamNumber(num)) :
    print "Adam Number"
else :
    print "Not a Adam Number"
  
  
# This code is contributed by Nikita Tiwari


C#
// C# Program to Check Adam Number
using System;
  
public class main {
  
    // To reverse Digits of numbers
    static int reverseDigits(int num)
    {
        int rev = 0;
        while (num > 0)
        {
            rev = rev * 10 + num % 10;
            num /= 10;
        }
        return rev;
    }
  
    // To square number
    static int square(int num)
    {
        return (num * num);
    }
  
    // To check Adam Number
    static bool checkAdamNumber(int num)
    {
        // Square first number and square
        // reverse digits of second number 
        int a = square(num);
        int b = square(reverseDigits(num));
          
        // If reverse of b equals a then 
        // given number is Adam number
        if (a == reverseDigits(b))
        return true;
        return false; 
    }
  
    // Driver Code
    public static void Main(String []args)
    {
        int num = 12;
          
        if (checkAdamNumber(num))
        Console.WriteLine("Adam Number");
        else
        Console.WriteLine("Not a Adam Number"); 
    }
  
}
  
// This code is contributed by vt_m.


PHP
 0)
    {
        $rev = $rev * 10 + $num % 10;
        $num = (int) $num / 10;
    }
    return $rev;
}
  
// To square number
function square($num)
{
    return ($num * $num);
}
  
// To check Adam Number
function checkAdamNumber($num)
{
    // Square first number and square
    // reverse digits of second number 
    $a = square($num);
    $b = square(reverseDigits($num));
      
    // If reverse of b equals a then
    // given number is Adam number
    if ($a == reverseDigits($b))
    return 0;
    return -1; 
}
  
// Driver Code
$num = 12;
  
if (checkAdamNumber($num))
echo "Adam Number";
else
echo "Not a Adam Number"; 
  
// This code is contributed by aj_36
?>


输出 :

Adam Number