Check if Sum of total number of factors from 1 to N is even or odd
Last Updated :
07 Nov, 2023
Given an integer N. The task is to find check if the Sum of total number of factors from 1 to N is even or odd i.e., sum % 2 = 0 or 1.
Examples:
Input: N = 3
Output: Odd
Explanation: 1 has 1 factor, 2 has 2 factors (1, 2) and 3 has 2 factors (1, 3),
Hence, total number of factors is 5 which is odd.
Input: N = 4
Output: Even
Explanation: 1 has 1 factor, 2 has 2 factors (1, 2), 3 has 2 factors (1, 3) and 4 has 3 factors (1, 2, 4),
Hence, a total of 8 factors which is even.
Naive approach: The basic idea to solve the problem is to first Find all the factors of the numbers from 1 to N and then store their sum and check if the sum is even or odd.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int factor( int n)
{
int count = 0;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j < sqrt (n) + 1;
j++) {
if (i % j == 0) {
if (i / j == j) {
count += 1;
}
else {
count += 2;
}
count %= 2;
}
}
}
return count;
}
int main()
{
int N = 4;
if (factor(N))
cout << "Odd" ;
else
cout << "Even" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int factor( int n)
{
int count = 0 ;
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j < Math.sqrt(n) + 1 ; j++) {
if (i % j == 0 ) {
if (i / j == j) {
count += 1 ;
}
else {
count += 2 ;
}
count %= 2 ;
}
}
}
return count;
}
public static void main (String[] args) {
int N = 4 ;
if (factor(N) != 0 )
System.out.print( "Odd" );
else
System.out.print( "Even" );
}
}
|
Python3
import math
def factor(n):
count = 0
for i in range ( 1 , n + 1 ):
for j in range ( 1 ,math.floor(math.sqrt(n) + 1 )):
if (i % j = = 0 ):
if (i / j = = j):
count + = 1
else :
count + = 2
count % = 2
return count
N = 4
if (factor(N)):
print ( "Odd" )
else :
print ( "Even" )
|
C#
using System;
class GFG {
static int factor( int n)
{
int count = 0;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j < Math.Sqrt(n) + 1; j++) {
if (i % j == 0) {
if (i / j == j) {
count += 1;
}
else {
count += 2;
}
count %= 2;
}
}
}
return count;
}
public static void Main()
{
int N = 4;
if (factor(N) != 0)
Console.Write( "Odd" );
else
Console.Write( "Even" );
}
}
|
Javascript
<script>
function factor(n)
{
let count = 0;
for (let i = 1; i <= n; i++) {
for (let j = 1; j < Math.sqrt(n) + 1; j++) {
if (i % j == 0) {
if (i / j == j) {
count += 1;
}
else {
count += 2;
}
count %= 2;
}
}
}
return count;
}
let N = 4;
if (factor(N))
document.write( "Odd" );
else
document.write( "Even" );
</script>
|
Time Complexity: O(N*sqrt(N))
Auxiliary Space: O(1)
Better Approach: The idea to solve the problem is the same as of the naive apporach. But finding factors is done in more efficient way here. We will be counting factors in O(n1/3) time complexity using the apporach mentioned and discussed in this article: Count Divisors of n in O(n^1/3).
Algorithm:
- Define a function SieveOfEratosthenes() that takes an integer n, a boolean array prime[], a boolean array primesquare[] and an integer array a[] as input.
- Initialize all entries in prime[] to true.
- Initialize all entries in primesquare[] to false.
- Set prime[1] to false.
- Using Sieve of Eratosthenes algorithm, set prime[i] to false if i is not a prime.
- Using Sieve of Eratosthenes algorithm, set primesquare[i] to true if i is a square of a prime.
- Store all prime numbers less than or equal to n in the integer array a[].
- Define a function factor() that takes an integer n as input.
- If n is 1, return 1.
- Call the SieveOfEratosthenes() function with parameters n, prime[], primesquare[], and a[].
- Initialize ans to 1.
- Loop through all prime numbers in a[].
- If the cube of a[i] is greater than n, break from the loop.
- Calculate the power of a[i] in n.
- Update ans by multiplying it with cnt, which is the power of a[i] in n plus 1.
- If n is still greater than 1, it has a prime factor greater than the cube root of n.
- If n is prime, update ans by multiplying it with 2.
- If n is a square of a prime, update ans by multiplying it with 3.
- If n is neither prime nor a square of a prime, update ans by multiplying it with 4.
- Return ans..
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes( int n, bool prime[],
bool primesquare[], int a[]) {
for ( int i = 2; i <= n; i++)
prime[i] = true ;
for ( int i = 0; i <= (n * n + 1); i++)
primesquare[i] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= n; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
int j = 0;
for ( int p = 2; p <= n; p++) {
if (prime[p]) {
a[j] = p;
primesquare[p * p] = true ;
j++;
}
}
}
int factor( int n) {
if (n == 1)
return 1;
bool prime[n + 1], primesquare[n * n + 1];
int a[n];
SieveOfEratosthenes(n, prime, primesquare, a);
int ans = 1;
for ( int i = 0;; i++) {
if (a[i] * a[i] * a[i] > n)
break ;
int cnt = 1;
while (n % a[i] == 0)
{
n = n / a[i];
cnt = cnt + 1;
}
ans = ans * cnt;
}
if (prime[n])
ans = ans * 2;
else if (primesquare[n])
ans = ans * 3;
else if (n != 1)
ans = ans * 4;
return ans;
}
int main() {
int N = 4;
long long int sum = 0;
for ( int i = 1; i <= N; i++){
sum += factor(i);
}
if (sum % 2 != 0)
cout << "Odd" ;
else
cout << "Even" ;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static void SieveOfEratosthenes( int n, boolean [] prime, boolean [] primesquare, int [] a) {
Arrays.fill(prime, true );
Arrays.fill(primesquare, false );
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= n; p++) {
if (prime[p]) {
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
int j = 0 ;
for ( int p = 2 ; p <= n; p++) {
if (prime[p]) {
a[j] = p;
primesquare[p * p] = true ;
j++;
}
}
}
static int factor( int n) {
if (n == 1 )
return 1 ;
boolean [] prime = new boolean [n + 1 ];
boolean [] primesquare = new boolean [n * n + 1 ];
int [] a = new int [n];
SieveOfEratosthenes(n, prime, primesquare, a);
int ans = 1 ;
for ( int i = 0 ;; i++) {
if (a[i] * a[i] * a[i] > n)
break ;
int cnt = 1 ;
while (n % a[i] == 0 ) {
n = n / a[i];
cnt = cnt + 1 ;
}
ans = ans * cnt;
}
if (prime[n])
ans = ans * 2 ;
else if (primesquare[n])
ans = ans * 3 ;
else if (n != 1 )
ans = ans * 4 ;
return ans;
}
public static void main(String[] args) {
int N = 4 ;
long sum = 0 ;
for ( int i = 1 ; i <= N; i++) {
sum += factor(i);
}
if (sum % 2 != 0 )
System.out.println( "Odd" );
else
System.out.println( "Even" );
}
}
|
Python3
def sieve_of_eratosthenes(n):
prime = [ True ] * (n + 1 )
prime[ 0 ] = prime[ 1 ] = False
for p in range ( 2 , int (n * * 0.5 ) + 1 ):
if prime[p]:
for i in range (p * p, n + 1 , p):
prime[i] = False
return [p for p in range ( 2 , n + 1 ) if prime[p]]
def factor(n):
if n = = 1 :
return 1
primes = sieve_of_eratosthenes(n)
ans = 1
for p in primes:
cnt = 0
while n % p = = 0 :
n / / = p
cnt + = 1
ans * = cnt + 1
if n > 1 :
ans * = 2
return ans
def main():
N = 4
total_factors_sum = sum (factor(i) for i in range ( 1 , N + 1 ))
if total_factors_sum % 2 ! = 0 :
print ( "Odd" )
else :
print ( "Even" )
if __name__ = = "__main__" :
main()
|
C#
using System;
class MainClass
{
static void SieveOfEratosthenes( int n, bool [] prime, bool [] primesquare, int [] a)
{
Array.Fill(prime, true );
Array.Fill(primesquare, false );
prime[1] = false ;
for ( int p = 2; p * p <= n; p++)
{
if (prime[p])
{
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
int j = 0;
for ( int p = 2; p <= n; p++)
{
if (prime[p])
{
a[j] = p;
primesquare[p * p] = true ;
j++;
}
}
}
static int Factor( int n)
{
if (n == 1)
return 1;
bool [] prime = new bool [n + 1];
bool [] primesquare = new bool [n * n + 1];
int [] a = new int [n];
SieveOfEratosthenes(n, prime, primesquare, a);
int ans = 1;
for ( int i = 0; ; i++)
{
if (a[i] * a[i] * a[i] > n)
break ;
int cnt = 1;
while (n % a[i] == 0)
{
n = n / a[i];
cnt = cnt + 1;
}
ans = ans * cnt;
}
if (prime[n])
ans = ans * 2;
else if (primesquare[n])
ans = ans * 3;
else if (n != 1)
ans = ans * 4;
return ans;
}
public static void Main( string [] args)
{
int N = 4;
long sum = 0;
for ( int i = 1; i <= N; i++)
{
sum += Factor(i);
}
if (sum % 2 != 0)
Console.WriteLine( "Odd" );
else
Console.WriteLine( "Even" );
}
}
|
Javascript
function factor(n) {
if (n === 1) {
return 1;
}
const prime = new Array(n + 1).fill( true );
const primesquare = new Array(n * n + 1).fill( false );
const a = [];
function SieveOfEratosthenes() {
prime[0] = prime[1] = false ;
for (let p = 2; p * p <= n; p++) {
if (prime[p]) {
for (let i = p * p; i <= n; i += p) {
prime[i] = false ;
}
}
}
let j = 0;
for (let p = 2; p <= n; p++) {
if (prime[p]) {
a[j] = p;
primesquare[p * p] = true ;
j++;
}
}
}
SieveOfEratosthenes();
let ans = 1;
for (let i = 0;; i++) {
if (a[i] * a[i] * a[i] > n) {
break ;
}
let cnt = 1;
while (n % a[i] === 0) {
n = n / a[i];
cnt = cnt + 1;
}
ans = ans * cnt;
}
if (prime[n]) {
ans = ans * 2;
} else if (primesquare[n]) {
ans = ans * 3;
} else if (n !== 1) {
ans = ans * 4;
}
return ans;
}
function main() {
const N = 4;
let sum = 0;
for (let i = 1; i <= N; i++) {
sum += factor(i);
}
if (sum % 2 !== 0) {
console.log( "Odd" );
} else {
console.log( "Even" );
}
}
main();
|
Time Complexity: O( n * n1/3 ) where n is the size of the input integer.
Space Complexity: O(n) as boolean arrays prime and primesquare has been created. Here, n is the size of the input integer.
Efficient Approach: The idea to solve the problem efficiently is based on the following observation.
As, all numbers except perfect squares, have an even number of factors. So, the concern is only perfect squares.
Find the number of perfect squares that lie between 1 to N.
If the number of perfect squares is odd then total number of factors is odd, otherwise, even.
Follow the steps to solve the problem:
- Declare any count variable to store the factors of a number and initialize it with 0.
- Update count with the total number of perfect squares from 1 to N which is the same as the sqrt(N).
- Check if count is even or odd and return 0 or 1 accordingly.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int factor( int N)
{
int count = 0;
count = ( int ) sqrt (N);
count = count % 2;
return count;
}
int main()
{
int N = 4;
if (factor(N))
cout << "Odd" ;
else
cout << "Even" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int factor( int N)
{
int count = 0 ;
count = ( int )Math.sqrt(N);
count = count % 2 ;
return count;
}
public static void main(String[] args)
{
int N = 4 ;
if (factor(N) != 0 )
System.out.print( "Odd" );
else
System.out.print( "Even" );
}
}
|
Python3
import math
def factor(N):
count = 0
count = math.floor(math.sqrt(N))
count = count % 2
return count
N = 4
if (factor(N)):
print ( "Odd" )
else :
print ( "Even" )
|
C#
using System;
class GFG {
static int factor( int N)
{
int count = 0;
count = ( int )Math.Sqrt(N);
count = count % 2;
return count;
}
public static void Main()
{
int N = 4;
if (factor(N) != 0)
Console.Write( "Odd" );
else
Console.Write( "Even" );
}
}
|
Javascript
<script>
function factor(N)
{
let count = 0
count = Math.floor(Math.sqrt(N))
count = count % 2
return count
}
let N = 4
if (factor(N))
document.write( "Odd" , "</br>" )
else
document.write( "Even" , "</br>" )
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...