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

标签 javascript pdf compilation

我要解决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 文件以及一堆具有可怕扩展名的其他文件:
Solution to Project Euler Problem 1
如何运行此 PDF 文件以计算解决方案?我知道问题的解决方案,但我想知道如何执行 PDF 文件来计算解决方案。
我之所以喜欢 LaTeX 而不是其他编程语言是因为它支持 literate programming , Donald Knuth 介绍的一种编程方法,TeX 的创建者并且是有史以来最伟大的计算机科学家之一。
编辑:能够在屏幕上或纸上打印计算出的解决方案也很好。在不打印的情况下计算解决方案对于加热房间很有用,但随着夏季和全局变暖的开始,它已经很热了。此外,打印解决方案将教我如何在 LaTeX 中编写一个 hello world 程序。

最佳答案

所以,今天似乎是解决这个问题的安全日子......

OP 似乎并不那么精通 PDF。
然而,他显然是一个相当有文化的 LaTeX 人。
这意味着,他也一定非常了解 TeX,因为他是 Donald Knuth 的崇拜者……

预赛就这么多。
现在是真正的肉。

一、报价 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 编程语言...图灵完备,就像 TeX 是, 的创建Donald Knuth ,有史以来最伟大的计算机科学家之一。

另一方面,PostScript 文件 程序,并且可以很容易地被 PostScript 打印机执行(尽管这个执行时间不能可靠地提前确定)。

因此,第二 ,OP应该能够找到一种将他的高级LaTeX代码转换为低级TeX代码的方法。
该代码需要发出 PostScript 程序,而该程序又可以由 PostScript 打印机执行。
一旦给了他应该是他的 TeX 代码结果的 PostScript 代码,对于像 OP 这样的人来说,编写 TeX 代码应该是微不足道的。

我自己对问题解决过程的 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) !)写入标准输出 channel 。 (如果栈顶的项不是 (string) 类型,它将触发 typecheck PostScript 错误。)

因此,将修改后的 PostScript 文件(其中 print 替换了我的 PS 代码的最后一部分)发送到打印机将不起作用(除非打印机支持交互式 executive PostScript 模式——这不是 PostScript 的标准部分语)。但是,如果您将该文件提供给终端中的 Ghostscript 或 cmd.exe,它将起作用。 window 。

关于javascript - 我成功编译了我的程序。现在我该如何运行它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29381899/

相关文章:

javascript最短正则表达式格式化帐号

php - 使用 PHP 在现有 PDF 文件中插入图像文件

java - 清理构建 Java 命令行

javascript - 使用 d3 显示来自 csv 的图像

javascript - 如何在ckeditor中自定义工具栏?

vb.net - 在没有实际 Excel 的情况下使用 Microsoft.Office.Interop.Excel?

c++ - 前向声明库名称

java - 创建不可逆向工程的 Java 程序

javascript - 翻译 Fancybox-Button

objective-c - 在 iOS 中,如何以编程方式填写 pdf 表单字段?