【发布时间】:2015-06-05 13:48:13
【问题描述】:
如果我们列出所有小于 10 且是 3 或 5 倍数的自然数,我们会得到 3、5、6 和 9。这些倍数之和是 23。
求1000以下所有3或5的倍数之和。
这是我的代码:
\documentclass[10pt,a4paper]{article}
\usepackage{hyperref}
\newcommand*\rfrac[2]{{}^{#1}\!/_{#2}}
\title{Solution to Project Euler Problem 1}
\author{Aadit M Shah}
\begin{document}
\maketitle
We want to find the sum of all the multiples of 3 or 5 below 1000. We can use the formula of the $n^{th}$ triangular number\footnote{\url{http://en.wikipedia.org/wiki/Triangular_number}} to calculate the sum of all the multiples of a number $m$ below 1000. The formula of the $n^{th}$ triangular number is:
\begin{equation}
T_n = \sum_{k = 1}^n k = 1 + 2 + 3 + \ldots + n = \frac{n (n + 1)}{2}
\end{equation}
If the last multiple of $m$ below 1000 is $x$ then $n = \rfrac{x}{m}$. The sum of all the multiples of $m$ below 1000 is therefore:
\begin{equation}
m \times T_{\frac{x}{m}} = m \times \sum_{k = 1}^{\frac{x}{m}} k = \frac{x (\frac{x}{m} + 1)}{2}
\end{equation}
Thus the sum of all the multiples of 3 or 5 below 1000 is equal to:
\begin{equation}
3 \times T_{\frac{999}{3}} + 5 \times T_{\frac{995}{5}} - 15 \times T_{\frac{990}{15}} = \frac{999 \times 334 + 995 \times 200 - 990 \times 67}{2}
\end{equation}
\end{document}
我使用pdflatex编译成功:
$ pdflatex Problem1.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014/Arch Linux) (preloaded format=pdflatex)
.
.
.
Output written on Problem1.pdf (1 page, 106212 bytes).
Transcript written on Problem1.log.
它生成了以下输出 PDF 文件以及一堆带有可怕扩展名的其他文件:
如何运行此 PDF 文件以计算解决方案?我知道问题的解决方案,但我想知道如何执行 PDF 文件来计算解决方案。
我之所以喜欢 LaTeX 而不是其他编程语言是因为它支持literate programming,这是Donald Knuth 引入的一种编程方法,TeX 的创建者和有史以来最伟大的计算机科学家之一。
编辑:如果能够在屏幕上或纸上打印计算出的解决方案也将是一件好事。在不打印的情况下计算解决方案对于加热房间很有用,但随着夏季和全球变暖的到来,它已经很热了。此外,打印解决方案会教我如何用 LaTeX 编写一个 hello world 程序。
【问题讨论】:
-
“愚人节”标签...? :)
-
这不是很明显吗?你需要编译成postscript,而不是pdf,然后你可以在你的打印机上运行程序。
-
也许只是复制粘贴到 Wolfram Alpha 中。这就是所有大数学天才的做法。
-
很抱歉对这个“愚人节”问题投票 - 但我认为这有点离题
-
我认为这可以使用内置的 Linux 功能轻松解决。只需执行以下操作: Problem1.tex > /dev/null; /dev/urandom > Problem1-Solution.txt
标签: javascript pdf compilation