Skip to Main Content
Mathematica 錦囊妙計
book

Mathematica 錦囊妙計

by Sal Mangano
April 2014
Beginner to intermediate content levelBeginner to intermediate
830 pages
15h 21m
Chinese
GoTop Information, Inc.
Content preview from Mathematica 錦囊妙計
50
|
第二章
討論說明
不識此道的人可能會覺得
somePrimes
最前面的定義是最好理解的版本。首先,我必須說
沒有哪一種程式風格是「最好的」,所以不必特別屈就某種方式,精練的程式碼效率未
必佳。接下來我要為這種程式設計方式辯解,再一次強調我認為最恰當的版本。
首先,
@
算符的使用不該成為障礙,畢竟算符是很常見的,每個國小一年級的學生都須
學會 1 + 1 或是 $15 中算符的意義,算符並不神祕,只要你好好了解它。[Page-050]
然而,可讀性才是重點,此程式直接了當地表示出程式目的:「建立
{2^n-1, 2^n+1}
列,其中
n
0
nmax
,接著將陣列展平為串列,消除重複值後,再將質數項挑出」【譯
註:原文為“select the primes of the union of the flattening of the table of pairs
{2^n-1,
2^n+1}
with
n
ranging from
0
to
nmax
",原文適當地表示出程式碼與問題描述之貼近程
度,譯文恐失原味】,如同前文所述,本程式最關鍵的動作是挑選質數,Mathematica
許你以不同表達方式強調表示式主要功能,這是相當方便的。
相反地,你也可以把函數置放於程式碼的結尾,降低其重要性,這也是後置式
//
的其
中一個特色,通常這會用於排版格式或運算計時等場合。舉例而言,下列程式碼的重點
在於取出
somePrimes[500]
的最後元素,順便輸出計算時間,計時指令置於結尾以便於
隨時刪除。
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ubuntu for Non-Geeks, 4th Edition

Ubuntu for Non-Geeks, 4th Edition

Rickford Grant, Phil Bull

Publisher Resources

ISBN: 9789863470106