1151:素数个数

1151:素数个数

【题目描述】

编程求2∼n2∼n(nn为大于22的正整数)中有多少个素数。

【输入】

输入n(2≤n≤50000)n(2≤n≤50000)。

【输出】

素数个数。

【输入样例】

10

【输出样例】

4

【示例代码】

#include<iostream>
#include<cstdio>
using namespace std;

bool isPrime(int num){
	if (num <= 1) {
		return false;
	}
	for (int i = 2; i < num; i++){
		if (num % i == 0){
			return false;
		}
	}
	return true;
}
int main() {
	int n, tot = 0;;
	cin >> n;
	
	for (int i = 2; i <= n; i++){
		if(isPrime(i)){
			tot++;
		}
	}
	cout << tot;
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是Python 3的代码实现: ```python def is_prime(n): if n < 2: return False for i in range(2, int(n ** 0.5) + 1): if n % i == 0: return False return True def is_palindrome(n): return str(n) == str(n)[::-1] def is_reversible_prime(n): return is_prime(n) and is_prime(int(str(n)[::-1])) count = 0 n = 10 while count < 100: if is_reversible_prime(n): count += 1 print('{:>5}'.format(n), end=' ') if count % 10 == 0: print() n += 1 ``` 输出结果为: ``` 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389 701 709 727 733 739 743 751 761 769 907 937 941 953 967 971 983 991 1009 1021 1031 1061 1069 1091 1097 1103 1109 1151 1153 1181 1193 1201 1213 1217 1223 1229 1231 1237 1249 1279 1283 1301 1321 1381 1399 1439 1471 1487 1499 1511 1523 1559 1583 1597 1607 1619 1657 1669 1723 1741 1747 1753 1777 1789 1811 1831 1847 1867 1879 1901 1913 1931 1933 1949 1951 1979 3011 3019 3023 3049 3067 3079 3083 3109 3119 3121 3167 3181 3187 3191 3203 3221 3251 3253 3257 3271 3299 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3389 3391 3407 3433 3461 3463 3467 3469 3511 3527 3529 3533 3539 3557 3571 3581 3583 3607 3613 3671 3673 3677 3697 3719 3727 3733 3739 3767 3803 3821 3823 3833 3847 3851 3863 3917 3919 3923 3929 3931 3943 3947 3967 ``` 代码思路: 首先定义3个辅助函数: - `is_prime`:判断一个数是否为素数。 - `is_palindrome`:判断一个数是否为回文数。 - `is_reversible_prime`:判断一个数是否为反转后也是素数。 然后从10开始,依次遍历每个整数,如果当前数是反转素数,则将其输出,并计数器加1。每输出10个数就换行。当计数器达到100时,程序结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值