Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created February 26, 2018 00:17
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 Fhernd/89c0e51e1ae0a4b100ad99977ced26cc to your computer and use it in GitHub Desktop.
Save Fhernd/89c0e51e1ae0a4b100ad99977ced26cc to your computer and use it in GitHub Desktop.
Generación de números suma-producto en C# con LINQ.
void Main()
{
Enumerable.Range(0, 1000)
.Where(k => {
var digitos = k.Digitos();
return digitos.Sum() * digitos.Aggregate((x, y) => x * y) == k;
}).Dump("Números Suma-Producto entre 0-1000");
}
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