首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我成功地编译了我的程序。现在我该如何运行它?

我成功地编译了我的程序。现在我该如何运行它?
EN

Stack Overflow用户
提问于 2015-04-01 10:32:40
回答 2查看 556关注 0票数 19

我想解决Project Euler Problem 1

如果我们列出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程序。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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,它将会正常工作。

票数 5
EN

Stack Overflow用户

发布于 2015-04-01 10:38:24

你不能运行pdf文件。您需要使用latex命令而不是pdflatex。例如:

latex Problem1.tex

Here's some documentation

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29381899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档