📜  综合编号

📅  最后修改于: 2021-05-04 18:33:35             🧑  作者: Mango

复合数字是一个非整数的正整数。换句话说,它具有一个正除数,也可以不是一个。前几个复合数字为4、6、8、9、10、12、14、15、16、18、20,…………

  • 每个大于1的整数都是质数或复合数。
  • 第一是一个单位–它既不是素数也不是合成数。

如何检查给定数字是否为合成数字?
例子:

Input : n = 21
Output: Yes
The number is a composite number!

Input : n = 11
Output : No

这个想法很简单,我们可以使用以下任何一种用于素数检查的方法。我们只需要更改return语句。返回true更改为返回false,反之亦然。

  • 原始性测试|第一组(介绍和学校方法)
  • 原始性测试|套装2(Fermat方法)
  • 原始性测试|第三组(米勒-拉宾)

在下面的代码中讨论了优化的学校方法。

C++
// A optimized school method based C++ program to check
// if a number is composite.
#include 
using namespace std;
  
bool isComposite(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return false;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return true;
  
    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return true;
  
    return false;
}
  
  
// Driver Program to test above function
int main()
{
    isComposite(11)?  cout << " true\n": cout << " false\n";
    isComposite(15)?  cout << " true\n": cout << " false\n";
    return 0;
}


Java
/// An optimized method based Java
// program to check if a number
// is Composite or not.
import java.io.*;
 
class Composite
{
    static boolean isComposite(int n)
    {
        // Corner cases
        if (n <= 1)
        System.out.println("False");
         
        if (n <= 3)
        System.out.println("False");
 
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0) return true;
 
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
            return true;
 
        return false;
    }
 
    // Driver Program to test above function
    public static void main(String args[])
    {
        System.out.println(isComposite(11) ?
                       "true" : "false");
                        
        System.out.println(isComposite(15) ?
                       "true" : "false");
    }
}
 
// This code is contributed by Anshika Goyal


Python 3
# A optimized school method based Python program to check
# if a number is composite.
 
def isComposite(n):
 
    # Corner cases
    if (n <= 1):
        return False
    if (n <= 3):
        return False
 
    # This is checked so that we can skip
    # middle five numbers in below loop
    if (n % 2 == 0 or n % 3 == 0):
        return True
    i = 5
    while(i * i <= n):
         
        if (n % i == 0 or n % (i + 2) == 0):
            return True
        i = i + 6
         
    return False
 
# Driver Program to test above function
 
print("true") if(isComposite(11)) else print("false")
print("true") if(isComposite(15)) else print("false")
# This code is contributed by Anant Agarwal.


C#
// A optimized school method based C# program
// to check if a number is composite.
using System;
 
namespace Composite
{
public class GFG
{    
                 
    public static bool isComposite(int n)
    {
         
    // Corner cases
    if (n <= 1) return false;
    if (n <= 3) return false;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0) return true;
 
    for (int i = 5; i * i <= n; i = i + 6)
     
        if (n % i == 0 || n % (i + 2) == 0)
        return true;
 
    return false;
    }
 
 
    // Driver Code
    public static void Main()
    {
         
    if(isComposite(11)) Console.WriteLine("true");
    else Console.WriteLine("false");
     
    if(isComposite(15)) Console.WriteLine("true");
    else Console.WriteLine("false");
    }
 
}
}
 
 
// This code is contributed by Sam007


PHP


Javascript


输出:

false
true

合成数字程序

  • 查找给定长度的合成数字范围
  • 生成n个连续复合数字的列表(一种有趣的方法)
  • 数组中k个最小和k个最大复合数的总和与乘积
  • 数组中所有复合数字的乘积
  • 数组中复合元素的计数和总和
  • 将n拆分为最大复合数