# Loop over integers mod p [closed]

What is the most efficient way to factor a polynomial mod $p$ for various $p$ all at once, perhaps to be assembled into a table with more fancy code?

For example, suppose I want to factor $x^2-17 mod p$ for the first 100 primes. My first guess is to type out the proper code to factor $x^2-17$ over the ring Integers(i) where i runs through an appropriate prime_range. But Sage says I am not allowed to put a variable into Integers(). So what is the best way to do this?

edit retag reopen merge delete

### Closed for the following reason the question is answered, right answer was accepted by Supersingularity close date 2016-04-05 13:19:04.881841

Please provide the code you are trying and the error you are getting.

Sort by » oldest newest most voted

It would be interesting to know what code you tried and what error you got.

This would allow to explain precisely what failed in what you tried and why.

The following works for me.

sage: for p in primes_first_n(100):
....:     x = polygen(GF(p))
....:     print("%4d %s" %(p, (x^2 - 17).factor()))
....:


This runs through the first 100 primes, and for each of them declares x as the generator for a polynomial ring over the finite field with p elements, and prints out the factorization of the polynomial x^2 - 17 in that ring.

Note that Zmod(p) or Integers(p) creates the ring of integers modulo p, considered with its ring structure, while GF(p) (for Galois Field) creates it as a field.

more
{"ft_lang_label":"__label__en","ft_lang_prob":0.8877492,"math_prob":0.89167815,"size":574,"snap":"2020-45-2020-50","text_gpt3_token_len":141,"char_repetition_ratio":0.10877193,"word_repetition_ratio":0.0,"special_character_ratio":0.2508711,"punctuation_ratio":0.06557377,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9798984,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-25T20:21:28Z\",\"WARC-Record-ID\":\"<urn:uuid:415e55b8-63d4-49a2-b990-4caeb3b77076>\",\"Content-Length\":\"55357\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e01da1b9-ae7e-4819-998e-417786685c91>\",\"WARC-Concurrent-To\":\"<urn:uuid:441564b3-272c-496f-86ac-212282f4af9c>\",\"WARC-IP-Address\":\"140.254.118.68\",\"WARC-Target-URI\":\"https://ask.sagemath.org/question/33000/loop-over-integers-mod-p/\",\"WARC-Payload-Digest\":\"sha1:5XQYJ2WLZZ3AHTWX5IY2SWCXT3KUV5GC\",\"WARC-Block-Digest\":\"sha1:BMT5BAOCNEZLIXMEYHEUIR4LIAMSBDY5\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141184123.9_warc_CC-MAIN-20201125183823-20201125213823-00010.warc.gz\"}"}