//prioritize digits at digit depth private Digit[] orderDigits(int depth) { Digit[] d = new Digit[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; BigInteger[] products = new BigInteger[10]; BigInteger[] ordered = new BigInteger[10]; for(int i = 0; i < 10; i++) { index[depth] = (Byte)d[i]; setNums(depth); products[i] = nums[0].Value * nums[1].Value; } for (int i = 0; i < 10; i++) ordered[i] = products[i]; Array.Sort(ordered); for (int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { if (products[i] == ordered[j]) d[i] = j; } } return d; }
public void addDigit(Digit d) { if (num == 0)//if value is empty { num = d.Value;//then set single digit value dig = 1;//set single digit counter return;//exit function } //if digits list doesn't have enough digits if (digits.Count + 1 <= dig) digits.Add(digits[dig - 2] * 10); num += digits[dig - 1] * d.Value; dig++; }
public static Pair[] genFirstRoots(Digit d) { //create array of root arrays Pair[][] roots = new Pair[][] { new Pair[] { new Pair(1,1), new Pair(3,7), new Pair(9,9)}, //produce 1 in ones digit new Pair[] { new Pair(1,3), new Pair(7,9)}, //produce 3 in ones digit new Pair[] { new Pair(1,7) , new Pair(3,9)}, //produce 7 in ones digit new Pair[] { new Pair(1,9) , new Pair(3,3), new Pair(7,7)}}; // produce 9 in ones digit switch (d.Value) { case 1: //if ones digit is 1 return roots[0]; case 3: //if ones digit is 3 return roots[1]; case 7: //if ones digit is 7 return roots[2]; case 9: //if ones digit is 9 return roots[3]; default: //else return defualt return null; } }
public Pair(int x = 0, int y = 0) { this.x = new Digit(x); this.y = new Digit(y); }
public Pair[] getPA(int branch, Digit d) { return toPair(nums[branch][d.Value]); }
//generate roots based on Digit provided public PairGenerator(Digit d) { roots = genFirstRoots(d); setup(d, out this.branches); }
private void setup(Digit d, out int branches) { switch (d.Value) { case 1: nums = make1(); branches = 3; break; case 3: nums = make3(); branches = 2; break; case 7: nums = make7(); branches = 2; break; default: nums = make9(); branches = 3; break; } }
//set value at this digit public void setDigit(Digit d, int index) { if (index >= 1)//make sure index is in range { Digit atIndex = new Digit(); setDigitsList(index);//make sure Digits list is long enough if (dig >= index) { atIndex.set(num, index); if (atIndex.Value != 0)//if digit at index is not 0 removeDigit(index);//then set to 0 } num += (digits[index - 1]) * d.Value;//add digit to num at specified index } }