Skip to content

Instantly share code, notes, and snippets.

@grishace
Created June 11, 2020 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grishace/5806ef057dbe04b0db11058e87eb51b0 to your computer and use it in GitHub Desktop.
Save grishace/5806ef057dbe04b0db11058e87eb51b0 to your computer and use it in GitHub Desktop.
Arithmetic || geometric progression
var arr = new [] { 0, 2, 4, 6, 8, 10 };
Func<int[], bool, Func<int, Tuple<bool, int>, bool>, bool> aggregate =
(a, b, fn) => a.Skip(2).Aggregate(Tuple.Create(b, a[1]), (res, i) => Tuple.Create(res.Item1 && fn(i, res), i)).Item1;
var diff = arr[1] - arr[0];
var isA = aggregate(arr, true, (i, r) => i - r.Item2 == diff);
arr = new [] { 1, 2, 4, 8, 16, 32 };
diff = arr[1] / arr[0];
var isG = aggregate(arr, arr[1] % arr[0] == 0, (i, r) => i / r.Item2 == diff && i % r.Item2 == 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment