Find all the factors of given Number and their bitwise XOR
Given an integer N, the task is to find all the factors (excluding itself) of the number N and their XOR.
Examples:
Input: N = 8
Output:
Divisors are: 1 2 4
Xor: 7
Explanation: 1, 2 and 4 are all factors of 8 and 1^2^4 = 7.
Input: N = 25
Output:
Divisors are: 1 5
Xor: 4
Approach: Follow the below idea to solve the problem:
Store all the factors of the given number N and calculate the xor of all the factors
Follow the steps to solve this problem:
- Initialize a variable Xor = 0
- Create vector factors1 and factors2 for storing factors from 1 to sqrt(N) and from sqrt(N) to N.
- Traverse the array from 1 till sqrt(N)
- If N % i = 0, append i in factors1 and Xor = Xor^i
- And check If N / i != i, append N / i in factors2 and Xor = Xor^(n / i)
- After executing the loop, insert elements of factors2 in factors1 in reverse order
- Pop the element N from the factors1 vector.
- Print the factors1 vector and return Xor^N as we have already calculated N in the Xor.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findAllFactors( int n)
{
int Xor = 0;
vector< int > factors1;
vector< int > factors2;
for ( int i = 1; i * i <= n; i++) {
if (n % i == 0) {
factors1.push_back(i);
Xor ^= i;
if (n / i != i) {
factors2.push_back(n / i);
Xor ^= (n / i);
}
}
}
factors1.insert(factors1.end(), factors2.rbegin(),
factors2.rend());
factors1.pop_back();
cout << "Divisors are: " ;
for ( auto i : factors1) {
cout << i << " " ;
}
cout << endl;
cout << "Xor: " ;
return Xor ^ n;
}
int main()
{
int N = 8;
cout << findAllFactors(N) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static void findAllFactors( int N)
{
long Xor = 0 ;
ArrayList<Integer> factor1
= new ArrayList<Integer>();
ArrayList<Integer> factor2
= new ArrayList<Integer>();
for ( int i = 1 ; i * i <= N; i++) {
if (N % i == 0 ) {
factor1.add(i);
Xor = Xor ^ i;
if (N / i != i) {
int M = N / i;
factor2.add(M);
Xor = Xor ^ (N / i);
}
}
}
for ( int i = factor2.size() - 1 ; i >= 1 ; i--) {
factor1.add(factor2.get(i));
}
System.out.println( "Divisors are : " + factor1);
Xor = Xor ^ N;
System.out.println( "Xor : " + Xor);
}
public static void main(String[] args)
{
int N = 8 ;
findAllFactors(N);
}
}
|
Python3
import math
def findAllFactors(n):
Xor = 0
factors1 = []
factors2 = []
for i in range ( 1 , int (math.sqrt(n)) + 1 ):
if (n % i = = 0 ):
factors1.append(i)
Xor ^ = i
if (n / / i ! = i):
factors2.append(n / / i)
Xor ^ = (n / / i)
factors1 = factors1 + factors2[:: - 1 ]
factors1.pop()
print ( "Divisors are: " , end = "")
for i in factors1:
print (i, end = " " )
print ()
print ( "Xor: " , end = "")
return Xor ^ n
if __name__ = = "__main__" :
N = 8
print (findAllFactors(N))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void findAllFactors( int N)
{
long Xor = 0;
List< int > factor1 = new List< int >();
List< int > factor2
= new List< int >();
for ( int i = 1; i * i <= N; i++)
{
if (N % i == 0)
{
factor1.Add(i);
Xor = Xor ^ i;
if (N / i != i)
{
int M = N / i;
factor2.Add(M);
Xor = Xor ^ (N / i);
}
}
}
for ( int i = factor2.Count - 1; i >= 1; i--)
{
factor1.Add(factor2[i]);
}
Console.Write( "Divisors are : " );
foreach ( int item in factor1){
Console.Write(item + " " );
}
Console.WriteLine();
Xor = Xor ^ N;
Console.WriteLine( "Xor : " + Xor);
}
public static void Main()
{
int N = 8;
findAllFactors(N);
}
}
|
Javascript
function findAllFactors(n)
{
let Xor = 0;
let factors1 = [];
let factors2 = [];
for (let i = 1; i * i <= n; i++) {
if (n % i == 0) {
factors1.push(i);
Xor ^= i;
if (n / i != i) {
factors2.push(n / i);
Xor ^= (n / i);
}
}
}
for (let i = factors2.length - 1; i >= 0; i--) {
factors1.push(factors2[i]);
}
factors1.pop();
document.write( "Divisors are: " + factors1);
return Xor ^ n;
}
let N = 8;
console.log( "Xor : " + findAllFactors(N));
|
Output
Divisors are: 1 2 4
Xor: 7
Time Complexity: O(N1/2), where N is the given integer.
Auxiliary Space: O(N1/2), for storing all the factors of the given integer.
Last Updated :
14 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...