跳至主要內容
產品桌面工具TASTECAD圖面檢視TASTEFILES檔案管理TASTEFTP檔案傳輸TASTESSH遠端工作空間TASTEZIP壓縮與解壓縮TASTEWINM檔案管理開發工具TASTECODERust 整合開發環境TASTESTUDIO整合開發與測試環境TASTEETL資料整合(ETL)伺服器TASTEREMOTE遠端桌面伺服器TASTEGEO空間資訊伺服器TASTEMAIL郵件工作空間投資TASTESTOCK股票自動交易桌面應用程式開始使用下載文件購買所有產品
市集
資訊
技術資訊今日開發資訊開發文件程式語言RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWeb 前端框架Next.jsReactVue.js · NuxtSvelteKitAngular伺服器框架Spring BootDjangoFastAPIASP.NET CoreNestJS · Express應用程式與桌面框架TauriFlutterReact NativeElectron演算法演算法基礎核心演算法進階演算法實務演算法
社群專案開發日誌提示詞AI 工具問題作品展示社群指南
合作技術傳授網站建置桌面應用程式建置發布與付款流程銷售合作夥伴聯絡我們
服務遠端桌面安裝包客製
TASTEDEV
產品

桌面工具

TASTECAD再複雜的圖面,也能輕鬆開啟TASTEFILES只挑需要的檔案,最快送達TASTEFTP讓檔案往來,走最俐落的路TASTESSH在你與伺服器之間,築起更穩固的連線TASTEZIP壓得更快,解得更安心TASTEWINM以鍵盤為主、用兩個窗格整理檔案的檔案管理器

開發工具

TASTECODE在一個視窗中完成 Rust 應用程式的編輯、建置、偵錯與測試TASTESTUDIO從編輯到遠端測試,再到經核准的 AI 修正TASTEETL邊看數值,邊搬資料

伺服器

TASTEREMOTE在自己的伺服器上運行的遠端桌面TASTEGEO以 Rust 重新打造相容 GeoServer 的空間資訊伺服器TASTEMAIL把郵件的主導權拿回自己手上

投資

TASTESTOCK何時、做什麼——依你訂下的規則運作的股票自動交易

開始使用

下載所有產品的最新版本文件各產品使用方法購買訂閱與永久授權所有產品一覽全部產品
所有產品
市集資訊
技術

資訊

今日開發資訊語言、框架、GitHub 與 AI 官方發布開發文件語言、框架與演算法指南

程式語言

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

框架

Web 前端框架Next.jsReactVue.js · NuxtSvelteKitAngular
伺服器框架Spring BootDjangoFastAPIASP.NET CoreNestJS · Express
應用程式與桌面框架TauriFlutterReact NativeElectron

演算法

演算法基礎複雜度分析 · 排序 · 搜尋 · 基礎資料結構 · 樹與堆積核心演算法圖形走訪 · 最短路徑與最小生成樹 · 動態規劃 · 貪婪演算法 · 分治法 · 回溯法進階演算法字串演算法 · 數學與數論 · 位元運算 · 線段樹 · 併查集實務演算法程式面試解題策略 · LRU 快取 · 速率限制 · 一致性雜湊
查看全部開發文件
社群
專案探索開發者與作品開發日誌決策與試錯提示詞可重複使用的指令AI 工具問題Claude、Codex、Gemini 的問題與替代做法作品展示已完成的成果社群指南我們如何公開打造
合作
技術傳授用 Rust 與 egui 打造原生應用程式網站建置10 種語言、帳號、文件與下載桌面應用程式建置Rust 與 egui,不用 WebView發布與付款流程簽章、MSI、deb、rpm、AppImage、雜湊銷售合作夥伴推薦即得 30%聯絡我們開發、合作與投資諮詢
服務
遠端桌面用瀏覽器連線你的電腦安裝包客製只裝需要的,設定預先放好
購買
繁
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
登入
登入
TASTEDEV

從小工具到更好的開發。

support@tastedev.net支持 TASTEDEV
產品產品系列購買文件下載市集客戶支援
社群專案開發日誌提示詞AI 工具問題作品展示
政策關於我們隱私權政策服務條款訂閱條款退款政策社群規範
帳戶登入註冊
訪客
今天
昨天
累計瀏覽
公司名稱
GXSOFT
負責人
CHOI YOUNG GU
營業登記號碼
853-44-00368
地址
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
電話
+82-10-4404-1523
電子郵件
whitecalvin@tastedev.net
個人資料保護負責人
CHOI YOUNG GU

© 2026 TASTEDEV產品資訊以經驗證的開發原始碼為準。已發布·持續改進

數學與數論 文件
數學與數論
ConceptsHow it worksImplementationComplexityPractice problemsReal-world use
討論

數學與數論 指南 · 2/6

How it works

本章目前僅提供英文版。

This chapter traces each algorithm on small numbers. Following the tables by hand is the fastest way to see why the code looks the way it does.

Euclidean algorithm: gcd(252, 105)

Let r be the remainder of a divided by b. Because r = a - q × b, every common divisor of a and b also divides r, and vice versa. So replacing (a, b) with (b, r) keeps the GCD unchanged while the numbers keep shrinking.

Stepaba mod b
125210542
21054221
342210

The remainder hits 0 at step 3, so the answer is the b of that row: 21. Three steps in total.

def gcd(a: int, b: int) -> int:
    while b:
        a, b = b, a % b
    return a

print(gcd(252, 105))  # 21

Extended Euclid: 240x + 46y = gcd(240, 46)

The extended version tracks, next to each remainder r, the coefficients x and y that express it as 240 × x + 46 × y. Every time a quotient q is produced, all three values follow the same rule: new = previous - q × current.

qrxy
-24010
-4601
5101-5
46-421
145-26
12-947
2023-120

The answer is the row just before the remainder becomes 0: gcd = 2, x = -9, y = 47, and indeed 240 × (-9) + 46 × 47 = -2160 + 2162 = 2. Check that 240 × x + 46 × y = r holds on every row. That invariant is exactly why the algorithm is correct.

def extended_gcd(a: int, b: int) -> tuple[int, int, int]:
    old_r, r = a, b
    old_x, x = 1, 0
    old_y, y = 0, 1
    while r:
        q = old_r // r
        old_r, r = r, old_r - q * r
        old_x, x = x, old_x - q * x
        old_y, y = y, old_y - q * y
    return old_r, old_x, old_y

print(extended_gcd(240, 46))  # (2, -9, 47)
對本章有疑問嗎?
上一章Concepts下一章Implementation

Fast exponentiation: 3^13 mod 7

Instead of twelve multiplications, look at the exponent in binary. 13 = 1101 (base 2) = 8 + 4 + 1, so 3^13 = 3^8 × 3^4 × 3^1. Keep squaring the base (3, 3^2, 3^4, 3^8, …) and multiply it into the result only where the exponent has a 1 bit.

exp (binary)lowest bitresultbase (for next step)
110111 × 3 = 33^2 = 9 ≡ 2
110032^2 = 4
1113 × 4 = 12 ≡ 54^2 = 16 ≡ 2
115 × 2 = 10 ≡ 32^2 = 4

The answer is 3. Fermat's little theorem confirms it: 3^6 ≡ 1 (mod 7), so 3^13 = (3^6)^2 × 3 ≡ 3. The number of multiplications is proportional to the number of bits in the exponent, so even an exponent of 10^18 needs only about 60 iterations.

Sieve of Eratosthenes: primes up to 30

Walk upward from 2. Whenever you meet a number that has not been crossed out, it is prime, and you cross out all of its multiples. It is enough to start at i × i: any smaller multiple i × k with k < i was already crossed out while processing the smaller factor k.

inewly crossed out
24, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30
39, 15, 21, 27 (12, 18, 24 and 30 are already gone)
4already crossed out, skip
525 (30 is already gone)
66 × 6 = 36 exceeds 30, stop

What remains is 2, 3, 5, 7, 11, 13, 17, 19, 23, 29: ten primes. The outer loop only runs while i × i ≤ n, so for 30 the work is effectively done at i = 5.

Factorization and nCr mod p

The simplest factorization method is trial division up to √n. For 360, divide by 2 three times (360, 180, 90, 45), then by 3 twice (45, 15, 5). The leftover 5 has no divisor up to √5, so it is prime, and 360 = 2^3 × 3^2 × 5.

A binomial coefficient C(n, r) = n! / (r! × (n - r)!) modulo a prime p cannot be computed by plain division. Instead you multiply by the inverse of the denominator. Inverting with Fermat's little theorem gives C(n, r) ≡ n! × (r!)^(p-2) × ((n-r)!)^(p-2) (mod p) when n < p.

p = 10**9 + 7

def ncr_mod(n: int, r: int) -> int:
    if r < 0 or r > n:
        return 0
    num = den = 1
    for i in range(r):
        num = num * (n - i) % p
        den = den * (i + 1) % p
    return num * pow(den, p - 2, p) % p

print(ncr_mod(10, 3))          # 120
print(ncr_mod(100000, 50000))  # 149033233 — the exact value has over 30,000 digits

Summary

The Euclidean algorithm shrinks big numbers by replacing them with remainders, and the extended version carries the Bezout coefficients along. Fast exponentiation uses the binary form of the exponent to cut the number of multiplications to a logarithm. The sieve crosses out multiples starting at i × i to find every prime in a range at once, and nCr mod p multiplies by inverses instead of dividing. The next chapter turns all of this into reusable code.

0 則留言

熱門留言由新到舊

登入 · 登入後即可留言。