Created
December 2, 2014 16:19
-
-
Save oyms/4b19dcba474e33c8ec9b to your computer and use it in GitHub Desktop.
Luke 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var result = FindNumber(9); | |
Console.WriteLine (result); | |
} | |
public int FindNumber(int n){ | |
var seed = n * (int) Math.Pow(10,n-1); | |
return FindNumber(seed, n-1, n, new Primes()); | |
} | |
public int FindNumber(int seed, int position, int lastDigit, Primes primes){ | |
if(position < 0) return seed; | |
foreach (var element in primes.BeginningWith(lastDigit)) | |
{ | |
var p = primes.Clone(); | |
var newSeed = AddLastDigitAt(seed,element,position); | |
p.Remove(element); | |
var result = FindNumber(newSeed,position-1,element % 10, p); | |
if(result > 0) return result; | |
} | |
return 0; | |
} | |
public int AddLastDigitAt(int seed, int numberToAdd, int pos){ | |
var digit = numberToAdd % 10; | |
return seed + digit * (int) Math.Pow(10,pos-1); | |
} | |
public class Primes | |
{ | |
List<int> _primes; | |
public Primes():this(new List<int>{11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 }) | |
{ | |
} | |
private Primes(List<int> primes) | |
{ | |
_primes = primes; | |
} | |
public Primes Clone(){ | |
return new Primes(_primes.ToList()); | |
} | |
public void Remove(int d){ | |
_primes.Remove(d); | |
} | |
public IEnumerable<int> BeginningWith(int d){ | |
return _primes.OrderBy (x => x).Where (x => (x-x%10)/10 == d); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment