using System.Collections.Generic;
public static class Program
static IEnumerable<IEnumerable<T>> GetKCombsWithRept<T>(IEnumerable<T> list, int length) where T : IComparable
if (length == 1) return list.Select(t => new T[] { t });
return GetKCombsWithRept(list, length - 1)
.SelectMany(t => list.Where(o => o.CompareTo(t.Last()) >= 0),
(t1, t2) => t1.Concat(new T[] { t2 }));
public static void Main()
Console.WriteLine("Hello World");
double lookingFor = 362899;
_options = new List<string>();
int[] lettersArray = new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
var values1 = new[] { 1, 2, 3, 4, 5 };
foreach (var permutation in GetPermutations(values1))
var combos = GetKCombsWithRept<int>(lettersArray, length);
foreach(var combo in combos){
string option = FormatStringFromLetterPositions(combo.ToArray());
_permutations = new List<string>();
char[] charArray = option.ToCharArray();
foreach (var permutation in GetPermutations(combo))
string word = String.Empty;
for(int i = 0; i < permutation.Count(); i++){
word +=Char.ToString(_letters[permutation.ElementAt(i)]);
sum += GetHashOfLettAtPostion(word, i, length);
Console.WriteLine(string.Join(", ", permutation) + " " + word + " sum " + sum);
Console.WriteLine("Got it: " + permutation + " " + sum);
foreach(string permutation in _permutations)
foreach(var option in _options){
Console.WriteLine("option to be permutated " + option);
_permutations = new List<string>();
char[] charArry = option.ToCharArray();
foreach(string permutation in _permutations)
for(int i = 0; i < permutation.Length; i++){
sum += GetHashOfLettAtPostion(permutation.Substring(i, 1), i, length);
Console.WriteLine("perm " + permutation + " sum " + sum);
Console.WriteLine("Got it: " + permutation + " " + sum);
Console.WriteLine("Number is: " + num);
Console.WriteLine("Add number to itself 3 times: " + result);
int resultTimes37 = result * 37;
Console.WriteLine("Result mutiplied by 37: " + resultTimes37);
Console.WriteLine("Woah! The answer is original number repeated 3 times");
Console.WriteLine(" 5 + 5 + 5 multiplied by 37 = 555");
Console.WriteLine("Hello World");
static List<string> _permutations;
static void permute(char[] arry, int i, int n)
string str = new string(arry);
swap(ref arry[i],ref arry[j]);
swap(ref arry[i], ref arry[j]);
static void swap(ref char a, ref char b)
const string _letters = "acdegilmnoprstuw";
public static int[] _positions = new int[3];
public static double[] _values = new double[3];
public static Something Y1(Func<Something, Something> f){
return (a, b) => f(Y1(f)) (a, b);
public static void ReverseHash(double answer, int length)
public static Something Answer(Something f)
Something del = delegate(double hashedString, int counter)
int lenOfWord = _positions.Length;
for (_positions[counter] = 0; _positions[counter] < _letters.Length; _positions[counter]++)
_values[counter] = _positions[counter] * Math.Pow(37, lenOfWord - 1 - counter);
_values[counter] += (7 * Math.Pow(37, lenOfWord));
foreach (var val in _values)
if (sumOfValues == hashedString)
string unhashedString = FormatStringFromLetterPositions(_positions);
Console.WriteLine("Answer is " + unhashedString);
else if (counter < lenOfWord - 1)
f(hashedString, counter + 1);
public delegate void Something(double hashResult, int counter);
public static string FormatStringFromLetterPositions(int[] positions)
foreach (int i in positions)
resultStr += Char.ToString(_letters[i]);
public double hashedLetterAtPosition;
private static double GetHashOfLettAtPostion(string letter, int position, int length){
double hashedLetterAtPosition = _letters.IndexOf(letter) * Math.Pow(37, length - 1 - position);
hashedLetterAtPosition += (7 * Math.Pow(37, length));
return hashedLetterAtPosition;
public static void TryAgain(double answer, int length){
Dictionary<int, List<DTO>> positions = new Dictionary<int, List<DTO>>();
for(int i = 0; i< length; i++)
for (int letter = 0; letter < _letters.Length; letter++)
double hashedLetterAtPosition = letter * Math.Pow(37, length - 1 - i);
hashedLetterAtPosition += (7 * Math.Pow(37, length));
hashedLetterAtPosition = hashedLetterAtPosition
if(!positions.ContainsKey(i)){
positions[i] = new List<DTO>();
List<DTO> list = positions[i];
static void printCombination(int[] arr, int n, int r)
combinationUtil(arr, n, r, 0, data, 0);
public static List<string> _options;
static void combinationUtil(int[] arr, int n, int r, int index,
sum += _letters[data[j]];
combinationUtil(arr, n, r, index+1, data, i+1);
combinationUtil(arr, n, r, index, data, i+1);
public static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> enumerable)
var array = enumerable as T[] ?? enumerable.ToArray();
var factorials = Enumerable.Range(0, array.Length + 1)
for (var i = 0L; i < factorials[array.Length]; i++)
var sequence = GenerateSequence(i, array.Length - 1, factorials);
yield return GeneratePermutation(array, sequence);
private static IEnumerable<T> GeneratePermutation<T>(T[] array, IReadOnlyList<int> sequence)
var clone = (T[]) array.Clone();
for (int i = 0; i < clone.Length - 1; i++)
Swap(ref clone[i], ref clone[i + sequence[i]]);
private static int[] GenerateSequence(long number, int size, IReadOnlyList<long> factorials)
var sequence = new int[size];
for (var j = 0; j < sequence.Length; j++)
var facto = factorials[sequence.Length - j];
sequence[j] = (int)(number / facto);
number = (int)(number % facto);
static void Swap<T>(ref T a, ref T b)
private static long Factorial(int n)
for (int i = 1; i < n; i++)