Check if a given number N has at least one odd divisor not exceeding N – 1
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 = 10
Output: Yes
Explanation:
10 has 5 as the odd divisor. Therefore, print Yes.
Input: N = 8
Output: 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++
#include <bits/stdc++.h>
using namespace std;
string oddDivisor( int N)
{
int X = N;
while (N % 2 == 0) {
N /= 2;
}
for ( int i = 3; i * i <= X; i += 2) {
if (N % i == 0) {
return "Yes" ;
}
}
if (N != X)
return "Yes" ;
return "No" ;
}
int main()
{
int N = 10;
cout << oddDivisor(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static String oddDivisor( int N)
{
int X = N;
while (N % 2 == 0 ) {
N /= 2 ;
}
for ( int i = 3 ; i * i <= X; i += 2 ) {
if (N % i == 0 ) {
return "Yes" ;
}
}
if (N != X) {
return "Yes" ;
}
return "No" ;
}
public static void main(String[] args)
{
int N = 10 ;
System.out.println(oddDivisor(N));
}
}
|
Python3
def oddDivisor(N):
X = N
while (N % 2 = = 0 ):
N / / = 2
i = 3
while (i * i < = X):
if (N % i = = 0 ):
return "Yes"
i + = 2
if (N ! = X):
return "Yes"
return "No"
N = 10
print (oddDivisor(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static string oddDivisor( int N)
{
int X = N;
while (N % 2 == 0) {
N /= 2;
}
for ( int i = 3; i * i <= X; i += 2) {
if (N % i == 0) {
return "Yes" ;
}
}
if (N != X) {
return "Yes" ;
}
return "No" ;
}
static public void Main()
{
int N = 10;
Console.Write(oddDivisor(N));
}
}
|
Javascript
<script>
function oddDivisor(N)
{
var X = N;
var i;
while (N % 2 == 0) {
N /= 2;
}
for (i = 3; i * i <= X; i += 2) {
if (N % i == 0) {
return "Yes" ;
}
}
if (N != X)
return "Yes" ;
return "No" ;
}
var N = 10;
document.write(oddDivisor(N));
</script>
|
Time Complexity: O(√N)
Auxiliary Space: O(1)
Last Updated :
22 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...