📜  TCS 编码练习题 |最多 N 的素数

📅  最后修改于: 2021-10-23 07:52:11             🧑  作者: Mango

给定一个数字 N,任务是使用命令行参数找到从 1 到 N 的素数。
例子:

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

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

方法:

  • 由于数字是作为命令行参数输入的,因此不需要专用的输入行
  • 从命令行参数中提取输入数字
  • 这个提取的数字将是字符串类型。
  • 将此数字转换为整数类型并将其存储在变量中,例如 N
  • 现在循环遍历从 2 到 N 的数字,使用变量 i 来检查它们是否是素数。这是按如下方式完成的:
    在每次迭代中,
    • 检查从 2 到 (i/2+1) 的任何数字是否完全除以 i(即它是否是 i 的因数)。
    • 如果是,则 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++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程