// load "/home/obrien/subs/conjugacy/attach.m";

IsScalarGroup := function (G)

   return forall {x : x in Generators (G) | IsScalar (x)};

end function;

/* all integer sequences whose i-th entry is in the 
   range [1..M[i] by 1] for i in [1..#M] */

BackTrack := function (M)
 
   if Set (M) eq {1} then return [M]; end if;
 
   offset := 1;
   n := #M;
   m := [1: i in [1..#M]] cat [1];
   original := m;
   min := Minimum (m);
 
   IndexList := [i: i in [1..#M] | M[i] ge min];
   len := #IndexList;
   Append (~IndexList, n + 1);
 
   Solns := [];
   repeat
      index := 1;
      m[IndexList[index]] +:= offset;
      while (index le len and m[IndexList[index]] gt M[IndexList[index]]) do
         m[IndexList[index]] := original[IndexList[index]];
         index +:= 1;
         m[IndexList[index]] +:= offset;                                       
      end while;
      Append (~Solns, Prune (m));
   until (index gt len);
 
   return Solns;
 
end function;

/* largest power of p dividing n */

LargestPower := function (n, p)
   factors := Factorisation (n);
   f := [f[1]: f in factors];
   pos := Position (f, p);
   if pos eq 0 then return 0; end if;
   pow := factors[pos][2];
   return pow;
end function;
