首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【UVa】10200 - Prime Time(打表)

【UVa】10200 - Prime Time(打表)

作者头像
FishWang
发布2025-08-27 10:29:41
发布2025-08-27 10:29:41
10400
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

Euler is a well-known matematician, and, among many other things, he discovered that the formula n 2 + n + 41 produces a prime for 0 ≤ n < 40. For n = 40, the formula produces 1681, which is 41 ∗ 41. Even though this formula doesn’t always produce a prime, it still produces a lot of primes. It’s known that for n ≤ 10000000, there are 47,5% of primes produced by the formula! So, you’ll write a program that will output how many primes does the formula output for a certain interval.

Input Each line of input will be given two positive integer a and b such that 0 ≤ a ≤ b ≤ 10000. You must read until the end of the file.

Output For each pair a, b read, you must output the percentage of prime numbers produced by the formula in this interval (a ≤ n ≤ b) rounded to two decimal digits.

Sample Input 0 39 0 40 39 40

Sample Output 100.00 97.56 50.00

题意就是说用 n^2 + n +41 这个公式出来的数是素数的概率。

对0~10000打一个表就行了。

输出的时候注意四舍五入(这一点特别坑)

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
#define f(x) (x*x+x+41)
int sum[10000+11];
bool isPrime(int x)
{
	int endd = sqrt(x);
	for (int i = 2 ; i <= endd ; i++)
	{
		if (x % i == 0)
			return false;
	}
	return true;
}
void getPrime()
{
	sum[0] = 1;
	for (int i = 1 ; i <= 10000 ; i++)
	{
		if (isPrime(f(i)))
			sum[i] = sum[i-1] + 1;
		else
			sum[i] = sum[i-1];
	}
}
int main()
{
	getPrime();
	int n;
	int l,r;
	double ans;
	while (scanf ("%d %d",&l,&r) != EOF)
	{
		if (l == 0)
			ans = (double)(sum[r]) / (r+1);
		else
			ans = (double)(sum[r] - sum[l-1]) / (r - l + 1);
		ans = (int)(ans*10000+0.5) / 100.0;
		printf ("%.2lf\n",ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档