📜  偶数的最大奇数因子

📅  最后修改于: 2021-04-29 13:32:01             🧑  作者: Mango

给定偶数N ,任务是找到N的最大可能奇数因子。
例子:

天真的方法:天真的方法是找到N的所有因子,然后从中选择最大的奇数因子。
时间复杂度: O(sqrt(N))

高效方法:此问题的有效方法是观察每个偶数N可以表示为:

N = 2i*odd_number

因此,要获得最大的奇数,我们需要将给定数N除以2,直到N变为奇数为止。

下面是上述方法的实现:

C++
// C++ program for the above approach 
#include 
using namespace std; 
  
// Function to print greatest odd factor 
int greatestOddFactor(int n) 
{ 
    int pow_2 = (int)(log(n)); 
      
    // Initialize i with 1 
    int i = 1; 
      
    // Iterate till i <= pow_2 
    while (i <= pow_2) 
    { 
          
        // Find the pow(2, i) 
        int fac_2 = (2 * i); 
        if (n % fac_2 == 0) 
        { 
            // If factor is odd, then 
            // print the number and break 
            if ((n / fac_2) % 2 == 1) 
            { 
                return (n / fac_2); 
            } 
        } 
  
        i += 1; 
    } 
} 
  
// Driver Code 
int main() 
{ 
      
    // Given Number 
    int N = 8642; 
      
    // Function Call 
    cout << greatestOddFactor(N); 
    return 0; 
} 
  
// This code is contributed by Amit Katiyar


Java
// Java program for the above approach
class GFG{
      
// Function to print greatest odd factor 
public static int greatestOddFactor(int n) 
{ 
    int pow_2 = (int)(Math.log(n)); 
      
    // Initialize i with 1 
    int i = 1; 
      
    // Iterate till i <= pow_2 
    while (i <= pow_2) 
    { 
          
        // Find the pow(2, i) 
        int fac_2 = (2 * i); 
        if (n % fac_2 == 0) 
        { 
              
            // If factor is odd, then 
            // print the number and break 
            if ((n / fac_2) % 2 == 1) 
            { 
                return (n / fac_2); 
            } 
        } 
        i += 1; 
    } 
    return 0;
} 
  
// Driver code
public static void main(String[] args)
{
      
    // Given Number 
    int N = 8642; 
      
    // Function Call 
    System.out.println(greatestOddFactor(N)); 
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
  
# importing Maths library
import math 
  
# Function to print greatest odd factor
def greatestOddFactor(n):
    
  pow_2 = int(math.log(n, 2))
    
# Initialize i with 1
  i = 1
  
# Iterate till i <= pow_2
  while i <= pow_2:
  
# find the pow(2, i)
    fac_2 = (2**i)
  
    if (n % fac_2 == 0) :
  
      # If factor is odd, then print the
      # number and break
      if ( (n // fac_2) % 2 == 1):
        print(n // fac_2)
        break
  
    i += 1
  
# Driver Code
  
# Given Number
N = 8642
  
# Function Call
greatestOddFactor(N)


C#
// C# program for the above approach
using System;
  
class GFG{
      
// Function to print greatest odd factor 
public static int greatestOddFactor(int n) 
{ 
    int pow_2 = (int)(Math.Log(n)); 
      
    // Initialize i with 1 
    int i = 1; 
      
    // Iterate till i <= pow_2 
    while (i <= pow_2) 
    { 
          
        // Find the pow(2, i) 
        int fac_2 = (2 * i); 
        if (n % fac_2 == 0) 
        { 
              
            // If factor is odd, then 
            // print the number and break 
            if ((n / fac_2) % 2 == 1) 
            { 
                return (n / fac_2); 
            } 
        } 
        i += 1; 
    } 
    return 0;
} 
  
// Driver code
public static void Main(String[] args)
{
      
    // Given number 
    int N = 8642; 
      
    // Function call 
    Console.WriteLine(greatestOddFactor(N)); 
}
}
  
// This code is contributed by gauravrajput1


输出:
4321

时间复杂度: O(log2(N))