Finding primitive roots using python

In summary: def prim_root(value): # `tot` gets the list of values coprime to the input, # so len(tot()) is the correct totient value totient = tot(value) roots = [] exp = len(totient) for x in totient: y = 1 while pow(x, y, value) != 1:# i forget exactly why i did this y += 1 # i think it was because of the if y == exp:
  • #1
wrong_class
18
0
Im not sure if this should go in the math/number theory section or here, but here it goes:

how do programs calculate the primitive roots mod n of extremely large primes? My program will only go up to 12-14 bits before having memory errors caused by storage of the totient of the prime number

does anyone have any code that i can integrate into my program? pseudocode? general idea that can be easily translated into code? anything?

THIS IS NOT HOMEWORK. WRITING MATH PROGRAMS IS A HOBBY, NOT AN ASSIGNMENT
 
Technology news on Phys.org
  • #2
how do programs calculate the primitive roots mod n of extremely large primes?
It depends on the library your using, but probably the way it's described in the wiki article.

Can I see the code you're using?
As a quick fix for memory errors, I try to optimize my code so that as much of the math as possible is done in place 'cause python's memory management is kind of lousy.

Are you using numpy and scipy (which push a lot of the heavy number crunching to C++ and fortran) and what kind of machine are you using? I do some fairly large number crunching with numpy/scipy and can usually push it pretty far before it croaks.
 
  • #3
Im not using any library at all. I like to write programs with as few outside sources as possible.

my code is currently:
Code:
def prim_root(value):
	# `tot` gets the list of values coprime to the input, 
        # so len(tot()) is the correct totient value
	totient = tot(value)
	roots = []
	exp = len(totient)
	for x in totient:
		y = 1
		while pow(x, y, value) != 1:# i forget exactly why i did this
			y += 1              # i think it was because of the 
		if y == exp:                # period of the mod value
			roots += [x]
	return roots

i had a code that only tested prime numbers, using the carmichael == totient test, but it turns out this code is faster

im using a lenovo t410 laptop
 
  • #4
wrong_class said:
Im not using any library at all. I like to write programs with as few outside sources as possible.
*headdesk* It's really important to pick up enough software skills to be able to write your own code, but it's just as critical to be able to use other peoples code. Numpy & Scipy are optimized for numerical calculations and therefore may handle your numbers better.

Can I see tot and pow? I'm wondering if the problem may be in either of them.

random, but why aren't you doing roots.append(x)?

My program will only go up to 12-14 bits before having memory errors caused by storage of the totient of the prime number
which line does your code crash at?

im using a lenovo t410 laptop
how much ram?
 
  • #5
Code:
def gcd(a, b):
	a, b = max(a, b), min(a, b)
	c = 1
	while c:
		c = a % b
		a = b
		b = c
	return a

def tot(n):
	phi = []
	x = 1
	while x < n:# not for x in xrange(n) because the input is too big for xrange
		if gcd(x, n) == 1:
			phi += [x]
		x += 1
	return phi

pow is the built in command: pow(a,b,c) -> a**b mod c

is .append() faster/otherwise better than +=? if so, i'll change it.

ok. the code errors in 'tot': "phi += [x]' due to memory errors. the 'phi' list simply got too big

and the laptop has 4gb ram, with and idle load of about 1.5 gb. right now, its using 2.85 gb

go you know of any mathematical tricks to get at least some of the generators? the wiki article sort of has one, but it involves the factoring problem, making it only theoretical
 
Last edited:
  • #6
wrong_class said:
is .append() faster/otherwise better than +=? if so, i'll change it.
For some implementations, but it probably won't make a difference. It's just the common way to do it.

the code errors in 'tot': "phi += [x]' due to memory errors. the 'phi' list simply got too big
This is going to throw in some latency, but what about writing phi out to disk as you generate it? And then pulling in the numbers one by one as you calculate your factors? That way it's never all stored in memory? If you save it out as a numpy array, you can take advantage of slicing thereby keeping the pointer to the file open the entire time.

How big does phi get? Are you working with Int16? Int32? Int64? and how many numbers? The biggest phi could possibly get before crashing is the size of whatever your free memory+page space is.
 
Last edited:
  • #7
[STRIKE]hm... you gave me an idea...since i don't need the actual list, i'll calculate the actual totient value, rather than the set that makes them up.[/STRIKE] i do need the list, which is a problem

im using values up to 2048 bits. since the values I am getting are changed so that they become the closest prime, the 'tot' list gets quite big
 
  • #8
wrong_class said:
i do need the list, which is a problem
You actually don't. It'll make your code take longer, but you can build totient on the fly since all the phi calculations are done on each element of totient. You just substitute the for loop for the while loop and do everything else nested.

im using values up to 2048 bits.
That's Int256, meaning you probably don't even want to save totient to file as your data set is going to be massive.
 
  • #9
can you show me? I am not seeing it. I didnt put this in, but later on, I am going to be choosing a random value from the primitive roots to use for calculations
 
  • #10
roughly:
Code:
x = 1
roots = []
while x < n:
        if gcd(x, n) == 1:
                y = 1
	        while pow(x, y, value) != 1
	                y += 1              
		        if y == exp:               
			        roots += [x]
        x += 1
but since roots will also probably blow up, you should probably dump roots to file as you write it
 

Related to Finding primitive roots using python

1. What is a primitive root?

A primitive root is an integer that, when raised to powers, cycles through all possible values in a given finite field. In other words, it is a generator of the multiplicative group of a finite field.

2. Why is finding primitive roots important?

Finding primitive roots is important in many areas of mathematics and computer science, including number theory, cryptography, and coding theory. It allows for efficient computations in finite fields and is used in many algorithms and protocols.

3. How can python be used to find primitive roots?

Python has built-in functions and libraries that can be used to perform operations on finite fields, such as the pow() function and the gmpy2 library. These can be utilized to find primitive roots using various algorithms, such as the Shanks algorithm or the Pohlig-Hellman algorithm.

4. What are some challenges in finding primitive roots using python?

One challenge is that python is not optimized for performing operations on large numbers, which may be necessary for finding primitive roots in large finite fields. Additionally, the algorithms used to find primitive roots can be complex and require a deep understanding of number theory.

5. Are there any resources available for learning how to find primitive roots using python?

Yes, there are many online resources and tutorials available for learning how to find primitive roots using python. These include articles, videos, and open-source libraries that provide step-by-step instructions and examples for implementing different algorithms in python.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
21
Views
4K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
4K
Back
Top