Check if a given number N has at least one odd divisor not exceeding N - 1 - GeeksforGeeks

23 Nov, 2021
Given a positive integer N, the task is to check if the given number N has at least 1 odd divisor from the range [2, N – 1] or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 10Output: YesExplanation:10 has 5 as the odd divisor. Therefore, print Yes.
Input: N = 8Output: No
Approach: The idea to solve the given problem is to iterate through all possible odd divisors over the range [3, sqrt(N)] and if there exists any such divisor, then print “Yes”. Otherwise, print “No”. 
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to check whether N// has at least one odd divisor// not exceeding N - 1 or notstring oddDivisor(int N){    // Stores the value of N    int X = N;     // Reduce the given number    // N by dividing it by 2    while (N % 2 == 0) {        N /= 2;    }     for (int i = 3; i * i <= X; i += 2) {         // If N is divisible by        // an odd divisor i        if (N % i == 0) {            return "Yes";        }    }     // Check if N is an odd divisor after    // reducing N by dividing it by 2    if (N != X)        return "Yes";     // Otherwise    return "No";} // Driver Codeint main(){    int N = 10;     // Function Call    cout << oddDivisor(N);     return 0;}
/*package whatever //do not write package name here */import java.io.*; class GFG {     // Function to check whether N    // has at least one odd divisor    // not exceeding N - 1 or not    public static String oddDivisor(int N)    {               // Stores the value of N        int X = N;         // Reduce the given number        // N by dividing it by 2        while (N % 2 == 0) {            N /= 2;        }         for (int i = 3; i * i <= X; i += 2) {             // If N is divisible by            // an odd divisor i            if (N % i == 0) {                return "Yes";            }        }         // Check if N is an odd divisor after        // reducing N by dividing it by 2        if (N != X) {            return "Yes";        }               // Otherwise        return "No";    }     // Driver Code    public static void main(String[] args)    {        int N = 10;               // Function Call        System.out.println(oddDivisor(N));    }} // This code is contributed by aditya7409.
# Python program for the above approach # Function to check whether N# has at least one odd divisor# not exceeding N - 1 or notdef oddDivisor(N):         # Stores the value of N    X = N         # Reduce the given number    # N by dividing it by 2    while (N % 2 == 0):        N //= 2         i = 3    while(i * i <= X):                 # If N is divisible by        # an odd divisor i        if (N % i == 0):            return "Yes"        i += 2         # Check if N is an odd divisor after    # reducing N by dividing it by 2    if (N != X):        return "Yes"         # Otherwise    return "No"     # Driver Code N = 10# Function Callprint(oddDivisor(N)) # This code is contributed by shubhamsingh10
// C# program for the above approachusing System;using System.Collections.Generic; class GFG{   // Function to check whether N  // has at least one odd divisor  // not exceeding N - 1 or not  public static string oddDivisor(int N)  {     // Stores the value of N    int X = N;     // Reduce the given number    // N by dividing it by 2    while (N % 2 == 0) {      N /= 2;    }     for (int i = 3; i * i <= X; i += 2) {       // If N is divisible by      // an odd divisor i      if (N % i == 0) {        return "Yes";      }    }     // Check if N is an odd divisor after    // reducing N by dividing it by 2    if (N != X) {      return "Yes";    }     // Otherwise    return "No";  }   // Driver Code  static public void Main()  {    int N = 10;     // Function Call    Console.Write(oddDivisor(N));  }} // This code is contributed by sanjoy_62.
<script> // javascript program for the above approach // Function to check whether N// has at least one odd divisor// not exceeding N - 1 or notfunction oddDivisor(N){     // Stores the value of N    var X = N;    var i;         // Reduce the given number    // N by dividing it by 2    while (N % 2 == 0) {        N /= 2;    }     for (i = 3; i * i <= X; i += 2) {         // If N is divisible by        // an odd divisor i        if (N % i == 0) {            return "Yes";        }    }     // Check if N is an odd divisor after    // reducing N by dividing it by 2    if (N != X)        return "Yes";     // Otherwise    return "No";} // Driver Code    var N = 10;     // Function Call    document.write(oddDivisor(N)); // This code is contributed by ipg2016107.</script>
Yes
Time Complexity: O(√N)Auxiliary Space: O(1)
Another approach : The only possibility for any number n>1 not to have an odd divisor is for n to be a power of two.
To check power of two, we can use this approach 
n&(n−1) , the result will be zero only if n is a power of two.
C++
Java
#include <iostream>using namespace std; void oddDivisor(int n){  //checking power of two or not  if ((n & (n - 1)) == 0) {        cout << "NO" << endl;    } else {        cout << "YES" << endl;    }} int main() {    int N = 10;     // Function Call    oddDivisor(N);       return 0;}
/*package whatever //do not write package name here */ import java.io.*; class GFG {       public static void main (String[] args) {      int N = 10;     // Function Call    oddDivisor(N);    }     static void oddDivisor(int n){  //checking power of two or not  if ((n & (n - 1)) == 0) {        System.out.println("NO");    } else {        System.out.println("YES");    }}}
YES
aditya7409
SHUBHAMSINGH10
sanjoy_62
ipg2016107
abhi0709singh
divisors
Mathematical
Technical Scripter
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Prime Numbers
Modulo Operator (%) in C/C++ with Examples
Program for Decimal to Binary Conversion
Program to find sum of elements in a given array
Modulo 10^9+7 (1000000007)
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Operators in C / C++
Minimum number of jumps to reach end