📜  查找除以N但严格小于N的最大复合数

📅  最后修改于: 2021-06-26 22:32:45             🧑  作者: Mango

给定一个合成数N ,任务是找到除以N并严格小于N的最大合成数。如果没有这样的数字,则打印-1。
例子:

方法:
由于N是一个复合数字,因此N可以是两个数字的乘积,使得一个是质数,另一个是一个复合数字,如果我们找不到N的对,那么最大的复合数字会小于N N的除法不存在。
要找到最大的合成数,请找到除以N的最小素数(例如a)。然后,除以N且小于N的最大复合数可以由(N / a)给出
步骤如下:

  1. 找出N的最小素数(例如a)。
  2. 检查(N / a)是否为质数。如果是,那么我们找不到最大的复合数。
  3. 其他(N / a)是除N且小于N的最大复合数。

下面是上述方法的实现:

C++
// C++ program to find the largest
// composite number that divides
// N which is less than N
#include 
using namespace std;
 
// Function to check whether
// a number is prime or not
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;
 
    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// Function that find the largest
// composite number which divides
// N and less than N
int getSmallestPrimefactor(int n)
{
    // Find the prime number
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0)
            return i;
    }
}
 
// Driver's Code
int main()
{
    int N = 100;
    int a;
 
    // Get the smallest prime
    // factor
    a = getSmallestPrimefactor(N);
 
    // Check if (N/a) is prime
    // or not
    // If Yes print "-1"
    if (isPrime(N / a)) {
        cout << "-1";
    }
 
    // Else print largest composite
    // number (N/a)
    else {
        cout << N / a;
    }
    return 0;
}


Java
// Java program to find the largest
// composite number that divides
// N which is less than N
import java.util.*;
 
class GFG{
 
// Function to check whether
// a number is prime or not
static boolean isPrime(int n)
{
     
    // Corner case
    if (n <= 1)
        return false;
 
    // Check from 2 to n-1
    for(int i = 2; i < n; i++)
    {
       if (n % i == 0)
           return false;
    }
    return true;
}
 
// Function that find the largest
// composite number which divides
// N and less than N
static int getSmallestPrimefactor(int n)
{
     
    // Find the prime number
    for(int i = 2; i <= Math.sqrt(n); i++)
    {
       if (n % i == 0)
           return i;
    }
    return -1;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 100;
    int a;
 
    // Get the smallest prime
    // factor
    a = getSmallestPrimefactor(N);
 
    // Check if (N/a) is prime or
    // not. If Yes print "-1"
    if (isPrime(N / a))
    {
        System.out.print("-1");
    }
 
    // Else print largest composite
    // number (N/a)
    else
    {
        System.out.print(N / a);
    }
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program to find the largest
# composite number that divides
# N which is less than N
import math
 
# Function to check whether
# a number is prime or not
def isPrime(n):
 
    # Corner case
    if (n <= 1):
        return False;
 
    # Check from 2 to n-1
    for i in range(2, n):
        if (n % i == 0):
            return False;
 
    return True;
 
# Function that find the largest
# composite number which divides
# N and less than N
def getSmallestPrimefactor(n):
     
    # Find the prime number
    for i in range(2, (int)(math.sqrt(n) + 1)):
        if (n % i == 0):
            return i;
 
    return -1
 
# Driver Code
N = 100;
 
# Get the smallest prime
# factor
a = getSmallestPrimefactor(N);
 
# Check if (N/a) is prime
# or not. If Yes print "-1"
if ((isPrime((int)(N / a)))):
    print(-1)
     
# Else print largest composite
# number (N/a)
else:
    print((int)(N / a));
 
# This code is contributed by grand_master


C#
// C# program to find the largest
// composite number that divides
// N which is less than N
using System;
class GFG{
 
// Function to check whether
// a number is prime or not
static bool isPrime(int n)
{
     
    // Corner case
    if (n <= 1)
        return false;
 
    // Check from 2 to n-1
    for(int i = 2; i < n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}
 
// Function that find the largest
// composite number which divides
// N and less than N
static int getSmallestPrimefactor(int n)
{
     
    // Find the prime number
    for(int i = 2; i <= Math.Sqrt(n); i++)
    {
        if (n % i == 0)
            return i;
    }
    return -1;
}
 
// Driver Code
public static void Main()
{
    int N = 100;
    int a;
 
    // Get the smallest prime
    // factor
    a = getSmallestPrimefactor(N);
 
    // Check if (N/a) is prime or
    // not. If Yes print "-1"
    if (isPrime(N / a))
    {
        Console.Write("-1");
    }
 
    // Else print largest composite
    // number (N/a)
    else
    {
        Console.Write(N / a);
    }
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
50

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。