输出100-200之间的所有质数,每行输出5个质数,最终输出该范围内质数的个数。
输出100-200之间的所有质数,每行输出5个质数,最终输出该范围内质数的个数。 def is_prime(x):for i in range(2, int(x**0.5)+2):
if x % i == 0:
return False
return True
primes =
count = 0
for i in primes:
count += 1
print(i, end = ' ')
if count == 5:
print()
count = 0
print(len(primes)) c = 0
for n in range(100 , 201):
for i in range(2 , n):
if not n % i : break
else :
if c:
print() if not c % 5 else print(' ' , end = '')
print('%3d' % n , end = '')
c += 1
print()
print(c)
运行实况:
D:\\Python>python x.py
101 103 107 109 113
127 131 137 139 149
151 157 163 167 173
179 181 191 193 197
199
21
D:\\Python> #include <iostream>
#include <cmath>
bool isPrime(unsigned num) {
if (num < 2) return false;
else if (num == 2) return true;
for (int n = 2; n < sqrt(num) + .5; ++n) if (not(num % n))return false;
return true;
}
using std::cout, std::endl;
int main(void) {
for (int num = 100, n = 0; num < 200; ++num) {
if (isPrime(num)) {
n++;
cout << num << " ";
if (not(n % 5)) cout << endl;
}
}
return 0;
}101 103 107 109 113
127 131 137 139 149
151 157 163 167 173
179 181 191 193 197
199 一下子还真不会写,抄了书上的代码
#include <stdio.h>
#include <math.h>
int main()
{
int m,i,k,n=0;
for(m=101;m<=200;m=m+2)
{
k=sqrt(m);
for(i=2;i <= k;i++)
{
if(m%i == 0)
break;
}
if(i >= k+1)
{
printf("%4d",m);
n=n+1;
if((n % 5 == 0))
{
printf("\n");
}
}
}
printf(" %d \n",n);
return 0;
}
使用什么编程语言?
本帖最后由 漫星闪 于 2022-10-29 11:43 编辑for n in range(100,201):
for x in range(2,n):
if n % x == 0:
print(n,'=',x,'*',n // x)
break
else:
print(n,'是一个质数')
int main()
{
int flag,i,j;
int count = 0;
int many = 0;
for (i=100;i<=200;i++)
{
flag = 0;
for (j=2;j<i;j++)
{
if (i % j == 0) flag = 1;
}
if (flag == 0)
{
printf("%d",i);
if (count != 5) printf(" ");
many++;
count++;
}
if (count == 5)
{
printf("\n");
count = 0;
}
}
printf("\n%d\n",many);
return 0;
} 这是C++代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int m = 0;
int n = 0;
int x = 0;
for (m = 100; m <= 200; m++)
{
for (n = 2; n <= m; n++)
{
if (m % n == 0)
{
break;
}
}
if (m == n)
{
cout << m << " ";
x++;
if (x % 5 == 0)
{
cout << endl;
}
}
}
cout << endl;
return 0;
}
此帖为转载贴,原文链接:https://blog.csdn.net/weixin_46535880/article/details/112853929
页:
[1]