public int GetPeriodsOfSquareRoot(int N) { int a0 = GetClosestIntegertToSquare(N); if (a0 * a0 == N) { return(0); } List <PeriodParameters> periodParameterList = new List <PeriodParameters>(); var periodParameters = new PeriodParameters { an = a0, sub = -a0, f = 1.0 }; periodParameterList.Add(periodParameters); while (periodParameterList.Count < 300) { periodParameters = CalcNextPeriodParameters(a0, N, periodParameters); periodParameterList.Add(periodParameters); var treff = periodParameterList.Where(r => r.an == periodParameters.an && r.sub == periodParameters.sub && Math.Abs(r.f - periodParameters.f) < 0.10).ToList(); if (treff.Count() == 2) { int index = periodParameterList.IndexOf(treff[0]); int index2 = periodParameterList.Count() - 1; return(index2 - index); } } throw new Exception($"Periode ikke funnet for {N}"); }
public PeriodParameters CalcNextPeriodParameters(int a0, int N, PeriodParameters tt) { int f2 = N - tt.sub * tt.sub; int an = 0; while (-((an + 1) * f2 / tt.f) + (-1) * tt.sub >= -a0) { an++; } int sub = (int)-((an) * f2 / tt.f) + (-1) * tt.sub; var nextPeriodParameters = new PeriodParameters { an = an, sub = sub, f = (f2 / tt.f) }; return(nextPeriodParameters); }