public static void OldSolve(int numZeros) { BigInteger notBouncy = 0; BigInteger max = BigInteger.Pow(10, numZeros); for (BigInteger num = 1; num < max; ) { int bouncedAtIdx = 0; char lastChar, thisChar; string numStr = num.ToString(); if (MiscFunctions.IsBouncy(numStr, out bouncedAtIdx, out thisChar, out lastChar)) { // go ahead and fastforward to next possible non-bouncy number int len = numStr.Length; string topSide, repeater; if (thisChar > lastChar) { // was supposed to be going down, so next char needs to be smaller // num=9624 // AtIdx = 3, lastChar = 2, thisChar = 4 // increment the top part to 963 and put 0's the rest of the way out // giving 9630 BigInteger topSideBi = new BigInteger(numStr.Substring(0, bouncedAtIdx), 10); topSideBi++; topSide = topSideBi.ToString(); repeater = new string('0', len - bouncedAtIdx); } else { //if (bouncedAtIdx + 1 == len) continue; // it's at the 1s digit // was supposed to be going up. So next set of chars need to be the lowest value going up // num = 1240 // AtIdx = 3, lastChar = 4, thisChar = 0 // next char will be all 4's starting after position 3. // giving 124 + 4 being 1244 topSide = numStr.Substring(0, bouncedAtIdx); repeater = new string(lastChar, len - bouncedAtIdx); } num = new BigInteger(topSide + repeater, 10); } else { notBouncy++; num++; } } Console.WriteLine("For {0} {1}", max, notBouncy); }
private static bool WellBalanced(BigInteger bi) { char[] digits = bi.ToString().ToCharArray(); int midIdx = (int)Math.Ceiling(digits.Length/(double) 2) - 1; int maxIdx = digits.Length - 1; int left = 0; int right = 0; //int idxLeft = midIdx; //int idxRight = digits.Length - midIdx - 1; for (int i = 0; i <= midIdx ;i++ ) { left += (int)digits[i]; right += (int)digits[maxIdx - i]; } return left==right; }
public static string GetSequence(BigInteger num, BigInteger denom) { Console.WriteLine("\tBEFORE {0} {1}", num,denom); MyMath.ReduceFraction(ref num, ref denom); Console.WriteLine("\tAFTER {0} {1}", num, denom); return ""; int decimalsOfPrecision = 0; Int32.TryParse(denom.ToString(),out decimalsOfPrecision); decimalsOfPrecision *= 2; decimalsOfPrecision = Math.Max(5, decimalsOfPrecision); //BigInteger offset = BigInteger.Parse("1" + Repeat('0', decimalsOfPrecision)); // use offset to get n decimals of precision BigInteger offset = BigInteger.Pow(10, decimalsOfPrecision); Console.WriteLine("decimalsOfPrecision={0}", decimalsOfPrecision); BigInteger fraction = offset * num / denom; string unitFraction = fraction.ToString(); unitFraction = Repeat('0', decimalsOfPrecision - unitFraction.Length) + unitFraction; string sequence = GetRepeatingElement(unitFraction); return (sequence=="0")?"":sequence; }
/// <summary> /// Tests the correct implementation of the modulo exponential function /// using RSA encryption and decryption (using pre-computed encryption and /// decryption keys). /// </summary> /// <param name="rounds">The rounds.</param> public static void RSATest(int rounds) { Random rand = new Random(1); byte[] val = new byte[64]; // private and public key BigInteger bi_e = new BigInteger("a932b948feed4fb2b692609bd22164fc9edb59fae7880cc1eaff7b3c9626b7e5b241c27a974833b2622ebe09beb451917663d47232488f23a117fc97720f1e7", 16); BigInteger bi_d = new BigInteger("4adf2f7a89da93248509347d2ae506d683dd3a16357e859a980c4f77a4e2f7a01fae289f13a851df6e9db5adaa60bfd2b162bbbe31f7c8f828261a6839311929d2cef4f864dde65e556ce43c89bbbf9f1ac5511315847ce9cc8dc92470a747b8792d6a83b0092d2e5ebaf852c85cacf34278efa99160f2f8aa7ee7214de07b7", 16); BigInteger bi_n = new BigInteger("e8e77781f36a7b3188d711c2190b560f205a52391b3479cdb99fa010745cbeba5f2adc08e1de6bf38398a0487c4a73610d94ec36f17f3f46ad75e17bc1adfec99839589f45f95ccc94cb2a5c500b477eb3323d8cfab0c8458c96f0147a45d27e45a4d11d54d77684f65d48f15fafcc1ba208e71e921b9bd9017c16a5231af7f", 16); Console.WriteLine("e =\n" + bi_e.ToString(10)); Console.WriteLine("\nd =\n" + bi_d.ToString(10)); Console.WriteLine("\nn =\n" + bi_n.ToString(10) + "\n"); for (int count = 0; count < rounds; count++) { // generate data of random length int t1 = 0; while (t1 == 0) t1 = (int)(rand.NextDouble() * 65); bool done = false; while (!done) { for (int i = 0; i < 64; i++) { if (i < t1) val[i] = (byte)(rand.NextDouble() * 256); else val[i] = 0; if (val[i] != 0) done = true; } } while (val[0] == 0) val[0] = (byte)(rand.NextDouble() * 256); Console.Write("Round = " + count); // encrypt and decrypt data BigInteger bi_data = new BigInteger(val, t1); BigInteger bi_encrypted = bi_data.modPow(bi_e, bi_n); BigInteger bi_decrypted = bi_encrypted.modPow(bi_d, bi_n); // compare if (bi_decrypted != bi_data) { Console.WriteLine("\nError at round " + count); Console.WriteLine(bi_data + "\n"); return; } Console.WriteLine(" <PASSED>."); } }
public static BigInteger Reverse(BigInteger num) { string reversed = String.Empty; foreach (char c in num.ToString()) { reversed = c.ToString() + reversed; } return new BigInteger(reversed,10); }
//public static bool IsNotBouncy(int num) //{ // char[] digits = num.ToString().ToCharArray(); // int lastDigit = Int32.Parse(digits[0].ToString()); // bool wentUp = false; // bool wentDn = false; // foreach (char c in digits) // { // int thisDigit = Int32.Parse(c.ToString()); // if (thisDigit > lastDigit) // wentUp = true; // if (thisDigit < lastDigit) // wentDn = true; // if (wentUp && wentDn) // { // return false; // } // lastDigit = thisDigit; // } // return (wentUp && wentDn); //} //public static bool IsBouncy(int num) //{ // char[] digits = num.ToString().ToCharArray(); // int lastDigit = Int32.Parse(digits[0].ToString()); // bool wentUp = false; // bool wentDn = false; // foreach (char c in digits) // { // int thisDigit = Int32.Parse(c.ToString()); // if (thisDigit > lastDigit) // wentUp = true; // if (thisDigit < lastDigit) // wentDn = true; // if (wentUp && wentDn) // { // break; // } // lastDigit = thisDigit; // } // return (wentUp && wentDn); //} public static int SumDigits(BigInteger num) { int sum = 0; foreach ( char c in num.ToString( ).ToCharArray( ) ) { sum += Int32.Parse(c.ToString()); } return sum; }
public static int SumDigits(BigInteger value) { return SumDigits(value.ToString()); }