Skip to content

Instantly share code, notes, and snippets.

@rnelson
Created May 10, 2021 23:35
Show Gist options
  • Save rnelson/fe7b2cdbc4905a0b518929f7ee8c5779 to your computer and use it in GitHub Desktop.
Save rnelson/fe7b2cdbc4905a0b518929f7ee8c5779 to your computer and use it in GitHub Desktop.
sameDigits()
<Query Kind="Program" />
void Main()
{
sameDigits(1);
sameDigits(10);
sameDigits(251894);
sameDigits(251895);
}
HashSet<char> GetDigits(long n) => new HashSet<char>(n.ToString().ToCharArray().Distinct().OrderBy(c => c).ToArray());
bool Eq<T>(HashSet<T> one, HashSet<T> two)
{
if (one is null && two != null || one != null && two is null) return false;
if (one.Count != two.Count) return false;
return one.Intersect(two).Count() == one.Count;
}
void sameDigits(long n)
{
var n3 = n * n * n;
var nSet = GetDigits(n);
var n3Set = GetDigits(n3);
var eq = Eq(nSet, n3Set);
Console.WriteLine($"sameDigits({n}) // {eq}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment