📜  TCS编码实践问题|质数最大为N

📅  最后修改于: 2021-05-31 23:54:29             🧑  作者: Mango

给定数字N,任务是使用命令行参数查找从1到N的质数。

例子:

Input: N = 7
Output: 2, 3, 5, 7

Input: N = 13
Output: 2, 3, 5, 7, 11, 13 

方法:

  • 由于该数字是作为命令行参数输入的,因此不需要专用的输入行
  • 从命令行参数中提取输入数字
  • 提取的数字将为String类型。
  • 将此数字转换为整数类型并将其存储在变量中,例如N
  • 现在,使用变量i遍历2到N之间的数字,以检查它们是否为质数。这样做如下:
    在每次迭代中
    • 检查从2到(i / 2 + 1)的任何数字是否将i完全除(即,是否为i的因数)。
    • 如果是,那么我不是素数。因此检查下一个号码
    • 如果否,则我是质数。因此,打印i的值并检查下一个数字
  • 循环结束后,从1到N的质数会显示在屏幕上。

注意:请注意,在这种情况下不会选中1,因为1是零素数或复合数。

程序:

C
// C program to find
// the Prime Numbers from 1 to N
// using command line arguments
  
#include 
  
#include  /* atoi */
  
// Function to check if x is prime
int isPrime(int x)
{
    int i;
  
    // Loop to check if x has any factor
    // other than 1 and x itself
    for (i = 2; i < x / 2 + 1; i++) {
        if (x % i == 0) {
            // Since i is a factor of x
            // x is not prime
            return 0;
        }
    }
  
    // x is prime
    return 1;
}
  
// Function to find prime numbers up to n
void findPrimes(int n)
{
    int i;
  
    // Loop from 2 to n
    // to find all prime numbers in between
    for (i = 2; i <= n; i++) {
  
        // Check if i is prime
        // If yes then print it
        // else continue to next number
        if (isPrime(i) == 1)
            printf("%d, ", i);
    }
  
    printf("\n");
}
  
// Driver code
int main(int argc, char* argv[])
{
  
    int n;
  
    // Check if the length of args array is 1
    if (argc == 1)
        printf("No command line arguments found.\n");
    else {
  
        // Get the command line argument and
        // Convert it from string type to integer type
        // using function "atoi( argument)"
        n = atoi(argv[1]);
  
        // Find all prime numbers upto n
        findPrimes(n);
    }
    return 0;
}


Java
// Java program to find
// the Prime Numbers from 1 to N
// using command line arguments
  
class GFG {
  
    // Function to check if x is prime
    public static int isPrime(int x)
    {
        int i;
  
        // Loop to check if x has any factor
        // other than 1 and x itself
        for (i = 2; i < x / 2 + 1; i++) {
            if (x % i == 0) {
                // Since i is a factor of x
                // x is not prime
                return 0;
            }
        }
  
        // x is prime
        return 1;
    }
  
    // Function to find prime numbers up to n
    public static void findPrimes(int n)
    {
        int i;
  
        // Loop from 2 to n
        // to find all prime numbers in between
        for (i = 2; i <= n; i++) {
  
            // Check if i is prime
            // If yes then print it
            // else continue to next number
            if (isPrime(i) == 1)
                System.out.print(i + ", ");
        }
  
        System.out.println();
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Check if length of args array is
        // greater than 0
        if (args.length > 0) {
  
            // Get the command line argument and
            // Convert it from string type to integer type
            int n = Integer.parseInt(args[0]);
  
            // Find all prime numbers upto n
            findPrimes(n);
        }
        else
            System.out.println("No command line "
                               + "arguments found.");
    }
}


输出:

  • 在C中:
  • 在Java:
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”