Created
February 26, 2018 12:39
-
-
Save Fhernd/8afc23310987b081fa009a9385777bdb to your computer and use it in GitHub Desktop.
Factoriones en C# con LINQ.
This file contains 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
void Main() | |
{ | |
Enumerable.Range(0, 1000) | |
.Where(k => | |
k.Digitos().Where(n => n > 0) | |
.Select(d => Enumerable.Range(1, d).Aggregate((x, y) => x * y)).Sum() == k) | |
.Dump("Factoriones"); | |
} | |
public static class ExtractorDigitos | |
{ | |
// Método de extensión para extraer los digitos de un número> | |
public static IEnumerable<int> Digitos(this int numero) | |
{ | |
List<char> caracteres = new List<char>() { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; | |
List<int> digitos = new List<int>(); | |
foreach (char caracter in numero.ToString()) | |
{ | |
digitos.Add(caracteres.IndexOf(caracter)); | |
} | |
return digitos.AsEnumerable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment