📜  程序查找给定间隔之间的质数

📅  最后修改于: 2021-05-28 02:13:20             🧑  作者: Mango

给定两个数字a和b作为间隔范围,任务是找到此间隔之间的质数。

例子:

Input : a = 1, b = 10
Output : 2, 3, 5, 7

Input : a = 10, b = 20
Output : 11, 13, 17, 19

在下面的程序中,将数字范围作为输入并存储在变量“ a”和“ b”中。然后使用for循环遍历a和b的间隔之间的数字。对于for循环中的每个数字,将检查该数字是否为质数。如果发现是素数,请打印数字。然后检查循环中的下一个数字,直到检查完所有数字。

程序:

C++
// C++ program to find the prime numbers
// between a given interval
 
#include 
using namespace std;
 
int main()
{
    // Declare the variables
    int a, b, i, j, flag;
 
    // Ask user to enter lower value of interval
    cout << "Enter lower bound of the interval: ";
    cin >> a; // Take input
 
    // Ask user to enter upper value of interval
    cout << "\nEnter upper bound of the interval: ";
    cin >> b; // Take input
 
    // Print display message
    cout << "\nPrime numbers between "
         << a << " and " << b << " are: ";
 
    // Traverse each number in the interval
    // with the help of for loop
    for (i = a; i <= b; i++) {
        // Skip 0 and 1 as they are
        // niether prime nor composite
        if (i == 1 || i == 0)
            continue;
 
        // flag variable to tell
        // if i is prime or not
        flag = 1;
 
        for (j = 2; j <= i / 2; ++j) {
            if (i % j == 0) {
                flag = 0;
                break;
            }
        }
 
        // flag = 1 means i is prime
        // and flag = 0 means i is not prime
        if (flag == 1)
            cout << i << " ";
    }
 
    return 0;
}
 
// This code is contributed by Akanksha Rai


C
// C program to find the prime numbers
// between a given interval
 
#include 
 
int main()
{
    // Declare the variables
    int a, b, i, j, flag;
 
    // Ask user to enter lower value of interval
    printf("Enter lower bound of the interval: ");
    scanf("%d", &a); // Take input
 
    // Ask user to enter upper value of interval
    printf("\nEnter upper bound of the interval: ");
    scanf("%d", &b); // Take input
 
    // Print display message
    printf("\nPrime numbers between %d and %d are: ", a, b);
 
    // Traverse each number in the interval
    // with the help of for loop
    for (i = a; i <= b; i++) {
        // Skip 0 and 1 as they are
        // niether prime nor composite
        if (i == 1 || i == 0)
            continue;
 
        // flag variable to tell
        // if i is prime or not
        flag = 1;
 
        for (j = 2; j <= i / 2; ++j) {
            if (i % j == 0) {
                flag = 0;
                break;
            }
        }
 
        // flag = 1 means i is prime
        // and flag = 0 means i is not prime
        if (flag == 1)
            printf("%d ", i);
    }
 
    return 0;
}


Java
import java.util.Scanner;
 
// Java program to find the prime numbers
// between a given interval
public class GFG {
 
    // driver code
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        // Declare the variables
        int a, b, i, j, flag;
 
        // Ask user to enter lower value of interval
        System.out.printf("Enter lower bound of the interval: ");
        a = sc.nextInt(); // Take input
 
        // Ask user to enter upper value of interval
        System.out.printf("\nEnter upper bound of the interval: ");
        b = sc.nextInt(); // Take input
 
        // Print display message
        System.out.printf("\nPrime numbers between %d and %d are: ", a, b);
 
        // Traverse each number in the interval
        // with the help of for loop
        for (i = a; i <= b; i++) {
 
            // Skip 0 and 1 as they are
            // niether prime nor composite
            if (i == 1 || i == 0)
                continue;
 
            // flag variable to tell
            // if i is prime or not
            flag = 1;
 
            for (j = 2; j <= i / 2; ++j) {
                if (i % j == 0) {
                    flag = 0;
                    break;
                }
            }
 
            // flag = 1 means i is prime
            // and flag = 0 means i is not prime
            if (flag == 1)
                System.out.println(i);
        }
    }
}


Python3
# Python3 program to find the prime
# numbers between a given interval
 
if __name__ == '__main__':
     
    # Declare the variables
    a, b, i, j, flag = 0, 0, 0, 0, 0
 
    # Ask user to enter lower value of interval
    print("Enter lower bound of the interval:",
                                      end = "")
    a = int(input()) # Take input
    print(a)
     
    # Ask user to enter upper value of interval
    print("Enter upper bound of the interval:",
                                      end = "")
    b = int(input()) # Take input
    print(b)
     
    # Print display message
    print("Prime numbers between", a, "and",
                        b, "are:", end = "")
 
    # Traverse each number in the interval
    # with the help of for loop
    for i in range(a, b + 1):
 
        # Skip 1 as1 is niether
        # prime nor composite
        if (i == 1):
            continue
 
        # flag variable to tell
        # if i is prime or not
        flag = 1
         
        for j in range(2, i // 2 + 1):
            if (i % j == 0):
                flag = 0
                break
             
        # flag = 1 means i is prime
        # and flag = 0 means i is not prime
        if (flag == 1):
            print(i, end = " ")
             
# This code is contributed
# by Mohit kumar 29


C#
// C# program to find the prime numbers
// between a given interval
using System;
 
class GFG{
 
// Driver code   
public static void Main(string[] args)
{
     
    // Declare the variables
    int a, b, i, j, flag;
 
    // Ask user to enter lower value of interval
    Console.WriteLine("Enter lower bound of " +
                      "the interval: ");
                       
    // Take input
    a = int.Parse(Console.ReadLine());
 
    // Ask user to enter upper value of interval
    Console.WriteLine("\nEnter upper bound " +
                      "of the interval: ");
                       
    // Take input
    b = int.Parse(Console.ReadLine());
 
    // Print display message
    Console.WriteLine("\nPrime numbers between " + 
                      "{0} and {1} are: ", a, b);
 
    // Traverse each number in the interval
    // with the help of for loop
    for(i = a; i <= b; i++)
    {
         
        // Skip 0 and 1 as they are
        // niether prime nor composite
        if (i == 1 || i == 0)
            continue;
 
        // flag variable to tell
        // if i is prime or not
        flag = 1;
 
        for(j = 2; j <= i / 2; ++j)
        {
            if (i % j == 0)
            {
                flag = 0;
                break;
            }
        }
 
        // flag = 1 means i is prime
        // and flag = 0 means i is not prime
        if (flag == 1)
            Console.WriteLine(i);
    }
}
}
 
// This code is contributed by jana_sayantan


C++
// C++ program to find the prime numbers
// between a given interval
 
#include 
using namespace std;
 
int main()
{
    // Declare the variables
    int a, b, i, j;
 
    // Ask user to enter lower value of interval please not
    // interval < 0 cannot be prime numbers
    cout << "Enter lower bound of the interval: ";
    cin >> a; // Take input
 
    // Ask user to enter upper value of interval
    cout << "\nEnter upper bound of the interval: ";
    cin >> b; // Take input
 
    // Print display message
    cout << "\nPrime numbers between " << a << " and " << b
         << " are: ";
 
    // Explicitly handling the cases when a is less than 2
    // since 0 and 1 are not prime numbers
    if (a <= 2) {
        a = 2;
        if (b >= 2) {
            cout << a << " ";
            a++;
        }
    }
 
    // MAKING SURE THAT a IS ODD BEFORE WE BEGIN
    // THE LOOP
    if (a % 2 == 0)
        a++;
 
    // NOTE : WE TRAVERSE THROUGH ODD NUMBERS ONLY
    for (i = a; i <= b; i = i + 2) {
 
        // flag variable to tell
        // if i is prime or not
        bool flag = 1;
 
        // WE TRAVERSE TILL SQUARE ROOT OF j only.
        // (LARGEST POSSIBLE VALUE OF A PRIME FACTOR)
        for (j = 2; j * j <= i; ++j) {
            if (i % j == 0) {
                flag = 0;
                break;
            }
        }
 
        // flag = 1 means i is prime
        // and flag = 0 means i is not prime
        if (flag == 1)
            cout << i << " ";
    }
 
    return 0;
}


Java
// Java program to find the prime numbers
// between a given interval
import java.util.Scanner;
  
class GFG {
  
    // driver code
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
         // Declare the variables
    int a, b, i, j,flag;
 
    // Ask user to enter lower value of interval
    System.out.printf( "Enter lower bound of the interval: ");
    a = sc.nextInt(); // Take input
 
    // Ask user to enter upper value of interval
    System.out.printf( "\nEnter upper bound of the interval: ");
    b = sc.nextInt(); // Take input
 
    // Print display message
    System.out.printf("\nPrime numbers between %d and %d are: ", a, b);
 
    // Explicitly handling the cases when a is less than 2
    if (a == 1) {
        System.out.println(a);
        a++;
        if (b >= 2) {
            System.out.println(a);
            a++;
        }
    }
    if (a == 2)
        System.out.println(a);
 
    // MAKING SURE THAT a IS ODD BEFORE WE BEGIN
    // THE LOOP
    if (a % 2 == 0)
        a++;
 
    // NOTE : WE TRAVERSE THROUGH ODD NUMBERS ONLY
    for (i = a; i <= b; i = i + 2) {
 
        // flag variable to tell
        // if i is prime or not
         flag = 1;
 
        // WE TRAVERSE TILL SQUARE ROOT OF j only.
        // (LARGEST POSSIBLE VALUE OF A PRIME FACTOR)
        for (j = 2; j * j <= i; ++j) {
            if (i % j == 0) {
                flag = 0;
                break;
            }
        }
 
        // flag = 1 means i is prime
        // and flag = 0 means i is not prime
        if (flag == 1)
            System.out.println(i);
    }
 
    }
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program to find the prime
# numbers between a given interval
  
if __name__ == '__main__':
     
    # Declare the variables
    a, b, i, j = 0, 0, 0, 0
     
    # Ask user to enter lower value of interval
    print("Enter lower bound of the interval:",end = "")
     
    a = int(input()) # Take input
    print(a)
     
    # Ask user to enter upper value of interval
    print("Enter upper bound of the interval:",end = "")
     
    b = int(input()) # Take input
    print(b)
     
    # Print display message
    print("Prime numbers between", a, "and",b, "are:", end = "")
     
     
    # Explicitly handling the cases when a is less than 2
    if (a == 1):
        print(a,end=" ")
        a+=1
        if (b >= 2):
            print(a,end=" ")
            a+=1
    if (a == 2):
        print(a,end=" ")
     
    # MAKING SURE THAT a IS ODD BEFORE WE BEGIN
    # THE LOOP
    if (a % 2 == 0):
        a+=1
    # NOTE : WE TRAVERSE THROUGH ODD NUMBERS ONLY
    for i in range(a,b+1,2):
         
        # flag variable to tell
        # if i is prime or not
        flag = 1
        # WE TRAVERSE TILL SQUARE ROOT OF j only.
        # (LARGEST POSSIBLE VALUE OF A PRIME FACTOR)
        j = 2
        while(j * j <= i):
            if (i % j == 0):
                flag = 0
                break
            j+=1
         
        # flag = 1 means i is prime
        # and flag = 0 means i is not prime
        if (flag == 1):
            print(i,end=" ")
 
# This code is contributed by shubhamsingh10


C#
// C# program to find the prime numbers
// between a given interval
using System;
class GFG
{
  // Driver code
  static public void Main()
  {
 
    // Declare the variables
    int a, b, i, j, flag;
 
    // Ask user to enter lower value of interval
    Console.Write(
      "Enter lower bound of the interval: ");
    a = Convert.ToInt32(
      Console.ReadLine()); // Take input
 
    // Ask user to enter upper value of interval
    Console.Write(
      "\nEnter upper bound of the interval: ");
    b = Convert.ToInt32(
      Console.ReadLine()); // Take input
 
    // Print display message
    Console.Write("\nPrime numbers between " + a
                  + " and " + b + " are: ");
 
    // Explicitly handling the cases when a is less than
    // 2
    if (a == 1)
    {
      Console.Write(a + " ");
      a++;
      if (b >= 2)
      {
        Console.Write(a + " ");
        a++;
      }
    }
    if (a == 2)
    {
      Console.Write(a + " ");
    }
 
    // MAKING SURE THAT a IS ODD BEFORE WE BEGIN
    // THE LOOP
    if (a % 2 == 0)
    {
      a++;
    }
 
    // NOTE : WE TRAVERSE THROUGH ODD NUMBERS ONLY
    for (i = a; i <= b; i = i + 2)
    {
 
      // flag variable to tell
      // if i is prime or not
      flag = 1;
 
      // WE TRAVERSE TILL SQUARE ROOT OF j only.
      // (LARGEST POSSIBLE VALUE OF A PRIME FACTOR)
      for (j = 2; j * j <= i; ++j)
      {
        if (i % j == 0)
        {
          flag = 0;
          break;
        }
      }
 
      // flag = 1 means i is prime
      // and flag = 0 means i is not prime
      if (flag == 1)
      {
        Console.Write(i + " ");
      }
    }
  }
}
 
// This code is contributed by rag2127


输出:

Enter lower bound of the interval: 1
Enter upper bound of the interval: 10
Prime numbers between 1 and 10 are: 2 3 5 7 

优化的解决方案:
这个想法是利用偶数(除2以外)不是质数这一事实。

C++

// C++ program to find the prime numbers
// between a given interval
 
#include 
using namespace std;
 
int main()
{
    // Declare the variables
    int a, b, i, j;
 
    // Ask user to enter lower value of interval please not
    // interval < 0 cannot be prime numbers
    cout << "Enter lower bound of the interval: ";
    cin >> a; // Take input
 
    // Ask user to enter upper value of interval
    cout << "\nEnter upper bound of the interval: ";
    cin >> b; // Take input
 
    // Print display message
    cout << "\nPrime numbers between " << a << " and " << b
         << " are: ";
 
    // Explicitly handling the cases when a is less than 2
    // since 0 and 1 are not prime numbers
    if (a <= 2) {
        a = 2;
        if (b >= 2) {
            cout << a << " ";
            a++;
        }
    }
 
    // MAKING SURE THAT a IS ODD BEFORE WE BEGIN
    // THE LOOP
    if (a % 2 == 0)
        a++;
 
    // NOTE : WE TRAVERSE THROUGH ODD NUMBERS ONLY
    for (i = a; i <= b; i = i + 2) {
 
        // flag variable to tell
        // if i is prime or not
        bool flag = 1;
 
        // WE TRAVERSE TILL SQUARE ROOT OF j only.
        // (LARGEST POSSIBLE VALUE OF A PRIME FACTOR)
        for (j = 2; j * j <= i; ++j) {
            if (i % j == 0) {
                flag = 0;
                break;
            }
        }
 
        // flag = 1 means i is prime
        // and flag = 0 means i is not prime
        if (flag == 1)
            cout << i << " ";
    }
 
    return 0;
}

Java

// Java program to find the prime numbers
// between a given interval
import java.util.Scanner;
  
class GFG {
  
    // driver code
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
         // Declare the variables
    int a, b, i, j,flag;
 
    // Ask user to enter lower value of interval
    System.out.printf( "Enter lower bound of the interval: ");
    a = sc.nextInt(); // Take input
 
    // Ask user to enter upper value of interval
    System.out.printf( "\nEnter upper bound of the interval: ");
    b = sc.nextInt(); // Take input
 
    // Print display message
    System.out.printf("\nPrime numbers between %d and %d are: ", a, b);
 
    // Explicitly handling the cases when a is less than 2
    if (a == 1) {
        System.out.println(a);
        a++;
        if (b >= 2) {
            System.out.println(a);
            a++;
        }
    }
    if (a == 2)
        System.out.println(a);
 
    // MAKING SURE THAT a IS ODD BEFORE WE BEGIN
    // THE LOOP
    if (a % 2 == 0)
        a++;
 
    // NOTE : WE TRAVERSE THROUGH ODD NUMBERS ONLY
    for (i = a; i <= b; i = i + 2) {
 
        // flag variable to tell
        // if i is prime or not
         flag = 1;
 
        // WE TRAVERSE TILL SQUARE ROOT OF j only.
        // (LARGEST POSSIBLE VALUE OF A PRIME FACTOR)
        for (j = 2; j * j <= i; ++j) {
            if (i % j == 0) {
                flag = 0;
                break;
            }
        }
 
        // flag = 1 means i is prime
        // and flag = 0 means i is not prime
        if (flag == 1)
            System.out.println(i);
    }
 
    }
}
 
// This code is contributed by shivanisinghss2110

Python3

# Python3 program to find the prime
# numbers between a given interval
  
if __name__ == '__main__':
     
    # Declare the variables
    a, b, i, j = 0, 0, 0, 0
     
    # Ask user to enter lower value of interval
    print("Enter lower bound of the interval:",end = "")
     
    a = int(input()) # Take input
    print(a)
     
    # Ask user to enter upper value of interval
    print("Enter upper bound of the interval:",end = "")
     
    b = int(input()) # Take input
    print(b)
     
    # Print display message
    print("Prime numbers between", a, "and",b, "are:", end = "")
     
     
    # Explicitly handling the cases when a is less than 2
    if (a == 1):
        print(a,end=" ")
        a+=1
        if (b >= 2):
            print(a,end=" ")
            a+=1
    if (a == 2):
        print(a,end=" ")
     
    # MAKING SURE THAT a IS ODD BEFORE WE BEGIN
    # THE LOOP
    if (a % 2 == 0):
        a+=1
    # NOTE : WE TRAVERSE THROUGH ODD NUMBERS ONLY
    for i in range(a,b+1,2):
         
        # flag variable to tell
        # if i is prime or not
        flag = 1
        # WE TRAVERSE TILL SQUARE ROOT OF j only.
        # (LARGEST POSSIBLE VALUE OF A PRIME FACTOR)
        j = 2
        while(j * j <= i):
            if (i % j == 0):
                flag = 0
                break
            j+=1
         
        # flag = 1 means i is prime
        # and flag = 0 means i is not prime
        if (flag == 1):
            print(i,end=" ")
 
# This code is contributed by shubhamsingh10

C#

// C# program to find the prime numbers
// between a given interval
using System;
class GFG
{
  // Driver code
  static public void Main()
  {
 
    // Declare the variables
    int a, b, i, j, flag;
 
    // Ask user to enter lower value of interval
    Console.Write(
      "Enter lower bound of the interval: ");
    a = Convert.ToInt32(
      Console.ReadLine()); // Take input
 
    // Ask user to enter upper value of interval
    Console.Write(
      "\nEnter upper bound of the interval: ");
    b = Convert.ToInt32(
      Console.ReadLine()); // Take input
 
    // Print display message
    Console.Write("\nPrime numbers between " + a
                  + " and " + b + " are: ");
 
    // Explicitly handling the cases when a is less than
    // 2
    if (a == 1)
    {
      Console.Write(a + " ");
      a++;
      if (b >= 2)
      {
        Console.Write(a + " ");
        a++;
      }
    }
    if (a == 2)
    {
      Console.Write(a + " ");
    }
 
    // MAKING SURE THAT a IS ODD BEFORE WE BEGIN
    // THE LOOP
    if (a % 2 == 0)
    {
      a++;
    }
 
    // NOTE : WE TRAVERSE THROUGH ODD NUMBERS ONLY
    for (i = a; i <= b; i = i + 2)
    {
 
      // flag variable to tell
      // if i is prime or not
      flag = 1;
 
      // WE TRAVERSE TILL SQUARE ROOT OF j only.
      // (LARGEST POSSIBLE VALUE OF A PRIME FACTOR)
      for (j = 2; j * j <= i; ++j)
      {
        if (i % j == 0)
        {
          flag = 0;
          break;
        }
      }
 
      // flag = 1 means i is prime
      // and flag = 0 means i is not prime
      if (flag == 1)
      {
        Console.Write(i + " ");
      }
    }
  }
}
 
// This code is contributed by rag2127

输出:

Enter lower bound of the interval: 1
Enter upper bound of the interval: 10
Prime numbers between 1 and 10 are: 1 2 3 5 7 

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。