Skip to content

Instantly share code, notes, and snippets.

@Feddas
Forked from WilliamHester/TwoHundred.java
Last active August 29, 2015 14:23
Show Gist options
  • Save Feddas/fb7abd66bfa264d38d89 to your computer and use it in GitHub Desktop.
Save Feddas/fb7abd66bfa264d38d89 to your computer and use it in GitHub Desktop.
If A=1, B=2, C=3, etc., then T+W+O+H+U+N+D+R+E+D+A+N+D+F+I+F+T+Y+N+I+N+E = 259
/// <summary>
/// brute force solution for http://www.reddit.com/r/Showerthoughts/comments/3a3xzw/if_a1_b2_c3_etc_then_twohundredandfiftynine_259/
/// Interaction logic for CypherLiteral.xaml
/// </summary>
public partial class CypherLiteral : UserControl
{
public readonly String[] ONES = new String[]{"",
"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
"SEVEN", "EIGHT", "NINE"};
public readonly String[] TEENS = new String[]{"TEN",
"ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN",
"FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"};
public readonly String[] TENS = new String[]{"",
"TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY",
"SIXTY", "SEVENTY", "EIGHTY", "NINETY"};
public const String HUNDRED = "HUNDRED";
public const String THOUSAND = "THOUSAND";
public const String NEGATIVE = "NEGATIVE";
public const int MAX = 9999;
public CypherLiteral()
{
InitializeComponent();
increment();
}
private void increment()
{
for (int i = -1 * MAX; i <= MAX; i++)
{
StringBuilder number = new StringBuilder();
int num = i;
if (num < 0)
{
number.Append(NEGATIVE);
num *= -1;
}
if (num / 1000 > 0)
{
number.Append(ONES[num / 1000])
.Append(THOUSAND);
num %= 1000;
}
if (num / 100 > 0)
{
number.Append(ONES[num / 100])
.Append(HUNDRED);
num %= 100;
}
if (num > 10 && num < 20)
{
number.Append(TEENS[num % 10]);
}
else if (num > 0)
{
number.Append(TENS[num / 10])
.Append(ONES[num % 10]);
}
int sum = SumChars(number.ToString());
int delta = Math.Abs(sum - Math.Abs(i));
if (delta <= 2)
txtOut.Text += string.Format(", {0} = {1}({2})", number.ToString(), SumChars(number.ToString()), delta);
}
}
int SumChars(string chars)
{
int sum = 0;
for (int i = 0; i < chars.Length; i++)
{
sum += chars[i] - 'A' + 1;
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment