Find elements in a given range having at least one odd divisor
Last Updated :
09 Dec, 2023
Given two integers N and M, the task is to print all elements in the range [N, M] having at least one odd divisor.
Examples:
Input: N = 2, M = 10
Output: 3 5 6 7 9 10
Explanation:
3, 6 have an odd divisor 3
5, 10 have an odd divisor 5
7 have an odd divisor 7
9 have two odd divisors 3, 9
Input: N = 15, M = 20
Output: 15 17 18 19 20
Naive Approach:
The simplest approach is to loop through all numbers in the range [1, N], and for every element, check if it has an odd divisor or not.
Time Complexity: O(N3/2)
Efficient Approach:
To optimize the above approach, we need to observe the following details:
Illustration:
In the range [3, 10], the elements which have at least one odd divisors are {3, 5, 6, 7, 9, 10} and {4, 8} does not contain any odd divisors.
Follow the steps below to solve the problem:
- Traverse the range [N, M] and check for each element if any of its set bit is set in the previous number or not, that is check whether i & (i – 1) is equal to 0 or not.
- If so, then the number is a power of 2 and is not considered. Otherwise, print the number as it is not a power of 2 and has at least one odd divisor.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int printOddFactorNumber( int n,
int m)
{
for ( int i = n; i <= m; i++) {
if ((i > 0)
&& ((i & (i - 1)) != 0))
cout << i << " " ;
}
}
int main()
{
int N = 2, M = 10;
printOddFactorNumber(N, M);
return 0;
}
|
Java
class GFG{
static int printOddFactorNumber( int n,
int m)
{
for ( int i = n; i <= m; i++)
{
if ((i > 0 ) && ((i & (i - 1 )) != 0 ))
System.out.print(i + " " );
}
return 0 ;
}
public static void main(String[] args)
{
int N = 2 , M = 10 ;
printOddFactorNumber(N, M);
}
}
|
Python3
def printOddFactorNumber(n, m):
for i in range (n, m + 1 ):
if ((i > 0 ) and ((i & (i - 1 )) ! = 0 )):
print (i, end = " " )
N = 2
M = 10
printOddFactorNumber(N, M)
|
C#
using System;
class GFG{
static int printOddFactorNumber( int n,
int m)
{
for ( int i = n; i <= m; i++)
{
if ((i > 0) && ((i & (i - 1)) != 0))
Console.Write(i + " " );
}
return 0;
}
public static void Main()
{
int N = 2, M = 10;
printOddFactorNumber(N, M);
}
}
|
Javascript
<script>
function printOddFactorNumber(n, m)
{
for (let i = n; i <= m; i++)
{
if ((i > 0) && ((i & (i - 1)) != 0))
document.write(i + " " );
}
return 0;
}
let N = 2, M = 10;
printOddFactorNumber(N, M);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach#2: Using prime factorization
This approach uses a function prime_factors to find the prime factors of a given number. It then uses another function elements_in_range_with_odd_divisors to iterate over the given range n to m, find the prime factors of each number and check if any of its factors are odd. If any of the factors are odd, the number is appended to the result list. Finally, the result list is converted to a space-separated string using the join() function.
Algorithm
1. Define a function prime_factors(num) that takes a number as input and returns its prime factors as a set.
2. Define a function elements_in_range_with_odd_divisors(n, m) that takes a range n to m as input.
3. Initialize an empty list result to store the numbers that have at least one odd divisor.
4. Iterate over the range n to m using a for loop.
5. For each number in the range, find its prime factors using the prime_factors function.
6. Check if any of the prime factors are odd using the any() function.
7. If any of the prime factors are odd, append the number to the result list.
8. Convert the result list to a space-separated string using the join() function.
9. Return the string.
C++
#include <iostream>
#include <vector>
#include <set>
#include <cmath>
std::set< int > prime_factors( int num) {
std::set< int > factors;
while (num % 2 == 0) {
factors.insert(2);
num /= 2;
}
for ( int i = 3; i <= sqrt (num); i += 2) {
while (num % i == 0) {
factors.insert(i);
num /= i;
}
}
if (num > 2) {
factors.insert(num);
}
return factors;
}
std::string elements_in_range_with_odd_divisors( int n, int m) {
std::vector< int > result;
for ( int i = n; i <= m; ++i) {
std::set< int > factors = prime_factors(i);
bool hasOddFactor = false ;
for ( int factor : factors) {
if (factor % 2 == 1) {
hasOddFactor = true ;
break ;
}
}
if (hasOddFactor) {
result.push_back(i);
}
}
std::string resultStr;
for ( int x : result) {
resultStr += std::to_string(x) + " " ;
}
return resultStr;
}
int main() {
int n = 2;
int m = 10;
std::string result = elements_in_range_with_odd_divisors(n, m);
std::cout << result << std::endl;
return 0;
}
|
Java
import java.util.HashSet;
import java.util.Set;
public class GFG {
public static Set<Integer> primeFactors( int num) {
Set<Integer> factors = new HashSet<>();
while (num % 2 == 0 ) {
factors.add( 2 );
num /= 2 ;
}
for ( int i = 3 ; i <= Math.sqrt(num); i += 2 ) {
while (num % i == 0 ) {
factors.add(i);
num /= i;
}
}
if (num > 2 ) {
factors.add(num);
}
return factors;
}
public static String elementsInRangeWithOddDivisors( int n, int m) {
StringBuilder resultStr = new StringBuilder();
for ( int i = n; i <= m; ++i) {
Set<Integer> factors = primeFactors(i);
boolean hasOddFactor = false ;
for ( int factor : factors) {
if (factor % 2 == 1 ) {
hasOddFactor = true ;
break ;
}
}
if (hasOddFactor) {
resultStr.append(i).append( " " );
}
}
return resultStr.toString().trim();
}
public static void main(String[] args) {
int n = 2 ;
int m = 10 ;
String result = elementsInRangeWithOddDivisors(n, m);
System.out.println(result);
}
}
|
Python3
def prime_factors(num):
factors = []
while num % 2 = = 0 :
factors.append( 2 )
num / / = 2
for i in range ( 3 , int (num * * 0.5 ) + 1 , 2 ):
while num % i = = 0 :
factors.append(i)
num / / = i
if num > 2 :
factors.append(num)
return set (factors)
def elements_in_range_with_odd_divisors(n, m):
result = []
for i in range (n, m + 1 ):
factors = prime_factors(i)
if any (factor % 2 = = 1 for factor in factors):
result.append(i)
return " " .join( str (x) for x in result)
n = 2
m = 10
print (elements_in_range_with_odd_divisors(n,m))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static HashSet< int > PrimeFactors( int num)
{
HashSet< int > factors = new HashSet< int >();
while (num % 2 == 0)
{
factors.Add(2);
num /= 2;
}
for ( int i = 3; i <= Math.Sqrt(num); i += 2)
{
while (num % i == 0)
{
factors.Add(i);
num /= i;
}
}
if (num > 2)
{
factors.Add(num);
}
return factors;
}
static string ElementsInRangeWithOddDivisors( int n, int m)
{
List< int > result = new List< int >();
for ( int i = n; i <= m; ++i)
{
HashSet< int > factors = PrimeFactors(i);
bool hasOddFactor = false ;
foreach ( int factor in factors)
{
if (factor % 2 == 1)
{
hasOddFactor = true ;
break ;
}
}
if (hasOddFactor)
{
result.Add(i);
}
}
return string .Join( " " , result);
}
static void Main()
{
int n = 2;
int m = 10;
string result = ElementsInRangeWithOddDivisors(n, m);
Console.WriteLine(result);
}
}
|
Javascript
function primeFactors(num) {
const factors = new Set();
while (num % 2 === 0) {
factors.add(2);
num /= 2;
}
for (let i = 3; i <= Math.sqrt(num); i += 2) {
while (num % i === 0) {
factors.add(i);
num /= i;
}
}
if (num > 2) {
factors.add(num);
}
return factors;
}
function elementsInRangeWithOddDivisors(n, m) {
const result = [];
for (let i = n; i <= m; ++i) {
const factors = primeFactors(i);
let hasOddFactor = false ;
for (let factor of factors) {
if (factor % 2 === 1) {
hasOddFactor = true ;
break ;
}
}
if (hasOddFactor) {
result.push(i);
}
}
return result.join( " " );
}
function main() {
const n = 2;
const m = 10;
const result = elementsInRangeWithOddDivisors(n, m);
console.log(result);
}
main();
|
The time complexity of the code is O((m-n) * sqrt(m))
Space complexity is O(m).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...