D - Primes

Write a program to read in a list of integers and determine whether or not each number is prime. A number, n, is prime if its only divisors are 1 and n. For this problem, the numbers 1 and 2 are not considered primes. 

Each input line contains a single integer. The list of integers is terminated with a number<= 0. You may assume that the input contains at most 250 numbers and each number is less than or equal to 16000. 

The output should consists of one line for every number, where each line first lists the problem number, followed by a colon and space, followed by "yes" or "no". 

Sample Input

1
2
3
4
5
17
0

Sample Output

1: no
2: no
3: yes
4: no
5: yes
6: yes

欧拉筛做的开始一直wa。发现bool数组小了。

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool vis[20001]={false};
int susu[6000],x;
void oula(int n)
{
    for(int i=2;i<=n;i++)
    {
        if(!vis[i]) susu[x++]=i;
        for(int j=0;j<x;j++)
        {
            if(i*susu[j]>n) break;
            vis[i*susu[j]]=true;
            if(i%susu[j]==0) break;
        }
    }
}
int main()
{
    oula(20000);
    int cnt=0,n;
    while(1)
    {
        cnt++;
        scanf("%d",&n);
        if(n<=0) break;
        int flag=0;
        if(n==1||n==2)
        {
            printf("%d: no\n",cnt);
            continue;
        }
        else
        {
            for(int i=0;i<x;i++)
            {
                if(susu[i]==n)
                {
                    printf("%d: yes\n",cnt);
                    flag=1;
                    break;
                }
                if(susu[i]>n) break;
            }
        }
        if(flag==0) printf("%d: no\n",cnt);
    }
    return 0;
}

 

### XV6 Primes 实验教程 #### 实现目标 `primes` 是一个经典的 UNIX 工具程序,用于通过管道机制筛选素数。在 XV6 的环境中实现 `primes` 程序的目标是让学生理解如何编写基于标准输入/输出流的工具,并将其与其他命令组合使用。 #### 功能描述 当运行 `make qemu` 启动 XV6 并执行以下命令时[^1]: ```bash $ primes prime 2 prime 3 prime 5 ... ``` 该程序会读取来自标准输入的一系列整数,并将其中的素数写入到标准输出中。此功能可以通过管道与其他命令结合使用来构建更复杂的处理流程。 --- ### XV6 Primes 实现原理 #### 输入与输出设计 `primes` 需要从标准输入读取一系列正整数,并判断这些数字是否为素数。如果是素数,则打印它们;如果不是,则忽略之。具体逻辑如下: - **输入源**:从标准输入获取数据。 - **输出目标**:向标准输出发送符合条件的数据(即素数)。 #### 判断素数的方法 为了高效地检测某个数字 \( n \) 是否为素数,可以采用试除法。这种方法尝试用小于等于 \( \sqrt{n} \) 的所有质数去除 \( n \),如果没有找到任何因数,则说明 \( n \) 是素数。 以下是伪代码表示的核心算法[^3]: ```c int is_prime(int num) { if (num < 2) return 0; int limit = sqrt(num); for (int i = 2; i <= limit; i++) { if (num % i == 0) return 0; } return 1; } ``` 上述函数返回值为 1 表示当前数字是一个素数,而返回值为 0 则不是。 #### 主循环结构 整个程序围绕一个无限循环展开,在每次迭代过程中完成以下操作: 1. 使用 `read()` 函数从标准输入缓冲区提取下一个待测试数值; 2. 调用辅助方法验证其是否满足条件——此处特指检验它是不是素数; 3. 如果确认属于所需类别则调用 `printf()` 输出相应消息至终端屏幕或者重定向文件位置处。 最终版本可能看起来像这样: ```c #include "kernel/types.h" #include "user/user.h" // Function to check whether a given integer 'n' is prime. int is_prime(int n){ ... } void main(){ char buf[5]; while(read(0,buf,sizeof(buf))>0){ int value=atoi(buf); if(is_prime(value)){ printf("prime %d\n",value); } } } ``` 注意这里省略了一些细节部分比如错误处理以及边界情况考虑等方面的内容以便于展示主要思路框架。 --- ### §相关问题§ 1. 如何修改现有的 `sleep.c` 文件以支持用户指定的时间间隔? 2. 在 XV6 中实现多进程协作时需要注意哪些关键点? 3. XV6 的 I/O 子系统是如何工作的?这对我们开发类似 `primes` 这样的应用程序有何影响? 4. 可否优化现有素数检测算法使其更加高效适用于大规模数据集场景下应用需求? 5. 探讨一下 XV6 下其他经典UNIX实用程序如 `grep`, `sort` 等的设计理念及其背后的技术支撑要素是什么?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值