Find elements in a given range having at least one odd divisor

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print all numbers
// with least one odd factor in the
// given range
#include <bits/stdc++.h>
using namespace std;
  
// Function to prints all numbers
// with at least one odd divisor
int printOddFactorNumber(int n,
                         int m)
{
    for (int i = n; i <= m; i++) {
  
        // Check if the number is
        // not a power of two
        if ((i > 0)
            && ((i & (i - 1)) != 0))
  
            cout << i << " ";
    }
}
  
// Driver Code
int main()
{
    int N = 2, M = 10;
    printOddFactorNumber(N, M);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all numbers
// with least one odd factor in the
// given range
class GFG{
  
// Function to prints all numbers
// with at least one odd divisor
static int printOddFactorNumber(int n,
                                int m)
{
    for (int i = n; i <= m; i++)
    {
  
        // Check if the number is
        // not a power of two
        if ((i > 0) && ((i & (i - 1)) != 0))
  
            System.out.print(i + " ");
    }
    return 0;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 2, M = 10;
    printOddFactorNumber(N, M);
}
}
  
// This code is contributed
// by shivanisinghss2110

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print all numbers 
# with least one odd factor in the 
# given range 
  
# Function to prints all numbers 
# with at least one odd divisor 
def printOddFactorNumber(n, m):
      
    for i in range(n, m + 1):
  
        # Check if the number is
        # not a power of two
        if ((i > 0) and ((i & (i - 1)) != 0)):
            print(i, end = " ")
              
# Driver Code 
N = 2
M = 10
  
printOddFactorNumber(N, M)
  
# This code is contributed by Vishal Maurya

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print all numbers
// with least one odd factor in the
// given range
using System;
class GFG{
  
// Function to prints all numbers
// with at least one odd divisor
static int printOddFactorNumber(int n,
                                int m)
{
    for (int i = n; i <= m; i++)
    {
  
        // Check if the number is
        // not a power of two
        if ((i > 0) && ((i & (i - 1)) != 0))
  
            Console.Write(i + " ");
    }
    return 0;
}
  
// Driver Code
public static void Main()
{
    int N = 2, M = 10;
    printOddFactorNumber(N, M);
}
}
  
// This code is contributed
// by Code_Mech

chevron_right


Output:

3 5 6 7 9 10


Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.