Notes
Search
K
Comment on page

Sage

GCD

#!/usr/bin/env sage
import sys
from sage.all import *
print(gcd(a,b))

Extended GCD

#!/usr/bin/env sage
import sys
from sage.all import *
print(xgcd(a,b)) # returns d,u,v

Solve Congruence

#!/usr/bin/env sage
import sys
from sage.all import *
var('x') # < symbolic variable, solve for that one
print(solve_mod(a == x, b))

Solve Power Modulo

#!/usr/bin/env sage
import sys
from sage.all import *
print(power_mod(a,b,c))

Multiplicative Inverse

#!/usr/bin/env sage
import sys
from sage.all import *
print(inverse_mod(a,b))

Factor Prime

#!/usr/bin/env sage
import sys
import hashlib
from sage.all import *
print(factor(a))
Last modified 9mo ago