2

I'm trinying to implement an algorithm to search a generator of a cyclic group G:
n is the order of the group G , and Pi is the decomposition of n to prime numbers like n = P1^e1 x P2^e2 ....x Pk^ek
this is the algorithm :

Step 1 : Choose an element g from G
Step 2 : for each i from 1 to k do
Step 2.1 : b <- pow ( g , n / Pi )
Step 2.3 : if b =1 goto step 1
Step 3 : return g

this is my implementation in java , I want to use a goto but it generate an error :

public int getGenerateur(){
    List<Integer> facteur=new ArrayList<Integer>();
    facteur=this.getFacteur(nombre);    

    double b;
    int generateur=0;   

    boolean etape = true;
       while(etape){
        etape = false;
        System.out.println("generateur aleatoire:");
        generateur =(int) (Math.random() * (nombre - 2)) + 2;
        System.out.println(generateur);


            for(int s=0;s<facteur.size();s++){      
                b=Math.pow(generateur,(nombre/facteur.get(s)))%(nombre+1);
                System.out.println("b= "+b);
                if(b==1){
                   etape = true;
                   break;

                }

            }   
     }
     return generateur;

}
Angelika
  • 381
  • 1
  • 9
  • 22
  • 1
    I have bad feeling about `goto`. You should take a look at this question ----> [Is there a goto statement in java?](http://stackoverflow.com/questions/2545103/is-there-a-goto-statement-in-java) – Smit Jan 25 '13 at 21:09
  • @smit is there another alternative to call the first step without goto ? – Angelika Jan 25 '13 at 21:19
  • You can make another method. Call that method from this method. I think you can also make use of recursion here. – Smit Jan 25 '13 at 22:17
  • 2
    @Angelika it's also possible to do this with a loop and break/continue statements. – Peter Elliott Jan 25 '13 at 22:18
  • 1
    @PeterElliott +1 Yes Your suggestion seems more easy to implement and give nice transparency to code. – Smit Jan 25 '13 at 22:20
  • I don't think the duplicate is valid, dear mods. This is not so much about the `goto` itself. – Maarten Bodewes Jan 25 '13 at 23:38

1 Answers1

3

if you use an outer loop and a break statement, you should be able to do what you want to do. It would look something like this

   boolean etape = true;
   while(etape){
    etape = false;
    System.out.println("Entrer un generateur :");
    generateur = sc.nextInt();


        for(int s=0;s<facteur.size();s++){      


            b=Math.pow(generateur,(nombre/facteur.get(s)))%(nombre+1);
            System.out.println("b= "+b);
            if(b==1){
               etape = true;
               break;

            }

        }   
 }
 return generateur;
Peter Elliott
  • 3,273
  • 16
  • 30