Given an integer N, the task is to find the XOR and OR values of all N-digit Armstrong numbers.
Examples
Input: N = 3
Output: XOR = 271, OR = 511
153, 370, 371, 407 are the three digit armstrong numbers
Input: N = 4
Output: XOR = 880, OR = 10098
Approach:
- Find the starting and ending number of N-digit Armstrong number by:
Starting N-digit Armstrong number = pow(10, n - 1)
Ending N-digit Armstrong number = pow(10, n) - 1
- Iterate over the N-digit Armstrong numbers from starting number till ending number and check whether that number is Armstrong or not.
- If the number is Armstrong, then take XOR and OR of that number separately.
- Else proceed for next iteration and print the value of XOR and OR after all iterations.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
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;
}
void CalculateXORandOR( int n)
{
int CalculateXOR = 0;
int CalculateOR = 0;
int start = ( int ) pow (10, n - 1);
int end = ( int ) pow (10, n) - 1;
for ( int i = start; i < end + 1; i++)
{
if (isArmstrong(i, n)) {
CalculateXOR = CalculateXOR ^ i;
CalculateOR = CalculateOR | i;
}
}
cout << "XOR = " << CalculateXOR << endl;
cout << "OR = " << CalculateOR << endl;
}
int main()
{
int n = 4;
CalculateXORandOR(n);
}
|
Java
import java.io.*;
class GFG
{
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;
}
static void CalculateXORandOR( int n) {
int CalculateXOR = 0 ;
int CalculateOR = 0 ;
int start = ( int ) Math.pow( 10 , n - 1 );
int end = ( int ) (Math.pow( 10 , n)) - 1 ;
for ( int i = start; i < end + 1 ; i++) {
if (isArmstrong(i, n)) {
CalculateXOR = CalculateXOR ^ i;
CalculateOR = CalculateOR | i;
}
}
System.out.println( "XOR = " + CalculateXOR);
System.out.println( "OR = " + CalculateOR);
}
public static void main(String[] args) {
int n = 4 ;
CalculateXORandOR(n);
}
}
|
Python3
def isArmstrong (x, n):
sum1 = 0
temp = x
while temp > 0 :
digit = temp % 10
sum1 + = digit * * n
temp / / = 10
return sum1 = = x
def CalculateXORandOR(n) :
CalculateXOR = 0
CalculateOR = 0
start = 10 * * (n - 1 )
end = ( 10 * * n) - 1
for i in range ( start, end + 1 ) :
if (isArmstrong(i, n)) :
CalculateXOR = CalculateXOR ^ i
CalculateOR = CalculateOR | i
print ( "XOR = " , CalculateXOR)
print ( "OR = " , CalculateOR)
if __name__ = = "__main__" :
n = 4 ;
CalculateXORandOR(n);
|
C#
using System;
class GFG
{
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;
}
static void CalculateXORandOR( int n) {
int CalculateXOR = 0;
int CalculateOR = 0;
int start = ( int ) Math.Pow(10, n - 1);
int end = ( int ) (Math.Pow(10, n)) - 1;
for ( int i = start; i < end + 1; i++) {
if (isArmstrong(i, n)) {
CalculateXOR = CalculateXOR ^ i;
CalculateOR = CalculateOR | i;
}
}
Console.WriteLine( "XOR = " + CalculateXOR);
Console.WriteLine( "OR = " + CalculateOR);
}
public static void Main(String[] args) {
int n = 4;
CalculateXORandOR(n);
}
}
|
Javascript
<script>
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 CalculateXORandOR(n)
{
let CalculateXOR = 0;
let CalculateOR = 0;
let start = Math.pow(10, n - 1);
let end = (Math.pow(10, n)) - 1;
for (let i = start; i < end + 1; i++)
{
if (isArmstrong(i, n))
{
CalculateXOR = CalculateXOR ^ i;
CalculateOR = CalculateOR | i;
}
}
document.write( "XOR = " + CalculateXOR + "</br>" );
document.write( "OR = " + CalculateOR + "</br>" );
}
let n = 4;
CalculateXORandOR(n);
</script>
|
Output:
XOR = 880
OR = 10098
Time Complexity: O((10n – 10n-1) * log10n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Dec, 2022
Like Article
Save Article