次の形式の数の素因数のリストがあります。int[]factors= {number of factor、factor1、poweroffactor1、factor2、poweroffactor2、...};
すべての要素を生成する動的にネストされたforループに相当するものを取得したいのですが、forループは次のようになります。
int currentpod = 1;
for(int i=0;i<factors[2];i++)
{
currentprod *= Math.Pow(factors[1],i);
for(int j=0;j<factors[4];j++)
{
currentprod *= Math.Pow(factors[3],i);
...
//When it hits the last level (i.e. the last prime in the list, it writes it to a list of divisors
for(int k=0;k<factors[6];k++)
{
divisors.Add(Math.Pow(factors[5],k)*currentprod);
}
}
}
残念ながら、currentprodが十分にリセットされないため、このコードは爆発します。これを達成するために私が使用している実際のコードは次のとおりです。
public static List<int> createdivisorlist(int level, List<int> factors, int[] prodsofar,List<int> listsofar)
{
if (level == factors[0])
{
prodsofar[0] = 1;
}
if (level > 1)
{
for (int i = 0; i <= 2*(factors[0]-level)+1; i++)
{
prodsofar[level-1] = prodsofar[level] * (int)Math.Pow(factors[2 * (factors[0] - level) + 1], i);
listsofar = createdivisorlist(level - 1, factors, prodsofar, listsofar);
}
}
else
{
for (int i = 0; i <= factors.Last(); i++)
{
listsofar.Add(prodsofar[level] * (int)Math.Pow(factors[2 * (factors[0] - level) + 1], i));
if (listsofar.Last() < 0)
{
int p = 0;
}
}
return listsofar;
}
return listsofar;
}
元の引数は次のとおりです。level=factors[0]factor=上記で指定された形式の素因数のリストprodsofar[]=すべての要素は1ですlistsofar=空のリスト
「爆発」せず、代わりに私が概説したことを実行するように、prodsofarをリセットするにはどうすればよいですか?注:テストとして、2310を使用します。現在のコードでは、追加される除数は負です(intオーバーフロー)。