单独的Python/Sage脚本

下面的Sage脚本对整数或多项式进行因式分解:

  1. #!/usr/bin/env sage
  2. import sys
  3. from sage.all import *
  4. if len(sys.argv) != 2:
  5. print("Usage: %s <n>" % sys.argv[0])
  6. print("Outputs the prime factorization of n.")
  7. sys.exit(1)
  8. print(factor(sage_eval(sys.argv[1])))

要使用这个脚本,你的SAGE_ROOT必须在在环境变量中。 如果上面的脚本叫 factor, 下面是用法示例:

  1. $ ./factor 2006
  2. 2 * 17 * 59