XOR y OR de todos los números de Armstrong de N dígitos

Dado un número entero N , la tarea es encontrar los valores XOR y OR de todos los números de Armstrong de N dígitos .

Ejemplos

Entrada: N = 3 
Salida: XOR = 271, OR = 511 
153, 370, 371, 407 son los números armstrong de tres dígitos

Entrada: N = 4 
Salida: XOR = 880, OR = 10098 
 

Acercarse: 

  • Encuentre el número inicial y final del número de Armstrong de N dígitos por:
Starting N-digit Armstrong number = pow(10, n - 1)
Ending N-digit Armstrong number   = pow(10, n) - 1
  • Repita los números de Armstrong de N dígitos desde el número inicial hasta el número final y verifique si ese número es Armstrong o no.
  • Si el número es Armstrong, entonces tome XOR y OR de ese número por separado.
  • De lo contrario, continúe con la siguiente iteración e imprima el valor de XOR y OR después de todas las iteraciones.

A continuación se muestra la implementación del enfoque anterior:  

C++14

// C++ program to find the XOR
// and OR of all Armstrong numbers
// of N digits
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a number
// is Armstrong or not
bool isArmstrong(int x, int n)
{
    int sum1 = 0;
    int temp = x;
    while (temp > 0) {
        int digit = temp % 10;
        sum1 += (int)pow(digit, n);
        temp /= 10;
    }
    return sum1 == x;
}
 
// Function to find XOR of all
// N-digits Armstrong number
void CalculateXORandOR(int n)
{
 
    // To store the XOR and OR of all
    // Armstrong number
    int CalculateXOR = 0;
    int CalculateOR = 0;
 
    // Starting N-digit
    // Armstrong number
    int start = (int)pow(10, n - 1);
 
    // Ending N-digit
    // Armstrong number
    int end = (int)pow(10, n) - 1;
     
    // Iterate over starting and
    // ending number
    for (int i = start; i < end + 1; i++)
    {
 
        // To check if i is
        // Armstrong or not
        if (isArmstrong(i, n)) {
            CalculateXOR = CalculateXOR ^ i;
            CalculateOR = CalculateOR | i;
        }
    }
     
    // Print the XOR and OR of all
    // Armstrong number
    cout << "XOR = " << CalculateXOR << endl;
    cout << "OR = " << CalculateOR << endl;
}
 
// Driver Code
int main()
{
 
    int n = 4;
    CalculateXORandOR(n);
}
 
// This code is contributed by shivanisinghss2110

Java

// Java program to find the XOR
// and OR of all Armstrong numbers
// of N digits
class GFG
{
     
    // Function to check if a number
    // is Armstrong or not
    static boolean isArmstrong(int x, int n) {
        int sum1 = 0;
        int temp = x;
        while (temp > 0) {
            int digit = temp % 10;
            sum1 += Math.pow(digit, n);
            temp /= 10;
        }
        return sum1 == x;
    }
 
    // Function to find XOR of all
    // N-digits Armstrong number
    static void CalculateXORandOR(int n) {
 
        // To store the XOR and OR of all
        // Armstrong number
        int CalculateXOR = 0;
        int CalculateOR = 0;
 
        // Starting N-digit
        // Armstrong number
        int start = (int) Math.pow(10, n - 1);
 
        // Ending N-digit
        // Armstrong number
        int end = (int) (Math.pow(10, n)) - 1;
         
        // Iterate over starting and
        // ending number
        for (int i = start; i < end + 1; i++) {
 
            // To check if i is
            // Armstrong or not
            if (isArmstrong(i, n)) {
                CalculateXOR = CalculateXOR ^ i;
                CalculateOR = CalculateOR | i;
            }
        }
         
        // Print the XOR and OR of all
        // Armstrong number
        System.out.println("XOR = " + CalculateXOR);
        System.out.println("OR = " + CalculateOR);
    }
 
    // Driver Code
    public static void main(String[] args) {
 
        int n = 4;
        CalculateXORandOR(n);
    }
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program to find the XOR
# and OR of all Armstrong numbers
# of N digits
 
# Function to check if a number
# is Armstrong or not
def isArmstrong (x, n):
    sum1 = 0 
    temp = x 
    while temp > 0:
        digit = temp % 10
        sum1 += digit **n
        temp //= 10
    return sum1 == x
 
# Function to find XOR of all
# N-digits Armstrong number
def CalculateXORandOR(n) :
 
    # To store the XOR and OR of all
    # Armstrong number
    CalculateXOR = 0
    CalculateOR = 0
 
    # Starting N-digit
    # Armstrong number
    start = 10 ** (n - 1)
 
    # Ending N-digit
    # Armstrong number
    end = (10**n) - 1
    # Iterate over starting and
    # ending number
    for i in range( start, end + 1) :
 
        # To check if i is
        # Armstrong or not
        if (isArmstrong(i, n)) :
            CalculateXOR = CalculateXOR ^ i
            CalculateOR = CalculateOR | i
 
    # Print the XOR and OR of all
    # Armstrong number
    print("XOR = ", CalculateXOR)
    print("OR = ", CalculateOR)
 
# Driver Code
if __name__ == "__main__" :
 
    n = 4;
    CalculateXORandOR(n);

C#

// C# program to find the XOR
// and OR of all Armstrong numbers
// of N digits
using System;
 
class GFG
{
     
    // Function to check if a number
    // is Armstrong or not
    static bool isArmstrong(int x, int n) {
        int sum1 = 0;
        int temp = x;
        while (temp > 0) {
            int digit = temp % 10;
            sum1 += (int)Math.Pow(digit, n);
            temp /= 10;
        }
        return sum1 == x;
    }
 
    // Function to find XOR of all
    // N-digits Armstrong number
    static void CalculateXORandOR(int n) {
 
        // To store the XOR and OR of all
        // Armstrong number
        int CalculateXOR = 0;
        int CalculateOR = 0;
 
        // Starting N-digit
        // Armstrong number
        int start = (int) Math.Pow(10, n - 1);
 
        // Ending N-digit
        // Armstrong number
        int end = (int) (Math.Pow(10, n)) - 1;
         
        // Iterate over starting and
        // ending number
        for (int i = start; i < end + 1; i++) {
 
            // To check if i is
            // Armstrong or not
            if (isArmstrong(i, n)) {
                CalculateXOR = CalculateXOR ^ i;
                CalculateOR = CalculateOR | i;
            }
        }
         
        // Print the XOR and OR of all
        // Armstrong number
        Console.WriteLine("XOR = " + CalculateXOR);
        Console.WriteLine("OR = " + CalculateOR);
    }
 
    // Driver Code
    public static void Main(String[] args) {
 
        int n = 4;
        CalculateXORandOR(n);
    }
}
 
// This code is contributed by PrinciRaj1992

Javascript

<script>
 
// Javascript program to find the XOR
// and OR of all Armstrong numbers
// of N digits
 
// Function to check if a number
// is Armstrong or not
function isArmstrong(x, n)
{
    let sum1 = 0;
    let temp = x;
     
    while (temp > 0)
    {
        let digit = temp % 10;
        sum1 += Math.pow(digit, n);
        temp = parseInt(temp / 10, 10);
    }
    return (sum1 == x);
}
 
// Function to find XOR of all
// N-digits Armstrong number
function CalculateXORandOR(n)
{
     
    // To store the XOR and OR of all
    // Armstrong number
    let CalculateXOR = 0;
    let CalculateOR = 0;
 
    // Starting N-digit
    // Armstrong number
    let start = Math.pow(10, n - 1);
 
    // Ending N-digit
    // Armstrong number
    let end = (Math.pow(10, n)) - 1;
       
    // Iterate over starting and
    // ending number
    for(let i = start; i < end + 1; i++)
    {
         
        // To check if i is
        // Armstrong or not
        if (isArmstrong(i, n))
        {
            CalculateXOR = CalculateXOR ^ i;
            CalculateOR = CalculateOR | i;
        }
    }
       
    // Print the XOR and OR of all
    // Armstrong number
    document.write("XOR = " + CalculateXOR + "</br>");
    document.write("OR = " + CalculateOR + "</br>");
}
 
// Driver code
let n = 4;
CalculateXORandOR(n);
 
// This code is contributed by divyeshrabadiya07  
 
</script>
Producción: 

XOR =  880
OR =  10098

 

Complejidad del tiempo: O((10 n – 10 n-1 ) * log 10 n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por SHUBHAMSINGH10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *