Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
reg matlab code for primitive roots

Subject: reg matlab code for primitive roots

From: naveen

Date: 13 Nov, 2010 18:43:03

Message: 1 of 3

function roots=primitive(num)
% num=7; for example
d=1;
k=0;
roots=0;

z=1;
for r=2 : (num-1)
    k=r^d;
    k=mod(k,num);
    
    while k>1
        
            d=d+1;
            k=k*r;
            k=mod(k,num);
            
    end
    if d==(num-1)
        roots(z,1)=r;
        z=z+1;
    end
    d=1;
end
z=z-1;
roots

        
            
            
        

Subject: reg matlab code for primitive roots

From: Matt Fig

Date: 13 Nov, 2010 20:10:04

Message: 2 of 3

O.k., was there a question in there?

Subject: reg matlab code for primitive roots

From: Roger Stafford

Date: 13 Nov, 2010 21:53:04

Message: 3 of 3

"naveen " <nkumarkuppili@gmail.com> wrote in message <ibmm7n$gvl$1@fred.mathworks.com>...
> .......
> while k>1
> d=d+1;
> k=k*r;
> k=mod(k,num);
> end
> ........
- - - - - - - - -
  You are apparently seeking the primitive roots of unity modulo 'num'. However, in your code you had better never let 'num' have two different prime factors, or you will get caught in an endless loop with your 'while' function. For example, if num is 6, then with r = 2 you get k = 2, then k = 4, then k = 2, then k = 4, infinitely repeating. You will need a somewhat different procedure to avoid this.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us