algorithms-ch1-Algorithms with numbers

1.1Basic arithmetic

1.1.1addition

-Given two binary numbers x and y, how long does our algorithm take to add them?
-We want the answer expressed as a function of the size of the input: the number of bits of x and y, the number of keystrokes needed to type them in.

Suppose x and y are each n bits long; O(n).

1.1.2multiplication

二進制乘法的兩個算法:


0.0

If x and y are both n bits, then there are n intermediate rows, with lengths of up to 2n bits (taking the shifting into account). The total time taken to add up these rows, doing two numbers at a time, is O(n) + O(n) + · · · + O(n)..(n-1 times): O(n^2)


@.@
function multiply(x, y)
Input: Two n-bit integers x and y, where y ≥ 0
Output: Their product
/
if y=0: return0
z = multiply(x, ?y/2?)
//每次遞歸調(diào)用钦购,接收到返回值之后 向遞歸下一步執(zhí)行
if y is even:
  return 2z
else:
  return x + 2z
function divide(x,y)
Input: Two n-bit integers x and y, where y ≥ 1
Output: The quotient and remainder of x divided by y
/
if x = 0: return (q,r) = (0,0)
(q, r) = divide(?x/2?, y)
q=2·q, r=2·r
if x is odd: r=r+1
if r≥y: r=r?y, q=q+1
return (q,r)
1.2mod
  1. if x = qN + r with 0 ≤ r < N, then x modulo N is equal to r.

  2. x and y are congruent modulo N if they differ by a multiple of N , or in symbols:

x≡y (modN) ?? N divides (x?y).

  1. Substitution rule
    If x ≡ x′ (mod N) and y ≡ y′ (mod N), then:x+y≡x′+y′ (modN) and xy≡x′y′ (modN).

  2. Modular addition and multiplication:

  • addition: O(n),
    n = ?log N ? is the size of N ;(regard N as a binary number, n is the bits of this number, each bits need one operations)
    To add two numbers x and y modulo N, Since x and y are eachin the range 0 to N ?1, their sum is between 0 and 2(N ?1), The overall computation therefore consists of an addition, and possibly a subtraction

  • multiplication: O(n^2)
    using our quadratic-time division algorithm.Multiplication thus remains a quadratic operation.

  • Division: O(n^3)

  1. Modular exponentiation
    -Problem: compute x^y mod N for values of x, y, and N that are several hundred bits long
    -Sol1: x mod N →x^2 mod N →x^3 mod N →···→x^y mod N,
    -Sol2: x mod N →x^2 mod N →x^4 mod N →x8^ mod N →···→x2^?logy? mod N.
    a polynomial time algorithm:
function modexp(x, y, N)
Input: Two n-bit integers x and N, an integer exponent y
Output: x^y mod N
/
if y=0: return1
z = modexp(x, ?y/2?, N )
if y is even:
  return z^2 mod N
else:
  return x · z^2 mod N
  1. Euclid's Alg for Great Common Divisor

Euclid’s rule If x and y are positive integers with x ≥ y, then gcd(x, y) = gcd(x mod y, y).

Lemma If a ≥ b,then a mod b < a/2.

function Euclid(a,b)
Input: Two integers a and b with a≥b≥0
Output: gcd(a, b)
/
if b=0: return a
return Euclid(b, a mod b)

both arguments, a and b, If they are initially n-bit integers, then the base case will be reached within 2n recursive calls. And since each call involves a quadratic-time division, the total time is O(n3).

Lemma if d divides both a and b, and d = ax + by for some integers x and y(may be negative) , then necessarily d = gcd(a,b)

function extended-euclid(a,b)
Input: Two positive integers a and b with a ≥ b ≥ 0
Output: Integers x,y, d, such that d=gcd(a,b) and ax+by=d
/
if b = 0: return (1,0,a)
(x′, y′, d) = Extended-Euclid(b, a mod b)
return (y′, x′ ? ?a/b?y′, d)

模除法:gcd(a,N) = 1(即互質(zhì)) <==> 存在x,使得ax ≡ 1 (mod N) (可用反證法證明)
左推右:用extend-euclid algorithm可以得到x,y
右推左:如果ax+Ny=d(gcd<=d), 且d整除a, N(d <=gcd)幅疼,那么d==gcd(a, N)

  1. x is the multiplicative inverse of a modulo N if ax ≡ 1 (mod N).

Modular division theorem For any a mod N, a has a multiplicative inverse modulo N if and only if it is relatively prime to N. When this inverse exists, it can be found in time O(n3)(where as usual n denotes the number of bits of N ) by running the extended Euclid algorithm.

1.3prime
function primality(N)
Input: Positive integer N
Output: yes/no
/
Pick a positive integer a < N at random 
if a^(N?1) ≡ 1 (mod N):
  return yes
else:
  return no

exercise都是clrs上的不附了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末聪轿,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌备禀,老刑警劉巖壁公,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件感论,死亡現(xiàn)場離奇詭異,居然都是意外死亡紊册,警方通過查閱死者的電腦和手機比肄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來囊陡,“玉大人芳绩,你說我怎么就攤上這事∽卜矗” “怎么了妥色?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長遏片。 經(jīng)常有香客問我垛膝,道長,這世上最難降的妖魔是什么丁稀? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任吼拥,我火速辦了婚禮,結(jié)果婚禮上线衫,老公的妹妹穿的比我還像新娘凿可。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布枯跑。 她就那樣靜靜地躺著惨驶,像睡著了一般。 火紅的嫁衣襯著肌膚如雪敛助。 梳的紋絲不亂的頭發(fā)上粗卜,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機與錄音纳击,去河邊找鬼续扔。 笑死,一個胖子當著我的面吹牛焕数,可吹牛的內(nèi)容都是我干的纱昧。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼堡赔,長吁一口氣:“原來是場噩夢啊……” “哼识脆!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起善已,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤灼捂,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后换团,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體纵东,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年啥寇,在試婚紗的時候發(fā)現(xiàn)自己被綠了偎球。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡辑甜,死狀恐怖衰絮,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情磷醋,我是刑警寧澤猫牡,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站邓线,受9級特大地震影響淌友,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜骇陈,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一震庭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧你雌,春花似錦器联、人聲如沸二汛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽肴颊。三九已至,卻和暖如春渣磷,著一層夾襖步出監(jiān)牢的瞬間婿着,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工醋界, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留竟宋,地道東北人。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓物独,卻偏偏與公主長得像袜硫,于是被迫代替她去往敵國和親氯葬。 傳聞我的和親對象是個殘疾皇子挡篓,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容