ウィキペディアの小数の繰り返しに関する情報を読んだ後、小数の繰り返し部分の桁数を見つける方法を見つけました。
例えば、
1/3 = 0.33333333333333333333333333333... したがって、結果は 1 桁です。
1/7 = 0.142857142857142857142857142857... したがって、結果は 6 桁になります。
ただし、(Java での) 私の方法は 1/6 では機能しませんでした。
1/6 = 0.1666... したがって、小数の非反復部分にもかかわらず、結果は 1 桁になります。
うまくいく解決策を見つけました(Nayuki Minaseの功績による)。
private static int getCycleLength(int n)
{
Map<Integer,Integer> stateToIter = new HashMap<Integer,Integer>();
int state = 1;
int iter = 0;
while (!stateToIter.containsKey(state))
{
stateToIter.put(state, iter);
state = state * 10 % n;
iter++;
}
System.out.println(iter + " - " + stateToIter.get(state));
return iter - stateToIter.get(state);
}
このアルゴリズムがどのように機能するかを誰かに説明してもらえますか? ありがとうございました。