Created
May 10, 2021 23:35
-
-
Save rnelson/fe7b2cdbc4905a0b518929f7ee8c5779 to your computer and use it in GitHub Desktop.
sameDigits()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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