鱼C论坛

 找回密码
 立即注册
查看: 4020|回复: 7

[已解决]输出100-200之间的所有质数,每行输出5个质数,最终输出该范围内质数的个数。

[复制链接]
发表于 2022-10-28 21:03:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
输出100-200之间的所有质数,每行输出5个质数,最终输出该范围内质数的个数。
最佳答案
2022-10-28 21:19:35
  1. c = 0
  2. for n in range(100 , 201):
  3.     for i in range(2 , n):
  4.         if not n % i : break
  5.     else :
  6.         if c:
  7.             print() if not c % 5 else print(' ' , end = '')
  8.         print('%3d' % n , end = '')
  9.         c += 1
  10. print()
  11. print(c)
复制代码

    运行实况:
  1. D:\[00.Exerciese.2022]\Python>python x.py
  2. 101 103 107 109 113
  3. 127 131 137 139 149
  4. 151 157 163 167 173
  5. 179 181 191 193 197
  6. 199
  7. 21

  8. D:\[00.Exerciese.2022]\Python>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-28 21:16:11 | 显示全部楼层
  1. def is_prime(x):
  2.     for i in range(2, int(x**0.5)+2):
  3.         if x % i == 0:
  4.             return False
  5.     return True

  6. primes = [i for i in range(100, 201) if is_prime(i)]
  7. count = 0
  8. for i in primes:
  9.     count += 1
  10.     print(i, end = ' ')
  11.     if count == 5:
  12.         print()
  13.         count = 0
  14. print(len(primes))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-28 21:19:35 | 显示全部楼层    本楼为最佳答案   
  1. c = 0
  2. for n in range(100 , 201):
  3.     for i in range(2 , n):
  4.         if not n % i : break
  5.     else :
  6.         if c:
  7.             print() if not c % 5 else print(' ' , end = '')
  8.         print('%3d' % n , end = '')
  9.         c += 1
  10. print()
  11. print(c)
复制代码

    运行实况:
  1. D:\[00.Exerciese.2022]\Python>python x.py
  2. 101 103 107 109 113
  3. 127 131 137 139 149
  4. 151 157 163 167 173
  5. 179 181 191 193 197
  6. 199
  7. 21

  8. D:\[00.Exerciese.2022]\Python>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-28 21:26:33 | 显示全部楼层
  1. #include <iostream>
  2. #include <cmath>

  3. bool isPrime(unsigned num) {
  4.         if (num < 2) return false;
  5.         else if (num == 2) return true;
  6.         for (int n = 2; n < sqrt(num) + .5; ++n) if (not(num % n))return false;
  7.         return true;
  8. }

  9. using std::cout, std::endl;
  10. int main(void) {
  11.         for (int num = 100, n = 0; num < 200; ++num) {
  12.                 if (isPrime(num)) {
  13.                         n++;
  14.                         cout << num << " ";
  15.                         if (not(n % 5)) cout << endl;
  16.                 }
  17.         }
  18.         return 0;
  19. }
复制代码
  1. 101 103 107 109 113
  2. 127 131 137 139 149
  3. 151 157 163 167 173
  4. 179 181 191 193 197
  5. 199
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-28 23:33:54 | 显示全部楼层
一下子还真不会写,抄了书上的代码
  1. #include <stdio.h>
  2. #include <math.h>

  3. int main()
  4. {
  5.         int m,i,k,n=0;
  6.         for(m=101;m<=200;m=m+2)
  7.         {
  8.                 k=sqrt(m);
  9.                 for(i=2;i <= k;i++)
  10.                 {
  11.                         if(m%i == 0)       
  12.                                 break;
  13.                 }

  14.                 if(i >= k+1)
  15.                 {
  16.                         printf("%4d",m);
  17.                         n=n+1;
  18.                         if((n % 5 == 0))
  19.                         {
  20.                                 printf("\n");
  21.                         }
  22.                 }
  23.         }
  24.        
  25.         printf(" %d \n",n);

  26.         return 0;
  27. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-29 11:41:23 | 显示全部楼层

使用什么编程语言?

本帖最后由 漫星闪 于 2022-10-29 11:43 编辑

  1. for n in range(100,201):
  2.         for x in range(2,n):
  3.                 if n % x == 0:
  4.                         print(n,'=',x,'*',n // x)
  5.                         break
  6.         else:
  7.                 print(n,'是一个质数')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-29 14:02:40 | 显示全部楼层
  1. int main()
  2. {
  3.         int flag,i,j;
  4.         int count = 0;
  5.         int many = 0;
  6.         for (i=100;i<=200;i++)
  7.         {
  8.                 flag = 0;
  9.                 for (j=2;j<i;j++)
  10.                 {
  11.                         if (i % j == 0) flag = 1;
  12.                 }
  13.                 if (flag == 0)
  14.                 {
  15.                         printf("%d",i);
  16.                         if (count != 5) printf(" ");
  17.                         many++;
  18.                         count++;
  19.                 }
  20.                 if (count == 5)
  21.                 {
  22.                         printf("\n");
  23.                         count = 0;
  24.                 }
  25.         }
  26.         printf("\n%d\n",many);
  27.         return 0;
  28. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-6 18:56:39 | 显示全部楼层
这是C++代码
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;

  4. int main()
  5. {
  6.         int m = 0;
  7.         int n = 0;
  8.         int x = 0;

  9.         for (m = 100; m <= 200; m++)
  10.         {
  11.                 for (n = 2; n <= m; n++)
  12.                 {
  13.                         if (m % n == 0)
  14.                         {
  15.                                 break;
  16.                         }
  17.                 }
  18.                 if (m == n)
  19.                 {
  20.                         cout << m << " ";
  21.                         x++;

  22.                         if (x % 5 == 0)
  23.                         {
  24.                                 cout << endl;
  25.                         }
  26.                 }

  27.         }
  28.         cout << endl;
  29.         return 0;
  30. }
复制代码

此帖为转载贴,原文链接:https://blog.csdn.net/weixin_46535880/article/details/112853929
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-14 19:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表