本文へスキップ
製品デスクトップツール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より速くまとめ、より安全にひらくTASTEWINM2つの画面で整理する、キーボード中心のファイルマネージャー

開発ツール

TASTECODERust アプリの編集・ビルド・デバッグ・テストまでひとつのウィンドウでTASTESTUDIO編集からリモートテスト、承認した AI 修正までTASTEETL値を見ながら、データを運ぶ

サーバー

TASTEREMOTE自社サーバーで運用するリモートデスクトップTASTEGEOGeoServer 互換の空間情報サーバーを Rust で作り直しますTASTEMAILメールの主導権を、もう一度自分の手に

投資

TASTESTOCKいつ・何を。自分で決めたルールどおりに動く株式自動売買

はじめる

ダウンロード全製品の最新版ドキュメント各製品の使い方購入サブスクリプション・永久ライセンスすべての製品全製品を一覧で
すべての製品
マーケットプレイスニュース
技術

ニュース

今日の開発ニュース言語・フレームワーク・GitHub・AI の公式発表開発ドキュメント言語・フレームワーク・アルゴリズムのガイド

プログラミング言語

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

フレームワーク

Webフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngular
サーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
アプリ・デスクトップ フレームワークTauriFlutterReact NativeElectron

アルゴリズム

アルゴリズムの基礎計算量解析 · ソート · 探索 · 基本データ構造 · 木構造とヒープ主要アルゴリズムグラフ探索 · 最短経路と最小全域木 · 動的計画法 · 貪欲法 · 分割統治法 · バックトラッキング発展アルゴリズム文字列アルゴリズム · 数学と整数論 · ビット演算 · セグメント木 · Union-Find実務のアルゴリズムコーディング面接対策 · LRUキャッシュ · レート制限 · コンシステントハッシュ法
開発ドキュメントをすべて見る
コミュニティ
プロジェクトビルダーと作品を探す開発ログ決定と試行錯誤プロンプト再利用できる指示AIツールの問題Claude・Codex・Gemini の問題と回避策ショーケース完成した成果コミュニティガイドオープンにつくる方法
協業
技術移転Rust・egui でネイティブアプリを作るウェブサイト構築10言語、アカウント、ドキュメントとダウンロードデスクトップアプリ構築Rust・egui、WebView なしリリース・決済パイプライン署名・MSI・deb・rpm・AppImage・ハッシュ販売パートナー紹介して決済額の30%お問い合わせ構築・提携・投資のご相談
サービス
リモートデスクトップブラウザから自分のパソコンへインストールパッケージのカスタマイズ必要な機能だけ、設定は事前に
購入
JA
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
ログイン
ログイン
TASTEDEV

小さなツールから、より良い開発へ。

support@tastedev.netTASTEDEVを支援
製品製品ラインナップ購入ドキュメントダウンロードマーケットプレイスサポート
コミュニティプロジェクト開発ログプロンプト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件

人気のコメント順新しい順

ログイン · ログインするとコメントできます。