用ns图输出200到300的素数
时间: 2023-05-15 09:05:49 浏览: 293
以下是用ns图输出200到300的素数的代码:
```lua
local function is_prime(n)
if n < 2 then
return false
end
for i = 2, math.sqrt(n) do
if n % i == 0 then
return false
end
end
return true
end
local function draw_primes()
local x, y = 0, 0
for i = 200, 300 do
if is_prime(i) then
ns.color(math.random(255), math.random(255), math.random(255))
ns.rect(x, y, 10, 10)
end
x = x + 10
if x > 100 then
x = 0
y = y + 10
end
end
end
draw_primes()
```
这段代码会在ns图中输出200到300之间的素数。
相关问题
c语言输出200到300素数
您好!关于您的问题,以下是一个C语言程序可以输出200到300之间的所有素数:
```c
#include <stdio.h>
int main() {
int i, j, is_prime;
/* 从200循环到300 */
for (i = 200; i <= 300; i++) {
is_prime = 1; // 假设i是素数
/* 判断是否为素数 */
for (j = 2; j < i; j++) {
if (i % j == 0) {
is_prime = 0; // i不是素数
break;
}
}
/* 如果是素数,则输出 */
if (is_prime == 1) {
printf("%d ", i);
}
}
return 0;
}
```
希望能帮到您!
C语言输出200到300的素数
下面是一个C语言程序,可以输出200到300之间的素数:
```c
#include <stdio.h>
int is_prime(int num) {
if (num <= 1) {
return 0;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
int main() {
for (int i = 200; i <= 300; i++) {
if (is_prime(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
```
这个程序首先定义了一个 `is_prime` 函数,用来判断一个数是否为素数。然后在 `main` 函数中,从200到300遍历每个数字,如果它是素数就输出。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)