Absolute difference between the count of odd and even factors of N
Last Updated :
20 Jul, 2022
Given a positive integer N, the task is to find the absolute difference of the count of odd and even factors of N.
Examples:
Input: N = 12
Output: 2
Explanation: The even factors of 12 are {2, 4, 6, 12}. Therefore, the count is 4.
The odd factors of 12 are {1, 3}. Therefore, the count is 2.
Hence, the difference between their counts is (4 – 2) = 2.
Input: N = 9
Output: 3
Naive Approach: The simplest approach to solve the given problem is to find all the divisors of the number N and then find the absolute difference of count of odd and even divisors of N.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized which is based on the following observations:
- According to the Unique Factorization Theorem, any number can be expressed in terms of the product of the power of primes. Therefore, N can be expressed as:
N = P1A1 * P2A2 * P3A3 * ……… * PkAK
where, each Pi is a prime and each Ai is a positive integer.(1 ? i ? K)
- Therefore, the total number of factors = (A1 + 1)*(A2 + 1)*(A3 + 1)* ……… *(A4 + 1). Let this count be T.
- The total number of odd factors can be calculated by excluding the power of 2 in the above formula. Let this count be O.
- The total number of even factors is equal to the difference between the total number of factors and the total number of odd factors.
Therefore, the idea is to find the prime factors and their powers in the prime factorization of N by using the Sieve of Eratosthenes and print the value absolute value of the difference of the total number of factors and twice the total number of odd factors as the result i.e., abs(T – 2*O).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sieveOfEratosthenes( int N, int s[])
{
vector< bool > prime(N + 1, false );
for ( int i = 2; i <= N; i += 2)
s[i] = 2;
for ( int i = 3; i <= N; i += 2) {
if (prime[i] == false ) {
s[i] = i;
for ( int j = i; j * i <= N;
j += 2) {
if (!prime[i * j]) {
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
void findDifference( int N)
{
int s[N + 1];
sieveOfEratosthenes(N, s);
int total = 1, odd = 1, even = 0;
int curr = s[N];
int cnt = 1;
while (N > 1) {
N /= s[N];
if (curr == s[N]) {
cnt++;
continue ;
}
if (curr == 2) {
total = total * (cnt + 1);
}
else {
total = total * (cnt + 1);
odd = odd * (cnt + 1);
}
curr = s[N];
cnt = 1;
}
even = total - odd;
cout << abs (even - odd);
}
int main()
{
int N = 12;
findDifference(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void sieveOfEratosthenes( int N, int s[])
{
boolean []prime = new boolean [N + 1 ];
for ( int i = 2 ; i <= N; i += 2 )
s[i] = 2 ;
for ( int i = 3 ; i <= N; i += 2 )
{
if (prime[i] == false )
{
s[i] = i;
for ( int j = i; j * i <= N; j += 2 )
{
if (!prime[i * j])
{
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
static void findDifference( int N)
{
int []s = new int [N + 1 ];
sieveOfEratosthenes(N, s);
int total = 1 , odd = 1 , even = 0 ;
int curr = s[N];
int cnt = 1 ;
while (N > 1 )
{
N /= s[N];
if (curr == s[N])
{
cnt++;
continue ;
}
if (curr == 2 )
{
total = total * (cnt + 1 );
}
else
{
total = total * (cnt + 1 );
odd = odd * (cnt + 1 );
}
curr = s[N];
cnt = 1 ;
}
even = total - odd;
System.out.print(Math.abs(even - odd));
}
public static void main(String[] args)
{
int N = 12 ;
findDifference(N);
}
}
|
Python3
def sieveOfEratosthenes(N, s):
prime = [ False ] * (N + 1 )
for i in range ( 2 , N + 1 , 2 ):
s[i] = 2
for i in range ( 3 , N, 2 ):
if (prime[i] = = False ):
s[i] = i
for j in range (i, N, 2 ):
if j * i > N:
break
if ( not prime[i * j]):
prime[i * j] = True
s[i * j] = i
def findDifference(N):
s = [ 0 ] * (N + 1 )
sieveOfEratosthenes(N, s)
total , odd , even = 1 , 1 , 0
curr = s[N]
cnt = 1
while (N > 1 ):
N / / = s[N]
if (curr = = s[N]):
cnt + = 1
continue
if (curr = = 2 ):
total = total * (cnt + 1 )
else :
total = total * (cnt + 1 )
odd = odd * (cnt + 1 )
curr = s[N]
cnt = 1
even = total - odd
print ( abs (even - odd))
if __name__ = = '__main__' :
N = 12
findDifference(N)
|
C#
using System;
class GFG
{
static void sieveOfEratosthenes( int N, int [] s)
{
bool [] prime = new bool [N + 1];
for ( int i = 2; i <= N; i += 2)
s[i] = 2;
for ( int i = 3; i <= N; i += 2) {
if (prime[i] == false ) {
s[i] = i;
for ( int j = i; j * i <= N; j += 2) {
if (!prime[i * j]) {
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
static void findDifference( int N)
{
int [] s = new int [N + 1];
sieveOfEratosthenes(N, s);
int total = 1, odd = 1, even = 0;
int curr = s[N];
int cnt = 1;
while (N > 1) {
N /= s[N];
if (curr == s[N]) {
cnt++;
continue ;
}
if (curr == 2) {
total = total * (cnt + 1);
}
else {
total = total * (cnt + 1);
odd = odd * (cnt + 1);
}
curr = s[N];
cnt = 1;
}
even = total - odd;
Console.Write(Math.Abs(even - odd));
}
public static void Main()
{
int N = 12;
findDifference(N);
}
}
|
Javascript
<script>
function sieveOfEratosthenes(N, s)
{
let prime = Array.from({length: N+1}, (_, i) => 0);
for (let i = 2; i <= N; i += 2)
s[i] = 2;
for (let i = 3; i <= N; i += 2)
{
if (prime[i] == false )
{
s[i] = i;
for (let j = i; j * i <= N; j += 2)
{
if (!prime[i * j])
{
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
function findDifference(N)
{
let s = Array.from({length: N+1}, (_, i) => 0);
sieveOfEratosthenes(N, s);
let total = 1, odd = 1, even = 0;
let curr = s[N];
let cnt = 1;
while (N > 1)
{
N /= s[N];
if (curr == s[N])
{
cnt++;
continue ;
}
if (curr == 2)
{
total = total * (cnt + 1);
}
else
{
total = total * (cnt + 1);
odd = odd * (cnt + 1);
}
curr = s[N];
cnt = 1;
}
even = total - odd;
document.write(Math.abs(even - odd));
}
let N = 12;
findDifference(N);
</script>
|
Time Complexity: O(N*(log log N))
Auxiliary Space: O(N), since n extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...