我成功地编译了我的程序。现在我怎么运行呢?
I successfully compiled my program. Now how do I run it?
我要解决Project Euler Problem 1:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
这是我的代码:
\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, an approach to programming introduced by Donald Knuth, the creator of TeX 并且是有史以来最伟大的计算机科学家之一。
编辑: 如果能够将计算出的解决方案打印在屏幕上或纸上也很好。在不打印的情况下计算解决方案对于加热房间很有用,但随着夏季和全球变暖的到来,它已经非常热了。此外,打印解决方案可以教会我如何用 LaTeX 编写 hello world 程序。
您不能运行 pdf 文件。您需要使用 latex
命令而不是 pdflatex
。例如
latex Problem1.tex
所以,今天似乎是解决这个问题的安全日子...
OP 似乎并不那么精通 PDF。
然而,他显然是一个相当有文化的 LaTeX 人。
这意味着,鉴于他是 Donald Knuth 的崇拜者,他也一定非常了解 TeX...
预赛到此为止。
现在是真正的肉。
首先,引用the official PDF-1.7 specification文档:
PDF is not a programming language, and a PDF file is not a program.
(p. 92, Section 7.10.1)
不过,PDF格式的前身PostScript,是一个Turing-complete programming language... Turing-complete, just as TeX is, the creation of Donald Knuth ,有史以来最伟大的计算机科学家之一。
PostScript 文件,另一方面,是 程序,并且可以很容易地由 PostScript 打印机执行(虽然这个执行时间不能可靠地提前确定)。
因此,second,OP 应该能够找到一种方法将他的高级 LaTeX 代码转换为低级水平的 TeX 代码。
该代码需要发出一个 PostScript 程序,该程序又可以由 PostScript 打印机执行。
编写 TeX 代码对于像 OP 这样的人来说应该是微不足道的,一旦他得到了应该是他的 TeX 代码结果的 PostScript 代码。
我本人不太精通该问题解决过程的 TeX 方面。
但是,我可以帮助处理 PostScript。
OP 的 TeX 代码应该生成的 PostScript 是这样的(肯定有更优化的版本可能——这只是第一个,快速的'n'dirty shot):
%!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 语言的标准部分)。但是,如果您将该文件提供给终端中的 Ghostscript 或 cmd.exe
window.
,它将起作用
我要解决Project Euler Problem 1:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
这是我的代码:
\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, an approach to programming introduced by Donald Knuth, the creator of TeX 并且是有史以来最伟大的计算机科学家之一。
编辑: 如果能够将计算出的解决方案打印在屏幕上或纸上也很好。在不打印的情况下计算解决方案对于加热房间很有用,但随着夏季和全球变暖的到来,它已经非常热了。此外,打印解决方案可以教会我如何用 LaTeX 编写 hello world 程序。
您不能运行 pdf 文件。您需要使用 latex
命令而不是 pdflatex
。例如
latex Problem1.tex
所以,今天似乎是解决这个问题的安全日子...
OP 似乎并不那么精通 PDF。 然而,他显然是一个相当有文化的 LaTeX 人。 这意味着,鉴于他是 Donald Knuth 的崇拜者,他也一定非常了解 TeX...
预赛到此为止。 现在是真正的肉。
首先,引用the official PDF-1.7 specification文档:
PDF is not a programming language, and a PDF file is not a program.
(p. 92, Section 7.10.1)
不过,PDF格式的前身PostScript,是一个Turing-complete programming language... Turing-complete, just as TeX is, the creation of Donald Knuth ,有史以来最伟大的计算机科学家之一。
PostScript 文件,另一方面,是 程序,并且可以很容易地由 PostScript 打印机执行(虽然这个执行时间不能可靠地提前确定)。
因此,second,OP 应该能够找到一种方法将他的高级 LaTeX 代码转换为低级水平的 TeX 代码。 该代码需要发出一个 PostScript 程序,该程序又可以由 PostScript 打印机执行。 编写 TeX 代码对于像 OP 这样的人来说应该是微不足道的,一旦他得到了应该是他的 TeX 代码结果的 PostScript 代码。
我本人不太精通该问题解决过程的 TeX 方面。 但是,我可以帮助处理 PostScript。
OP 的 TeX 代码应该生成的 PostScript 是这样的(肯定有更优化的版本可能——这只是第一个,快速的'n'dirty shot):
%!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 语言的标准部分)。但是,如果您将该文件提供给终端中的 Ghostscript 或 cmd.exe
window.