📌  相关文章
📜  检查给定数N是否有至少一个奇数除数不超过N – 1

📅  最后修改于: 2021-04-17 13:54:15             🧑  作者: Mango

给定正整数N ,任务是检查给定数字N是否具有至少一个[2,N – 1]范围内的奇数除数。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

方法:解决给定问题的想法是遍历[3,sqrt(N)]范围内所有可能的奇数除数,如果存在任何这样的除数,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check whether N
// has at least one odd divisor
// not exceeding N - 1 or not
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
int main()
{
    int N = 10;
 
    // Function Call
    cout << oddDivisor(N);
 
    return 0;
}


Java
/*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.


Python3
# Python program for the above approach
 
# Function to check whether N
# has at least one odd divisor
# not exceeding N - 1 or not
def 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 Call
print(oddDivisor(N))
 
# This code is contributed by shubhamsingh10


C#
// C# program for the above approach
using 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.


输出:
Yes

时间复杂度: O(√N)
辅助空间: O(1)