如果我们列出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程序。
发布于 2015-04-08 17:14:48
所以,今天似乎是解决这个问题的一个安全的日子…
OP似乎并不是那么精通PDF。然而,他显然是一个相当识字的LaTeX家伙。这意味着,他一定也非常了解TeX,因为他是Donald Knuth的崇拜者……
准备工作到此为止。现在是真正的肉了。
First, to document:
PDF不是一种编程语言,
文件也不是程序。
*,*(第7.10.1节)
然而,PDF格式的前置者,PostScript,是的编程语言...图灵-完全,就像一样,是有史以来最伟大的计算机科学家之一的创造。
另一方面,PostScript文件是程序,并且可以很容易地由PostScript打印机执行(尽管该执行时间不能可靠地预先确定)。
因此,对于第二个,OP应该能够找到一种方法将高级LaTeX代码转换为低级TeX代码。该代码需要发出一个PostScript程序,该程序可以由PostScript打印机执行。对于像OP这样的人来说,一旦他得到了应该是他的TeX代码的结果的TeX代码,那么编写PostScript代码应该是微不足道的。
我自己对问题解决过程的TeX方面并不是很熟悉。但是,我可以在PostScript方面提供帮助。
OP的TeX代码应该生成的PostScript是这样的(肯定还有更优化的版本--这只是它的第一个、快速的、肮脏的尝试):
%!PS
% define variables
/n1 999 def
/t1 334 def
/n2 995 def
/t2 200 def
/n3 990 def
/s1 67 def
/t3 2 def
% run the computational code
n1 t1 mul
n2 t2 mul
n3 s1 mul
sub
add
t3 div
% print result on printer, not on <stdout>
/Helvetica findfont
24 scalefont
setfont
30 500 moveto
(Result for 'Project Euler Problem No. 1' :) show
/Helvetica-Bold findfont
48 scalefont
setfont
80 400 moveto
( ) cvs show
showpage
将此PostScript代码发送到PostScript打印机,它将计算并打印解决方案。
更新
回答其中一条评论:如果您将以/Helvetica findfont
开头的最后一段PostScript代码替换为一个简单的print
语句,它将不会执行您可能想象的操作。
print
不会导致打印机输出纸张。相反,它要求PostScript解释器在堆栈上写入最上面的项(必须是(string)
!)传输到标准输出通道。(如果堆栈上最顶层的项不是(string)
类型,则会触发typecheck
PostScript错误。)
因此,向打印机发送修改后的PostScript文件(其中print
替换了PS代码的最后一部分)将不起作用(除非该打印机支持交互式executive
PostScript模式--该模式不是PostScript语言的标准部分)。但是,如果您将该文件提供给终端或cmd.exe
窗口中的Ghostscript,它将会正常工作。
发布于 2015-04-01 10:38:24
https://stackoverflow.com/questions/29381899
复制相似问题