The Principles of Good Programming

本文总结了C#编程中的关键原则与实践,包括DRY原则、抽象、KISS原则、避免YAGNI、简单设计、易于理解的代码、开闭原则、维护性编码、避免意外行为、最小耦合、最大化凝聚性、隐藏实现细节、迪米特法则、避免过早优化、代码重用、分散关注、拥抱变化等。这些原则有助于提高代码质量和可维护性。
 Artima最新文章,作者正在搞C#培训,总结以下几个原理:
1.DRY(拧干代码不要有水分) 不要有重复代码,很多概念实际就是为此存在,比如loops function和classes等等,如果有重复,进行抽象。 http://en.wikipedia.org/wiki/Don%27t_repeat_yourself


2.抽象原理,和DRY有关,代码中没一个重要的功能块都应该拧干抽象一下。 http://en.wikipedia.org/wiki/Abstraction_principle_(programming)

3.KISS(Keep it simple, stupid!),简化避免复杂是关键目标,简单代码花费时间短(代码写得少,脑子动得不见得少),少代码有较少BUGs和更易于修改。(banq:耦合都很高的代码有时很简单,但不易于修改) http://en.wikipedia.org/wiki/KISS_principle

4.避免创造YAGNI,不要增加你不需要的功能: http://en.wikipedia.org/wiki/YAGNI

5.做最简单只要能工作的设计,始终问自己,这样简单设计能够工作吗? http://c2.com/xp/DoTheSimplestThingThatCouldPossiblyWork.html

6.不要让我思考,代码应该易于理解。 http://www.sensible.com/dmmt.html

7.开闭原则 软件实体如classes类 模块和functions应该开放易于扩展,但是不允许修改,不要写其他人能够修改的类,而是写出人们能够扩展的类(banq:可用面向对象的继承 实现等方法扩展多个子类)。
http://en.wikipedia.org/wiki/Open_Closed_Principle

8.写代码要值得将来维护。 http://c2.com/cgi/wiki?CodeForTheMaintainer

8.做最少令人惊讶的事,代码易于理解,名称等各方面不要让人产生惊讶的副作用。 http://en.wikipedia.org/wiki/Single_responsibility_principle

9.最少耦合,代码(代码块,函数,类,等等)的任何部分,应尽量减少对其他地区的代码的依赖关系。这是通过使用尽可能少的共享变量 。“低耦合往往是一个结构完善的计算机系统的标志和一个好的设计,高凝聚力相结合,实现更高的可读性和可维护性的总体目标
http://en.wikipedia.org/wiki/Coupling_(computer_programming)

10.最大化凝聚性:相同功能代码应该在同样一个组件中。 http://en.wikipedia.org/wiki/Cohesion_(computer_science)

11.隐藏实现细节,隐藏实现细节,将允许改变执行代码组件,而最低限度影响的任何其他使用该组件的模块(实现细节怎么做是战术,做什么是方向战略) http://en.wikipedia.org/wiki/Information_Hiding

12.迪米特Demeter法则 ,代码组件只应该和他们的直接关系联系(直系血缘关系),如他们继承的父类,包含的对象和参数传递的对象 http://en.wikipedia.org/wiki/Law_of_Demeter

13.避免过早优化,除非你的代码比你预期慢,否则不要提早优化,过早优化是罪恶根源, http://en.wikipedia.org/wiki/Program_optimization

14.代码能够重用是好的,重用代码提高代码的可靠性,缩短开发时间。 http://en.wikipedia.org/wiki/Code_reuse

15.分散关注:不同功能区域,应该由不同代码和最小重叠的模块组成。(AOP是分散关注典型模式)

16.拥抱变化,这是一本Kent Beck书籍副标题,也被认为是极限编程和敏捷方法在一般的宗旨。最大限度地减少耦合使代码更容易改变。无论你是一个极端编程的医生,这种方法对于编写代码是有道理的。

\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. \section{References} \begin{thebibliography}{9} \bibitem{knuth} Donald E. Knuth. \textit{The Art of Computer Programming, Volume 2: Seminumerical Algorithms}. 3rd Edition, Addison-Wesley, 1997. ISBN: 0-201-89684-2. (See Section 4.5.4 for discussion of prime number sieves.) \bibitem{hardy} G. H. Hardy and E. M. Wright. \textit{An Introduction to the Theory of Numbers}. 6th Edition, Oxford University Press, 2008. ISBN: 978-0-19-921986-5. (Chapter 1 discusses prime numbers and includes historical notes on Eratosthenes.) \bibitem{pomerance} Carl Pomerance. \newblock “A Tale of Two Sieves.” \newblock \textit{Notices of the American Mathematical Society}, vol.~43, no.~12, pp.~1473–1485, December 1996. Available online: \url{https://www.ams.org/journals/notices/199612/199612FullIssue.pdf#page=1473} \bibitem{crandall} Richard Crandall and Carl Pomerance. \textit{Prime Numbers: A Computational Perspective}. 2nd Edition, Springer, 2005. ISBN: 978-0-387-25282-7. (A detailed treatment of sieve methods including Eratosthenes and segmented variants.) \bibitem{eratosthenes-original} Thomas L. Heath (Ed.). \textit{Greek Mathematical Works, Volume II: From Aristarchus to Pappus}. Harvard University Press (Loeb Classical Library), 1941. ISBN: 978-0-674-99396-7. (Contains surviving fragments and references to Eratosthenes’ work in ancient sources.) \end{thebibliography} \end{document} 修改错误 ,并且增加字数在2000字左右
12-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值