public class KeyGenerator
public static string GenerateKey(string code, long hint)
if (string.IsNullOrEmpty(code) || !code.All(char.IsDigit))
if (code.Any(c => c == '0'))
BigInteger currentKey = BigInteger.Parse(code);
BigInteger maxKey = BigInteger.Parse("9".PadLeft(code.Length + 2, '9'));
while (currentKey <= maxKey)
string keyString = currentKey.ToString();
if (keyString.Contains('0'))
foreach (char digitChar in keyString)
product *= (digitChar - '0');
public static void Main(string[] args)
Console.WriteLine($"Code: {code1}, Hint: {hint1}, Key: {GenerateKey(code1, hint1)}");
Console.WriteLine($"Code: {code2}, Hint: {hint2}, Key: {GenerateKey(code2, hint2)}");
Console.WriteLine($"Code: {code3}, Hint: {hint3}, Key: {GenerateKey(code3, hint3)}");
Console.WriteLine($"Code: {code4}, Hint: {hint4}, Key: {GenerateKey(code4, hint4)}");
Console.WriteLine($"Code: {code5}, Hint: {hint5}, Key: {GenerateKey(code5, hint5)}");