Skip to content

Instantly share code, notes, and snippets.

@cthrash
Created April 30, 2017 15:43
Show Gist options
  • Save cthrash/283897418f05c9c8680fcd918bad0537 to your computer and use it in GitHub Desktop.
Save cthrash/283897418f05c9c8680fcd918bad0537 to your computer and use it in GitHub Desktop.
Solution to FoxTrot 'Jumble'
using System;
using System.Collections.Generic;
namespace foxtrot
{
class Program
{
static IEnumerable<int> FourDigitMultipleOf117()
{
for (int i = ((1000/117)+1)*117; i < 10000; i += 117) {
yield return i;
}
}
static IEnumerable<int> ThreeDigitPrime()
{
bool[] arr = new bool[1000];
for (int i = 3; i<1000; i+=2) {
if (!arr[i]) {
if (i > 99) {
yield return i;
}
for (int j = i + i; j < 1000; j += i) {
arr[j] = true;
}
}
}
}
static IEnumerable<int> FourDigitCube()
{
for (int i = 31; ; i++) {
int j = i * i;
if (j > 9999) break;
yield return j;
}
}
static IEnumerable<int> NineDigitFactorial()
{
int i = 1;
for (int j = 2; ;j++) {
i *= j;
if (i > 999999999) break;
if (i > 99999999) yield return i;
}
}
static string DigitSort(int i)
{
var chars = i.ToString().ToCharArray();
Array.Sort(chars);
return new String(chars);
}
static int enumer(int match, Func<IEnumerable<int>> func)
{
var matchString = DigitSort(match);
foreach (int i in func()) {
if (DigitSort(i) == matchString) {
Console.WriteLine(i);
return i;
}
}
return 0;
}
static void Main(string[] args)
{
enumer(8820, FourDigitMultipleOf117);
enumer(134, ThreeDigitPrime);
enumer(6409, FourDigitCube);
enumer(601040907, NineDigitFactorial);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment