Skip to main content
ProductsDesktop toolsTASTECADDrawing reviewTASTEFILESFile managementTASTEFTPFile transferTASTESSHRemote workspaceTASTEZIPCompression and extractionTASTEWINMFile managementDeveloper toolsTASTECODERust integrated development environmentTASTESTUDIOIntegrated development and test environmentTASTEETLData integration (ETL)ServersTASTEREMOTERemote desktop serverTASTEGEOGeospatial serverTASTEMAILMail workspaceInvestingTASTESTOCKStock auto-trading desktop appGet startedDownloadsDocumentationBuyAll products
Marketplace
News
TechNewsToday's dev newsDeveloper docsProgramming languagesRustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWeb front-end frameworksNext.jsReactVue.js · NuxtSvelteKitAngularServer frameworksSpring BootDjangoFastAPIASP.NET CoreNestJS · ExpressApp & desktop frameworksTauriFlutterReact NativeElectronAlgorithmsAlgorithm basicsCore algorithmsAdvanced algorithmsPractical algorithms
CommunityProjectsDevelopment logsPromptsAI tool issuesShowcaseCommunity guide
Work with usTech transferWebsite buildDesktop app buildRelease and payment pipelineSales partnerContact
ServicesRemote desktopCustom installer packages
TASTEDEV
Products

Desktop tools

TASTECADComplex drawings, opened with easeTASTEFILESJust the files you need, as fast as possibleTASTEFTPThe tidiest path for your filesTASTESSHA stronger link between you and your serversTASTEZIPPack faster, unpack saferTASTEWINMA keyboard-first file manager with two panes

Developer tools

TASTECODEEdit, build, debug and test Rust apps in one windowTASTESTUDIOFrom editing to remote tests and approved AI fixesTASTEETLMove data while you watch the values

Servers

TASTEREMOTEA remote desktop you run on your own serverTASTEGEORebuilding a GeoServer-compatible geospatial server in RustTASTEMAILTake back control of your email

Investing

TASTESTOCKStock auto-trading that follows the rules you set: when, and what

Get started

DownloadsLatest builds of every productDocsHow to use each productBuySubscriptions and perpetual licencesAll productsEvery product at a glance
All products
MarketplaceNews
Tech

News

Today's dev newsOfficial announcements on languages, frameworks, GitHub and AIDeveloper docsGuides to languages, frameworks and algorithms

Programming languages

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

Frameworks

Web front-end frameworksNext.jsReactVue.js · NuxtSvelteKitAngular
Server frameworksSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
App & desktop frameworksTauriFlutterReact NativeElectron

Algorithms

Algorithm basicsComplexity analysis · Sorting · Searching · Basic data structures · Trees and heapsCore algorithmsGraph traversal · Shortest paths & minimum spanning trees · Dynamic programming · Greedy algorithms · Divide and conquer · BacktrackingAdvanced algorithmsString algorithms · Math and number theory · Bit manipulation · Segment trees · Union-findPractical algorithmsCoding interview strategy · LRU cache · Rate limiting · Consistent hashing
All development docs
Community
ProjectsDiscover builders and their workDevelopment logsDecisions and lessons learnedPromptsReusable instructionsAI tool issuesProblems and workarounds for Claude, Codex, GeminiShowcaseFinished resultsCommunity guideHow we build in the open
Work with us
Tech transferNative apps in Rust and eguiWebsite build10 languages, accounts, docs and downloadsDesktop app buildRust and egui, no WebViewRelease and payment pipelineSigning, MSI, deb, rpm, AppImage, hashesSales partnerRecommend us and earn 30%ContactBuilds, partnerships and investment
Services
Remote desktopReach your computer from a browserCustom installer packagesOnly the features you need, settings preloaded
Buy
EN
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
Log in
Log in
TASTEDEV

From small tools to better development.

support@tastedev.netSupport TASTEDEV
ProductsProduct lineupBuyDocsDownloadsMarketplaceSupport
CommunityProjectsDevelopment logsPromptsAI tool issuesShowcase
PoliciesAbout usPrivacy policyTerms of serviceSubscription termsRefund policyCommunity guidelines
AccountLog inSign up
Visitors
Today
Yesterday
Total views
Company
GXSOFT
Representative
CHOI YOUNG GU
Business registration number
853-44-00368
Address
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
Phone
+82-10-4404-1523
Email
whitecalvin@tastedev.net
Privacy officer
CHOI YOUNG GU

© 2026 TASTEDEVProduct information is based on verified development sources.Released · improving

Math and number theory docs
Math and number theory
ConceptsHow it worksImplementationComplexityPractice problemsReal-world use
Discussions

Math and number theory guide · 3/6

Implementation

This chapter builds the sieve, extended Euclid, the modular inverse and fast exponentiation as one Python module, explains it line by line, then ports the sieve to C++, Java and TypeScript.

Python implementation

def sieve(n: int) -> list[int]:
    """Return all primes <= n in ascending order."""
    if n < 2:
        return []
    is_prime = bytearray([1]) * (n + 1)
    is_prime[0] = is_prime[1] = 0
    i = 2
    while i * i <= n:
        if is_prime[i]:
            count = (n - i * i) // i + 1
            is_prime[i * i :: i] = bytes(count)
        i += 1
    return [k for k in range(n + 1) if is_prime[k]]


def extended_gcd(a: int, b: int) -> tuple[int, int, int]:
    """Return (g, x, y) with a * x + b * y == g == gcd(a, b)."""
    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


def mod_inverse(a: int, m: int) -> int:
    """Return x in [0, m) with a * x % m == 1; raise ValueError if none exists."""
    g, x, _ = extended_gcd(a % m, m)
    if g != 1:
        raise ValueError(f"{a} has no inverse modulo {m}")
    return x % m


def mod_pow(base: int, exp: int, mod: int) -> int:
    """Compute base ** exp % mod with O(log exp) multiplications."""
    result = 1 % mod
    base %= mod
    while exp > 0:
        if exp & 1:
            result = result * base % mod
        base = base * base % mod
        exp >>= 1
    return result


if __name__ == "__main__":
    print(sieve(30))                  # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
    print(extended_gcd(240, 46))      # (2, -9, 47)
    print(mod_inverse(3, 7))          # 5
    print(mod_pow(3, 13, 7))          # 3
    print(mod_pow(2, 10**18, 10**9 + 7) == pow(2, 10**18, 10**9 + 7))  # True

Line by line

sieve

  • bytearray([1]) * (n + 1) uses one byte per slot; a [True] * (n + 1) list stores an 8-byte pointer per slot.
  • 0 and 1 are not prime, so they are cleared first.
  • The outer loop stops once i * i > n: every composite has a prime factor no larger than its square root, so the rest are already crossed out.
  • is_prime[i * i :: i] = bytes(count) zeroes every i-th slot from i × i in one slice assignment, much faster than a Python loop. count must equal the slice length.
  • The final line collects the indices still marked 1.

extended_gcd

  • (old_r, r) are the two remainders; (old_x, x) and (old_y, y) write each remainder as a × x + b × y, starting from a = a × 1 + b × 0 and b = a × 0 + b × 1.
  • Each iteration updates all three pairs with new = previous - q × current; tuple assignment avoids temporaries.
  • Being iterative, it never hits Python's recursion limit.

mod_inverse

  • a % m first handles negative a.
  • If g != 1 there is no inverse, so it raises instead of returning a wrong value.
  • x % m normalizes a negative x into [0, m).

mod_pow

  • result = 1 % mod makes the answer 0 when mod == 1.
  • exp & 1 inspects the lowest bit; if it is 1, the current base is multiplied into the result.
  • Squaring base and shifting exp right by one moves on to the next bit.
  • Python integers never overflow, but % mod on every step keeps them small. In production, the built-in pow(base, exp, mod) is fastest.

The sieve in C++

vector<bool> is bit-packed, and long long keeps i * i from overflowing int.

#include <iostream>
#include <vector>

std::vector<int> sieve(int n) {
    std::vector<int> primes;
    if (n < 2) return primes;
    std::vector<bool> is_prime(n + 1, true);
    is_prime[0] = is_prime[1] = false;
    for (long long i = 2; i * i <= n; ++i) {
        if (!is_prime[i]) continue;
        for (long long j = i * i; j <= n; j += i) is_prime[j] = false;
    }
    for (int k = 2; k <= n; ++k)
        if (is_prime[k]) primes.push_back(k);
    return primes;
}

int main() {
    for (int p : sieve(30)) std::cout << p << ' ';
    std::cout << '\n';  // 2 3 5 7 11 13 17 19 23 29
}

The sieve in Java

A boolean[] starts all false, so storing "is composite" skips initialization; long avoids overflow in i * i.

import java.util.ArrayList;
import java.util.List;

public class Sieve {
    static List<Integer> sieve(int n) {
        List<Integer> primes = new ArrayList<>();
        if (n < 2) return primes;
        boolean[] composite = new boolean[n + 1];
        for (long i = 2; i * i <= n; i++) {
            if (composite[(int) i]) continue;
            for (long j = i * i; j <= n; j += i) composite[(int) j] = true;
        }
        for (int k = 2; k <= n; k++)
            if (!composite[k]) primes.add(k);
        return primes;
    }

    public static void main(String[] args) {
        System.out.println(sieve(30)); // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
    }
}

The sieve in TypeScript

A Uint8Array uses one byte per slot, and number is exact up to 2^53, so the index arithmetic is safe.

export function sieve(n: number): number[] {
  if (n < 2) return [];
  const isPrime = new Uint8Array(n + 1).fill(1);
  isPrime[0] = 0;
  isPrime[1] = 0;
  for (let i = 2; i * i <= n; i++) {
    if (!isPrime[i]) continue;
    for (let j = i * i; j <= n; j += i) isPrime[j] = 0;
  }
  const primes: number[] = [];
  for (let k = 2; k <= n; k++) if (isPrime[k]) primes.push(k);
  return primes;
}

console.log(sieve(30).join(" ")); // 2 3 5 7 11 13 17 19 23 29

Summary

All four sieves share the same shape: clear 0 and 1, cross out multiples from i × i for every prime i with i × i ≤ n, and collect what is left. They differ only in memory representation (bytearray, vector<bool>, boolean[], Uint8Array) and in how they keep i × i from overflowing. Extended Euclid and fast exponentiation need care with multiplication overflow in C++ and Java; see the real-world chapter.

Questions about this chapter?
Previous chapterHow it worksNext chapterComplexity

0 comments

Top commentsNewest first

Sign in · Sign in to leave a comment.