Skip to content

Instantly share code, notes, and snippets.

@sguzunov
Created February 18, 2018 11:16
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 sguzunov/d2be10baafa5899002ea025fe9cca1a2 to your computer and use it in GitHub Desktop.
Save sguzunov/d2be10baafa5899002ea025fe9cca1a2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicNumbers
{
public class Program
{
public static void Main()
{
// Version 1
//SolutionStoringResultsInCollection();
// Version 2
//SolutionPrintNumberOnConsoleImmediately();
// Version 3
//Reading the 'magic number'
//int magicNumber = int.Parse(Console.ReadLine());
//int[] number = new int[6];
//SolutionRecursive(0, magicNumber, number);
}
public static void SolutionRecursive(int position, int magicNumber, int[] number)
{
if (position >= 6)
{
int product = number[0] * number[1] * number[2] * number[3] * number[4] * number[5];
if (product == magicNumber)
{
string output = string.Join("", number);
Console.Write(output);
Console.Write(" ");
}
return;
}
for (int i = 1; i <= 9; i++)
{
number[position] = i;
int nextPosition = position + 1;
SolutionRecursive(nextPosition, magicNumber, number);
}
}
public static void SolutionStoringResultsInCollection()
{
// Reading the 'magic number'
int magicNumber = int.Parse(Console.ReadLine());
// Storing all valid outputs
var result = new List<string>();
// For each position [1...6], place one digit
for (int i = 1; i <= 9; i++)
{
for (int i1 = 1; i1 <= 9; i1++)
{
for (int i2 = 1; i2 <= 9; i2++)
{
for (int i3 = 1; i3 <= 9; i3++)
{
for (int i4 = 1; i4 <= 9; i4++)
{
for (int i5 = 1; i5 <= 9; i5++)
{
var product = i * i1 * i2 * i3 * i4 * i5;
// If the product is equal to the 'magic number', store it
if (product == magicNumber)
{
result.Add($"{i}{i1}{i2}{i3}{i4}{i5}");
}
}
}
}
}
}
}
// Joining the result
var output = string.Join(" ", result.ToArray());
// Print to 'terminal'
Console.WriteLine(output);
}
public static void SolutionPrintNumberOnConsoleImmediately()
{
// Reading the 'magic number'
int magicNumber = int.Parse(Console.ReadLine());
// For each position [1...6], place one digit
for (int i = 1; i <= 9; i++)
{
for (int i1 = 1; i1 <= 9; i1++)
{
for (int i2 = 1; i2 <= 9; i2++)
{
for (int i3 = 1; i3 <= 9; i3++)
{
for (int i4 = 1; i4 <= 9; i4++)
{
for (int i5 = 1; i5 <= 9; i5++)
{
var product = i * i1 * i2 * i3 * i4 * i5;
// If the product is equal to the 'magic number', print it
if (product == magicNumber)
{
Console.Write($"{i}{i1}{i2}{i3}{i4}{i5}");
Console.Write(" ");
}
}
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment