📜  阿姆斯特朗编号程序

📅  最后修改于: 2021-05-06 23:34:13             🧑  作者: Mango

给定数字x,请确定给定数字是否为阿姆斯特朗数字。 n位数字的正整数称为if的n阶阿姆斯特朗数(order是位数)。

abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + .... 

例子:

Input : 153
Output : Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153

Input : 120
Output : No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9

Input : 1253
Output : No
1253 is not a Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723

Input : 1634
Output : Yes
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634

这个想法是首先计算数字位数(或查找顺序)。令数字为n。对于输入数字x中的每个数字r,计算r n 。如果所有这些值的总和等于n,则返回true,否则返回false。

C++
// C++ program to determine whether the number is
// Armstrong number or not
#include
using namespace std;
  
/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
    if( y == 0)
        return 1;
    if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    return x*power(x, y/2)*power(x, y/2);
}
  
/* Function to calculate order of the number */
int order(int x)
{
    int n = 0;
    while (x)
    {
        n++;
        x = x/10;
    }
    return n;
}
  
// Function to check whether the given number is
// Armstrong number or not
bool isArmstrong(int x)
{
    // Calling order function
    int n = order(x);
    int temp = x, sum = 0;
    while (temp)
    {
        int r = temp%10;
        sum += power(r, n);
        temp = temp/10;
    }
  
    // If satisfies Armstrong condition
    return (sum == x);
}
  
// Driver Program
int main()
{
    int x = 153;
    cout << isArmstrong(x) << endl;
    x = 1253;
    cout << isArmstrong(x) << endl;
    return 0;
}


C
// C program to find Armstrong number
  
#include 
  
/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    return x * power(x, y / 2) * power(x, y / 2);
}
  
/* Function to calculate order of the number */
int order(int x)
{
    int n = 0;
    while (x) {
        n++;
        x = x / 10;
    }
    return n;
}
  
// Function to check whether the given number is
// Armstrong number or not
int isArmstrong(int x)
{
    // Calling order function
    int n = order(x);
    int temp = x, sum = 0;
    while (temp) {
        int r = temp % 10;
        sum += power(r, n);
        temp = temp / 10;
    }
  
    // If satisfies Armstrong condition
    if (sum == x)
        return 1;
    else
        return 0;
}
  
// Driver Program
int main()
{
    int x = 153;
    if (isArmstrong(x) == 1)
        printf("True\n");
    else
        printf("False\n");
  
    x = 1253;
    if (isArmstrong(x) == 1)
        printf("True\n");
    else
        printf("False\n");
  
    return 0;
}


Java
// Java program to determine whether the number is
// Armstrong number or not
public class Armstrong
{
    /* Function to calculate x raised to the
       power y */
    int power(int x, long y)
    {
        if( y == 0)
            return 1;
        if (y%2 == 0)
            return power(x, y/2)*power(x, y/2);
        return x*power(x, y/2)*power(x, y/2);
    }
  
    /* Function to calculate order of the number */
    int order(int x)
    {
        int n = 0;
        while (x != 0)
        {
            n++;
            x = x/10;
        }
        return n;
    }
  
    // Function to check whether the given number is
    // Armstrong number or not
    boolean isArmstrong (int x)
    {
        // Calling order function
        int n = order(x);
        int temp=x, sum=0;
        while (temp!=0)
        {
            int r = temp%10;
            sum = sum + power(r,n);
            temp = temp/10;
        }
  
        // If satisfies Armstrong condition
        return (sum == x);
    }
  
    // Driver Program
    public static void main(String[] args)
    {
        Armstrong ob = new Armstrong();
        int x = 153;
        System.out.println(ob.isArmstrong(x));
        x = 1253;
        System.out.println(ob.isArmstrong(x));
    }
}


Python
# Python program to determine whether the number is
# Armstrong number or not
  
# Function to calculate x raised to the power y
def power(x, y):
    if y==0:
        return 1
    if y%2==0:
        return power(x, y/2)*power(x, y/2)
    return x*power(x, y/2)*power(x, y/2)
  
# Function to calculate order of the number
def order(x):
  
    # variable to store of the number
    n = 0
    while (x!=0):
        n = n+1
        x = x/10
    return n
  
# Function to check whether the given number is
# Armstrong number or not
def isArmstrong (x):
    n = order(x)
    temp = x
    sum1 = 0
    while (temp!=0):
        r = temp%10
        sum1 = sum1 + power(r, n)
        temp = temp/10
  
    # If condition satisfies
    return (sum1 == x)
  
  
# Driver Program
x = 153
print(isArmstrong(x))
x = 1253
print(isArmstrong(x))
Output:1
0


Python3
# python3 >= 3.6 for typehint support 
# This example avoids the complexity of ordering
# through type conversions & string manipulation 
  
def isArmstrong(val:int) -> bool:
      
    """val will be tested to see if its an Armstrong number. 
    Arguments:
        val {int} -- positive integer only. 
    Returns:
        bool -- true is /false isn't
    """
      
    # break the int into its respective digits
    parts = [int(_) for _ in str(val)] 
      
    # begin test.
    counter = 0
    for _ in parts:
        counter += _**3
    return ( counter == val ) 
  
# Check Armstrong number
print(isArmstrong(100))
  
print(isArmstrong(153))
  
# Get all the Armstrong number in range(1000)
print([ _ for _ in range(1000) if isArmstrong(_)])


C#
// C# program to determine whether the 
// number is Armstrong number or not
using System;
  
public class GFG
{
      
    // Function to calculate x raised
    // to the power y
    int power(int x, long y)
    {
        if( y == 0)
            return 1;
        if (y % 2 == 0)
            return power(x, y / 2) * 
                   power(x, y / 2);
                     
        return x * power(x, y / 2) *
                   power(x, y / 2);
    }
  
    // Function to calculate 
    // order of the number
    int order(int x)
    {
        int n = 0;
        while (x != 0)
        {
            n++;
            x = x / 10;
        }
        return n;
    }
  
    // Function to check whether the 
    // given number is Armstrong number
    // or not
    bool isArmstrong (int x)
    {
          
        // Calling order function
        int n = order(x);
        int temp = x, sum = 0;
        while (temp != 0)
        {
            int r = temp % 10;
            sum = sum + power(r, n);
            temp = temp / 10;
        }
  
        // If satisfies Armstrong condition
        return (sum == x);
    }
  
    // Driver Code
    public static void Main()
    {
        GFG ob = new GFG();
        int x = 153;
        Console.WriteLine(ob.isArmstrong(x));
        x = 1253;
        Console.WriteLine(ob.isArmstrong(x));
    }
}
  
// This code is contributed by Nitin Mittal.


C++
// C++ Program to find 
// Nth Armstrong Number
#include
#include
using namespace std;
  
// Function to find Nth Armstrong Number
int NthArmstrong(int n)
{
    int count=0;
      
    // upper limit from integer 
    for(int i = 1; i <= INT_MAX; i++)
    {
        int num=i, rem, digit=0, sum=0;
          
        //Copy the value for num in num 
        num = i;
          
        // Find total digits in num 
        digit = (int) log10(num) + 1;
          
        // Calculate sum of power of digits 
        while(num > 0)
        { 
            rem = num % 10;
            sum = sum + pow(rem,digit);
            num = num / 10;
        }
        // Check for Armstrong number 
        if(i == sum)
            count++;
        if(count==n)
            return i;
    }
}
  
// Driver Function
int main()
{
    int n = 12;
    cout<


Java
// Java Program to find 
// Nth Armstrong Number
import java.lang.Math;
  
class GFG
{
      
// Function to find Nth Armstrong Number
static int NthArmstrong(int n)
{
    int count = 0;
      
    // upper limit from integer 
    for(int i = 1; i <= Integer.MAX_VALUE; i++)
    {
        int num = i, rem, digit = 0, sum = 0;
          
        //Copy the value for num in num 
        num = i;
          
        // Find total digits in num 
        digit = (int) Math.log10(num) + 1;
          
        // Calculate sum of power of digits 
        while(num > 0)
        { 
            rem = num % 10;
            sum = sum + (int)Math.pow(rem, digit);
            num = num / 10;
        }
          
        // Check for Armstrong number 
        if(i == sum)
            count++;
        if(count == n)
            return i;
    }
    return n;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 12;
    System.out.println(NthArmstrong(n));
}
}
  
  
// This code is contributed by Code_Mech.


Python3
# Python3 Program to find Nth Armstrong Number
import math;
import sys;
  
# Function to find Nth Armstrong Number
def NthArmstrong(n):
  
    count = 0;
      
    # upper limit from integer 
    for i in range(1, sys.maxsize):
  
        num = i; 
        rem = 0;
        digit = 0;
        sum = 0;
          
        # Copy the value for num in num 
        num = i;
          
        # Find total digits in num 
        digit = int(math.log10(num) + 1);
          
        # Calculate sum of power of digits 
        while(num > 0):
            rem = num % 10;
            sum = sum + pow(rem, digit);
            num = num // 10;
          
        # Check for Armstrong number 
        if(i == sum):
            count += 1;
        if(count == n):
            return i;
  
# Driver Code
n = 12;
print(NthArmstrong(n));
  
# This code is contributed by chandan_jnu


C#
// C# Program to find 
// Nth Armstrong Number
using System;
  
class GFG
{
      
// Function to find Nth Armstrong Number
static int NthArmstrong(int n)
{
    int count = 0;
      
    // upper limit from integer 
    for(int i = 1; i <= int.MaxValue; i++)
    {
        int num = i, rem, digit = 0, sum = 0;
          
        // Copy the value for num in num 
        num = i;
          
        // Find total digits in num 
        digit = (int) Math.Log10(num) + 1;
          
        // Calculate sum of power of digits 
        while(num > 0)
        { 
            rem = num % 10;
            sum = sum + (int)Math.Pow(rem, digit);
            num = num / 10;
        }
          
        // Check for Armstrong number 
        if(i == sum)
            count++;
        if(count == n)
            return i;
    }
    return n;
}
  
// Driver Code
public static void Main()
{
    int n = 12;
    Console.WriteLine(NthArmstrong(n));
}
}
  
// This code is contributed by Code_Mech.


PHP
 0)
        { 
            $rem = $num % 10;
            $sum = $sum + pow($rem, 
                              $digit);
            $num = (int)$num / 10;
        }
          
        // Check for 
        // Armstrong number 
        if($i == $sum)
            $count++;
        if($count == $n)
            return $i;
    }
}
  
// Driver Code
$n = 12;
echo NthArmstrong($n);
  
// This Code is Contributed 
// by akt_mit
?>


输出:

True
False

查找n阿姆斯特朗号

Input  : 9
Output : 9

Input  : 10
Output : 153

C++

// C++ Program to find 
// Nth Armstrong Number
#include
#include
using namespace std;
  
// Function to find Nth Armstrong Number
int NthArmstrong(int n)
{
    int count=0;
      
    // upper limit from integer 
    for(int i = 1; i <= INT_MAX; i++)
    {
        int num=i, rem, digit=0, sum=0;
          
        //Copy the value for num in num 
        num = i;
          
        // Find total digits in num 
        digit = (int) log10(num) + 1;
          
        // Calculate sum of power of digits 
        while(num > 0)
        { 
            rem = num % 10;
            sum = sum + pow(rem,digit);
            num = num / 10;
        }
        // Check for Armstrong number 
        if(i == sum)
            count++;
        if(count==n)
            return i;
    }
}
  
// Driver Function
int main()
{
    int n = 12;
    cout<

Java

// Java Program to find 
// Nth Armstrong Number
import java.lang.Math;
  
class GFG
{
      
// Function to find Nth Armstrong Number
static int NthArmstrong(int n)
{
    int count = 0;
      
    // upper limit from integer 
    for(int i = 1; i <= Integer.MAX_VALUE; i++)
    {
        int num = i, rem, digit = 0, sum = 0;
          
        //Copy the value for num in num 
        num = i;
          
        // Find total digits in num 
        digit = (int) Math.log10(num) + 1;
          
        // Calculate sum of power of digits 
        while(num > 0)
        { 
            rem = num % 10;
            sum = sum + (int)Math.pow(rem, digit);
            num = num / 10;
        }
          
        // Check for Armstrong number 
        if(i == sum)
            count++;
        if(count == n)
            return i;
    }
    return n;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 12;
    System.out.println(NthArmstrong(n));
}
}
  
  
// This code is contributed by Code_Mech.

Python3

# Python3 Program to find Nth Armstrong Number
import math;
import sys;
  
# Function to find Nth Armstrong Number
def NthArmstrong(n):
  
    count = 0;
      
    # upper limit from integer 
    for i in range(1, sys.maxsize):
  
        num = i; 
        rem = 0;
        digit = 0;
        sum = 0;
          
        # Copy the value for num in num 
        num = i;
          
        # Find total digits in num 
        digit = int(math.log10(num) + 1);
          
        # Calculate sum of power of digits 
        while(num > 0):
            rem = num % 10;
            sum = sum + pow(rem, digit);
            num = num // 10;
          
        # Check for Armstrong number 
        if(i == sum):
            count += 1;
        if(count == n):
            return i;
  
# Driver Code
n = 12;
print(NthArmstrong(n));
  
# This code is contributed by chandan_jnu

C#

// C# Program to find 
// Nth Armstrong Number
using System;
  
class GFG
{
      
// Function to find Nth Armstrong Number
static int NthArmstrong(int n)
{
    int count = 0;
      
    // upper limit from integer 
    for(int i = 1; i <= int.MaxValue; i++)
    {
        int num = i, rem, digit = 0, sum = 0;
          
        // Copy the value for num in num 
        num = i;
          
        // Find total digits in num 
        digit = (int) Math.Log10(num) + 1;
          
        // Calculate sum of power of digits 
        while(num > 0)
        { 
            rem = num % 10;
            sum = sum + (int)Math.Pow(rem, digit);
            num = num / 10;
        }
          
        // Check for Armstrong number 
        if(i == sum)
            count++;
        if(count == n)
            return i;
    }
    return n;
}
  
// Driver Code
public static void Main()
{
    int n = 12;
    Console.WriteLine(NthArmstrong(n));
}
}
  
// This code is contributed by Code_Mech.

的PHP

 0)
        { 
            $rem = $num % 10;
            $sum = $sum + pow($rem, 
                              $digit);
            $num = (int)$num / 10;
        }
          
        // Check for 
        // Armstrong number 
        if($i == $sum)
            $count++;
        if($count == $n)
            return $i;
    }
}
  
// Driver Code
$n = 12;
echo NthArmstrong($n);
  
// This Code is Contributed 
// by akt_mit
?>


输出:

371


参考:

http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html
http://www.programiz.com/c-programming/examples/check-armstrong-number