📜  查找给定范围内至少有 K 个连续复合整数的组

📅  最后修改于: 2022-05-13 01:56:06.744000             🧑  作者: Mango

查找给定范围内至少有 K 个连续复合整数的组

给定一个范围[L, R]和一个整数K ,找出给定范围内至少有 K 个连续复合整数的所有组。

例子:

蛮力方法: A 简单的解决方案是遍历给定范围内的所有数字,并且:

  • 检查每个数字是否为质数。
  • 记录连续的合数。
  • 如果值超过K ,则打印组范围及其计数。

时间复杂度: O((RL)*√R)
辅助空间: O(1)

高效方法:我们可以使用替代假设来查找给定范围内的素数并有效地解决问题,而不是查找所有连续的合数。

按照下面提到的步骤来实施上述方法:

  • 生成范围之间的素数。
  • 生成素数数组后,遍历数组。
  • 检查是否连续 合数超过 10。

下面是上述方法的实现:

C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function to find the range
void Composite_range(int l, int r, int K)
{
    // Code of sieve of Eratosthenes
    int n = r;
    bool prime[n + 1] = { false };
    for (int i = 0; i <= n; i++)
        prime[i] = true;
 
    for (int p = 2; p * p <= n; p++) {
 
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true) {
 
            // Update all multiples of p
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
 
    int count = 0;
 
    for (int i = l; i <= r; i++) {
 
        // If the number is a prime then
        // check whether the previous consecutive
        // composite number count is
        // greater than K
        if (prime[i] == true) {
            if (count >= K)
                cout << (i - count) << " "
                     << i - 1 << " "
                     << count << "\n";
 
            count = 0;
        }
 
        else
            count++;
    }
 
    if (count >= 10)
        cout << (r - count) << " "
             << r << " " << count
             << "\n";
}
 
// Driver Code
int main()
{
    int L = 500;
    int R = 650;
    int K = 10;
 
    // Function call
    Composite_range(L, R, K);
    return 0;
}


Java
// Java code to implement the approach
import java.util.*;
public class GFG {
 
  // Function to find the range
  static void Composite_range(int l, int r, int K)
  {
     
    // Code of sieve of Eratosthenes
    int n = r;
    boolean[] prime = new boolean[n + 1];
    for (int i = 0; i <= n; i++)
      prime[i] = true;
 
    for (int p = 2; p * p <= n; p++) {
 
      // If prime[p] is not changed,
      // then it is a prime
      if (prime[p] == true) {
 
        // Update all multiples of p
        for (int i = p * p; i <= n; i += p)
          prime[i] = false;
      }
    }
 
    int count = 0;
 
    for (int i = l; i <= r; i++) {
 
      // If the number is a prime then
      // check whether the previous consecutive
      // composite number count is
      // greater than K
      if (prime[i] == true) {
        if (count >= K)
          System.out.println((i - count) + " "
                            + (i - 1) + " "
                            + count);
 
        count = 0;
      }
 
      else
        count++;
    }
 
    if (count >= 10)
      System.out.println((r - count) + " " + r + " "
                        + count);
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int L = 500;
    int R = 650;
    int K = 10;
 
    // Function call
    Composite_range(L, R, K);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# python3 code to implement the approach
from cmath import sqrt
import math
 
# Function to find the range
def Composite_range(l, r, K):
 
    # Code of sieve of Eratosthenes
    n = r
    prime = [False for _ in range(n + 1)]
    for i in range(0, n+1):
        prime[i] = True
 
    for p in range(2, int(math.sqrt(n)) + 1):
 
        # If prime[p] is not changed,
        # then it is a prime
        if (prime[p] == True):
 
            # Update all multiples of p
            for i in range(p*p, n+1, p):
                prime[i] = False
 
    count = 0
 
    for i in range(l, r + 1):
 
        # If the number is a prime then
        # check whether the previous consecutive
        # composite number count is
        # greater than K
        if (prime[i] == True):
            if (count >= K):
                print(f"{(i - count)} {i - 1} {count}")
 
            count = 0
 
        else:
            count += 1
 
    if (count >= 10):
        print(f"{(r - count)} {r} {count}")
 
# Driver Code
if __name__ == "__main__":
 
    L = 500
    R = 650
    K = 10
 
    # Function call
    Composite_range(L, R, K)
 
# This code is contributed by rakeshsahni


C#
// C# code to implement the approach
using System;
class GFG {
 
  // Function to find the range
  static void Composite_range(int l, int r, int K)
  {
     
    // Code of sieve of Eratosthenes
    int n = r;
    bool[] prime = new bool[n + 1];
    for (int i = 0; i <= n; i++)
      prime[i] = true;
 
    for (int p = 2; p * p <= n; p++) {
 
      // If prime[p] is not changed,
      // then it is a prime
      if (prime[p] == true) {
 
        // Update all multiples of p
        for (int i = p * p; i <= n; i += p)
          prime[i] = false;
      }
    }
 
    int count = 0;
 
    for (int i = l; i <= r; i++) {
 
      // If the number is a prime then
      // check whether the previous consecutive
      // composite number count is
      // greater than K
      if (prime[i] == true) {
        if (count >= K)
          Console.WriteLine((i - count) + " "
                            + (i - 1) + " "
                            + count);
 
        count = 0;
      }
 
      else
        count++;
    }
 
    if (count >= 10)
      Console.WriteLine((r - count) + " " + r + " "
                        + count);
  }
 
  // Driver Code
  public static void Main()
  {
    int L = 500;
    int R = 650;
    int K = 10;
 
    // Function call
    Composite_range(L, R, K);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
510 520 11
524 540 17
620 630 11

时间复杂度 O(R*log(log(R)))
辅助空间 O(R)