Skip to content

Instantly share code, notes, and snippets.

@GuyInGrey
Created March 8, 2021 22:08
Show Gist options
  • Save GuyInGrey/43e2776adef466cee31ae8ee19b05681 to your computer and use it in GitHub Desktop.
Save GuyInGrey/43e2776adef466cee31ae8ee19b05681 to your computer and use it in GitHub Desktop.
[Command("fib")]
[Alias("fibonnaci")]
[Syntax("fib <n>")]
[RequireRole("staff")]
public async Task Fib(int n)
{
if (n <= 0) { await ReplyAsync("No."); return; }
if (n == 1) { await ReplyAsync("0"); return; }
if (n == 2) { await ReplyAsync("1"); return; }
var typing = Context.Channel.EnterTypingState();
var startTime = HighResolutionDateTime.UtcNow;
(var a, var b) = ((BigInteger)1, (BigInteger)1); for (var i = 0; i < n - 3; i++)
{
b = a + b; a = b - a;
if ((HighResolutionDateTime.UtcNow - startTime).TotalMilliseconds > 1000 * 60 * 5)
{
typing.Dispose();
await this.ReactError();
await ReplyAsync("Computation took more than 2 minutes, cancelled. Sorry!");
return;
}
}
var took = (HighResolutionDateTime.UtcNow - startTime).TotalMilliseconds;
await this.ReactOk();
var s = b.ToString();
var parts = await s.SplitWithLength(1950);
if (parts.Count > 5)
{
await ReplyAsync($"__Fibonacci Results__\nn = {n}\nTook {took} ms.\nfib(n) has {s.Length} digits.\n" +
$"Did not post due to requiring {parts.Count} messages.");
typing.Dispose();
return;
}
foreach (var p in parts)
{
await ReplyAsync("```\n" + p + "\n```");
}
await ReplyAsync($"__Fibonacci Results__\nn = {n}\nTook {took} ms.\nfib(n) has {s.Length} digits.");
typing.Dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment