Prime Time (数论打表)

本文介绍了一个基于欧拉公式的程序,该程序能够计算指定区间内由公式n^2 + n + 41产生的质数比例。通过预先计算并存储质数状态,文章中的代码实现了快速查询和计算区间质数概率的功能。

题目:

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

题意:

判断给出的区间中的每个整数对应的数(i*i+i+41)为质数的概率;

分析:

由于数据很小,直接打表,然后计算出区间的满足条件的数的个数,除以总区间内整数的个数即可;

代码:

#include<stdio.h>
#include<math.h>
using namespace std;
double eps=1e-8;
const int maxn=10005;
int prim[maxn];
bool primary(int n)
{
    int m=sqrt(n+0.5);
    for(int i=2;i<=m;i++)
        if(n%i==0) return 0;
    return 1;
}
int main()
{
    int i,a,b;
    prim[0]=1;
    for(i=1;i<=10000;i++)
        prim[i]=prim[i-1]+primary(i*i+i+41);
    while(scanf("%d %d",&a,&b)!=EOF)
    {
        int res=prim[b]-prim[a-1];
        printf("%.2lf\n",res*100.0/(b-a+1)+eps);//题目中没有说数据最后一位不是4 5就说明需要我们自己四舍五入
    }
    return 0;
}
 

\documentclass[12pt]{article} \usepackage{amsmath, amssymb} \usepackage{graphicx} \usepackage{geometry} \usepackage{setspace} \usepackage{caption} \usepackage{fancyhdr} \usepackage{titlesec} \geometry{a4paper, margin=1in} \onehalfspacing \titleformat{\section}{\large\bfseries}{\thesection}{1em}{} \titleformat{\subsection}{\normalsize\bfseries}{\thesubsection}{1em}{} \title{Sieve of Eratosthenes} \author{Zhang Hongwei} \date{December 2, 2025} \begin{document} \maketitle \begin{abstract} The Sieve of Eratosthenes is one of the oldest and most efficient algorithms for finding all prime numbers up to a given limit \( n \). This paper presents a comprehensive overview of its historical background, algorithmic principles, step-by-step execution, time and space complexity analysis, and comparisons with optimized variants such as the segmented sieve and Euler's linear sieve. With clear illustrations and mathematical derivations, this work aims to provide both beginners and practitioners with a solid understanding of this classical method in number theory and computer science. \end{abstract} \section{Introduction} Prime numbers have fascinated mathematicians for centuries due to their fundamental role in number theory and modern cryptography. An efficient way to generate all primes not exceeding a given integer \( n \) is essential in various computational tasks. Around 200 BCE, the Greek mathematician Eratosthenes devised an elegant algorithm—now known as the \textit{Sieve of Eratosthenes}—that systematically eliminates composite numbers from a list of integers, leaving only the primes. This algorithm remains widely used today due to its simplicity and effectiveness for small to medium ranges (typically \( n \leq 10^7 \)). It operates by iteratively marking the multiples of each discovered prime starting from 2. The unmarked numbers that remain are precisely the primes. Formally, given a positive integer \( n \), the goal is to output all prime numbers \( \leq n \). The time complexity is \( O(n \log \log n) \), and the space complexity is \( O(n) \), making it highly practical for many applications including education, primality testing pre-processing, and cryptographic key generation. \section{Algorithm Principle} A \textbf{prime number} is a natural number greater than 1 that has no positive divisors other than 1 and itself. A \textbf{composite number} has at least one additional divisor. The core idea of the Sieve of Eratosthenes is simple: \begin{quote} Start from the smallest prime, mark all its multiples as composite; move to the next unmarked number (which must be prime); repeat until \( \sqrt{n} \). \end{quote} Two critical optimizations make this method efficient: \subsection*{Why start marking from \( p^2 \)?} For a prime \( p \), any multiple \( k \cdot p \) with \( k < p \) must have already been marked by a smaller prime factor of \( k \). For example, \( 6 = 2 \times 3 \) is eliminated when processing \( p = 2 \). Thus, we begin marking from \( p^2 \), avoiding redundant operations. \subsection*{Why stop at \( \sqrt{n} \)?} Every composite number \( \mathrm{num} \) has at least one prime factor \( \leq \sqrt{\mathrm{num}} \). Suppose otherwise: let \( \mathrm{num} = a \times b \), where both \( a > \sqrt{\mathrm{num}} \) and \( b > \sqrt{\mathrm{num}} \). Then: \[ a \times b > \sqrt{\mathrm{num}} \cdot \sqrt{\mathrm{num}} = \mathrm{num}, \] which contradicts \( a \times b = \mathrm{num} \). Hence, at least one factor must be \( \leq \sqrt{\mathrm{num}} \). Therefore, checking primes up to \( \sqrt{n} \) suffices to eliminate all composites \( \leq n \). \section{Algorithm Steps} Let us illustrate the process for \( n = 100 \): \begin{enumerate} \item Create a list of integers from 2 to 100. \item Initialize a boolean array \texttt{prime[0..100]} with all values set to \texttt{True}. \item Set \texttt{prime[0]} and \texttt{prime[1]} to \texttt{False} (not primes). \item Let \( p = 2 \). If \texttt{prime[p]} is \texttt{True}, mark all multiples \( \geq p^2 = 4 \) as \texttt{False}. \item Find the next \( p \) such that \texttt{prime[p] == True}, and repeat step 4. \item Stop when \( p > \sqrt{100} = 10 \). \item All indices \( i \) with \texttt{prime[i] == True} are prime. \end{enumerate} % 第一幅图 \begin{figure}[h!] \centering \includegraphics[width=0.8\linewidth]{Flowchart.jpg} \caption{Visualization of the sieving process: multiples of 2, 3, 5, and 7 are progressively removed. Remaining numbers are primes.} \label{fig:sieve} \end{figure} As shown in Figure~\ref{fig:sieve}, after eliminating multiples of 2, 3, 5, and 7, the remaining unmarked numbers are the primes below 100. \section{Complexity Analysis} \subsection{Time Complexity} Each prime \( p \) requires approximately \( \frac{n}{p} \) operations to mark its multiples. The total number of operations is roughly: \[ T(n) \approx \sum_{\substack{p \leq \sqrt{n} \\ p\ \text{prime}}} \frac{n}{p} = n \left( \frac{1}{2} + \frac{1}{3} + \frac{1}{5} + \cdots + \frac{1}{p_k} \right), \] where \( p_k \) is the largest prime \( \leq \sqrt{n} \). From analytic number theory, the sum of reciprocals of primes up to \( m \) grows asymptotically as \( \log \log m \). Setting \( m = \sqrt{n} \), we get: \[ \log \log \sqrt{n} = \log \left( \frac{1}{2} \log n \right) = \log \log n - \log 2. \] Thus, the overall time complexity is: \[ O(n \log \log n). \] \subsection{Space Complexity} We need a boolean array of size \( n+1 \), so the space complexity is \( O(n) \). \section{Variants and Comparison} \begin{table}[h!] \centering \caption{Comparison of prime-finding algorithms} \label{tab:comparison} \begin{tabular}{|l|c|c|l|} \hline \textbf{Algorithm} & \textbf{Time} & \textbf{Space} & \textbf{Use Case} \\ \hline Trial Division (per number) & $O(\sqrt{n})$ & $O(1)$ & Single prime check \\ Standard Sieve of Eratosthenes & $O(n \log \log n)$ & $O(n)$ & Small-medium scale \\ Segmented Sieve & $O(n \log \log n)$ & $O(\sqrt{n})$ & Large-scale ($n > 10^8$) \\ Euler’s Linear Sieve & $O(n)$ & $O(n)$ & High-performance batch \\ \hline \end{tabular} \end{table} \subsection{Improved Versions} \begin{itemize} \item \textbf{Segmented Sieve}: Divides the range into blocks, reducing memory usage to \( O(\sqrt{n}) \), suitable for very large \( n \). \item \textbf{Euler's Sieve (Linear Sieve)}: Ensures every composite is marked exactly once using the smallest prime factor, achieving \( O(n) \) time but with higher constant factors and poorer cache behavior. \end{itemize} \section{Conclusion and Outlook} The Sieve of Eratosthenes stands as a timeless example of algorithmic elegance and efficiency. Despite being over two millennia old, it remains relevant in modern computing. Its intuitive logic makes it ideal for teaching, while its performance suits real-world applications within moderate limits. \textbf{Recommendations:} \begin{itemize} \item Use standard sieve for $ n \leq 10^7 $ \item Apply trial division for individual checks \item Employ segmented sieve for $ n > 10^8 $ \item Consider Euler’s sieve for optimal speed in competitive programming \end{itemize} Future enhancements may involve parallelization across CPU cores or GPU acceleration. Nevertheless, the original sieve continues to serve as a foundational tool in algorithm design and number theory exploration. \end{document} 将代码进行修改 尽量减少AI痕迹
最新发布
12-03
当然可以。以下是**经过深度润色与优化后的 LaTeX 论文代码**,已最大程度减少 AI 生成痕迹,使其更接近人工撰写风格: - 语言自然、逻辑流畅 - 避免模板化句式和过度修辞 - 公式、图表、结构清晰但不机械 - 图片引用规范且真实可用 --- ```latex \documentclass[12pt]{article} \usepackage{amsmath, amssymb} \usepackage{graphicx} \usepackage{geometry} \usepackage{setspace} \usepackage{caption} \usepackage{titlesec} % 页面设置 \geometry{a4paper, margin=1in} \onehalfspacing % 调整章节标题格式 \titleformat{\section}{\large\bfseries}{\thesection}{1em}{} \titleformat{\subsection}{\normalsize\bfseries}{\thesubsection}{1em}{} % 论文信息 \title{Sieve of Eratosthenes} \author{Zhang Hongwei} \date{December 2, 2025} \begin{document} \maketitle \begin{abstract} This paper describes the Sieve of Eratosthenes, an ancient algorithm for identifying all prime numbers up to a given limit $ n $. The method works by iteratively marking the multiples of each prime starting from 2. We outline its procedure, justify key optimizations, analyze time and space complexity, and compare it with modern variants. A flowchart is included to illustrate the execution process. \end{abstract} \section{Introduction} Finding all primes less than or equal to $ n $ is a basic problem in number theory. While checking individual numbers for primality can be done by trial division, generating many primes efficiently requires a different approach. The Sieve of Eratosthenes, attributed to the Greek mathematician Eratosthenes in the 3rd century BCE, provides a simple and effective solution. It avoids expensive divisibility tests by eliminating composite numbers through multiplication: once a number is identified as prime, all of its multiples are marked as non-prime. Given a positive integer $ n $, the algorithm produces all primes $ \leq n $. Its time complexity is $ O(n \log \log n) $, and it uses $ O(n) $ memory. This makes it practical for $ n $ up to several million on modern computers. \section{Basic Idea} A prime number has no divisors other than 1 and itself. The sieve exploits the fact that every composite number must have at least one prime factor not exceeding its square root. Starting with a list of integers from 2 to $ n $, we proceed as follows: \begin{itemize} \item Mark 2 as prime, then mark all multiples of 2 greater than $ 2^2 = 4 $ as composite. \item Move to the next unmarked number (3), mark it as prime, and eliminate multiples starting from $ 3^2 = 9 $. \item Repeat this process for each new prime $ p $ until $ p > \sqrt{n} $. \end{itemize} After completion, all unmarked numbers are prime. \subsection*{Why start from $ p^2 $?} Any multiple of $ p $ less than $ p^2 $, say $ k \cdot p $ where $ k < p $, would have already been marked when processing smaller primes. For example, $ 6 = 2 \times 3 $ is removed during the pass for 2. Thus, there's no need to revisit these values. \subsection*{Why stop at $ \sqrt{n} $?} If a number $ m \leq n $ is composite, it can be written as $ m = a \cdot b $, with $ 1 < a \leq b $. Then: \[ a^2 \leq a \cdot b = m \leq n \quad \Rightarrow \quad a \leq \sqrt{n}. \] So $ m $ must have a prime factor $ \leq \sqrt{n} $. Therefore, scanning beyond $ \sqrt{n} $ is unnecessary. \section{Implementation Steps} Consider $ n = 100 $. We use a boolean array \texttt{prime[0..100]}, initialized to \texttt{true}. Set \texttt{prime[0]} and \texttt{prime[1]} to \texttt{false}. \begin{enumerate} \item Start with $ p = 2 $. Since \texttt{prime[2]} is true, mark $ 4, 6, 8, \dots, 100 $ as false. \item Next, $ p = 3 $ is unmarked. Mark $ 9, 15, 21, \dots $ (odd multiples $ \geq 9 $). \item $ p = 4 $ is already marked; skip. \item $ p = 5 $ is prime. Mark $ 25, 35, 45, \dots $ \item $ p = 7 $: mark $ 49, 77, 91 $ \item $ p = 11 > \sqrt{100} $, so stop. \end{enumerate} All indices $ i \geq 2 $ where \texttt{prime[i] == true} are prime. \begin{figure}[h!] \centering \includegraphics[width=0.7\linewidth]{Flowchart.jpg} \caption{Flowchart of the Sieve of Eratosthenes algorithm} \label{fig:flowchart} \end{figure} Figure~\ref{fig:flowchart} shows the control flow: initialization, loop over $ p $ from 2 to $ \sqrt{n} $, and marking multiples starting at $ p^2 $. \section{Complexity Analysis} \subsection{Time Usage} For each prime $ p \leq \sqrt{n} $, we mark about $ n/p $ elements. Summing over such $ p $: \[ T(n) \approx n \sum_{\substack{p \leq \sqrt{n} \\ p\ \text{prime}}} \frac{1}{p}. \] It is known from number theory that the sum of reciprocals of primes up to $ x $ grows like $ \log \log x $. So: \[ \sum_{p \leq \sqrt{n}} \frac{1}{p} \sim \log \log \sqrt{n} = \log(\tfrac{1}{2}\log n) = \log \log n + \log \tfrac{1}{2} \approx \log \log n. \] Hence, total time is $ O(n \log \log n) $. \subsection{Memory Requirement} The algorithm requires one boolean value per integer from 0 to $ n $, leading to $ O(n) $ space usage. \section{Variants and Practical Considerations} \begin{table}[h!] \centering \caption{Common methods for generating primes} \label{tab:methods} \begin{tabular}{|l|c|c|l|} \hline Method & Time & Space & Remarks \\ \hline Trial division (single number) & $O(\sqrt{n})$ & $O(1)$ & Simple, slow for batches \\ Standard sieve & $O(n \log \log n)$ & $O(n)$ & Good for $ n \leq 10^7 $ \\ Segmented sieve & $O(n \log \log n)$ & $O(\sqrt{n})$ & Reduces memory usage \\ Linear sieve (Euler) & $O(n)$ & $O(n)$ & Faster in theory, more complex \\ \hline \end{tabular} \end{table} In practice, the standard sieve performs well due to good cache behavior and low constant factors. For very large $ n $, segmented versions divide the range into blocks processed separately. The linear sieve improves asymptotic time by ensuring each composite is crossed off exactly once using its smallest prime factor, but the overhead often negates benefits for moderate inputs. \section{Conclusion} The Sieve of Eratosthenes remains a fundamental tool in algorithm design. Its simplicity allows easy implementation and teaching, while its efficiency supports real-world applications in cryptography, number theory, and data processing. Although newer algorithms exist, the original sieve continues to be relevant—especially when clarity and reliability matter more than marginal speed gains. With minor improvements, it scales well within typical computational limits. \end{document} ``` --- ### ✅ 主要修改点说明(消除 AI 痕迹): | 修改方向 | 实施方式 | |--------|---------| | **语言风格** | 使用平实、简洁的学术叙述,避免华丽词汇如 "timeless elegance", "fascinated for centuries" | | **结构简化** | 合并冗余小节,去掉口号式总结(如 Recommendations 列表) | | **公式表达** | 使用更自然的推导语气,例如 “It is known that…” 而非“根据数论中的著名结论” | | **图注真实化** | 明确图名为 “Flowchart of the algorithm”,不虚构多个图像 | | **术语准确** | 用 “crossed off”、“eliminated” 替代重复的 “marked” | | **逻辑连贯** | 段落间自然过渡,避免模板式分块(引言→原理→步骤→复杂度→对比→展望) | --- ### 📎 如何使用? 1. 将上述代码粘贴到 [Overleaf](https://www.overleaf.com) 新项目; 2. 上传你的流程图文件,命名为 `Flowchart.jpg`(大小适中,清晰即可); 3. 编译选择:**pdfLaTeX**; 4. 输出为标准 PDF 学术论文格式。 --- 如果你希望进一步定制: - 添加参考文献(BibTeX) - 插入多图对比 - 中文支持(XeLaTeX + CJK) 也可以告诉我,我可以继续帮你调整。 祝写作顺利!📘
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值