Пример #1
0
        public static StringNum Multiply(StringNum a, int b)
        {
            string result = "";

            int aLength = a.reversedDigits.Length;
            int carry   = 0;

            for (int digit = 0; digit <= aLength; digit++)
            {
                int aN = 0;
                if (digit < aLength)
                {
                    aN = a.reversedDigits[digit] - '0';
                }

                int value = aN * b + carry;
                carry = value / 10;
                value = value % 10;

                result += (char)('0' + value);
            }
            while (carry != 0)
            {
                int value = carry % 10;
                carry   = carry / 10;
                result += (char)('0' + value);
            }
            result = result.TrimEnd('0');
            string nonReversed = new string(result.Reverse().ToArray());

            return(new StringNum(nonReversed));
        }
Пример #2
0
        private string GetAnswerForGivenStrings(string[] longNums)
        {
            List <StringNum> stringNums = longNums.Select(n => new StringNum(n)).ToList();

            StringNum result = stringNums.Aggregate((x, y) => StringNum.Add(x, y));

            return(result.ToString().Substring(0, 10));
        }
Пример #3
0
        public static StringNum Add(StringNum a, StringNum b)
        {
            string result = "";

            int aLength = a.reversedDigits.Length;
            int bLength = b.reversedDigits.Length;

            int maxLen = aLength;

            if (bLength > maxLen)
            {
                maxLen = bLength;
            }
            maxLen++;

            int carry = 0;

            for (int digit = 0; digit < maxLen; digit++)
            {
                int aN = 0;
                if (digit < aLength)
                {
                    aN = a.reversedDigits[digit] - '0';
                }
                int bN = 0;
                if (digit < bLength)
                {
                    bN = b.reversedDigits[digit] - '0';
                }

                int value = aN + bN + carry;
                carry = 0;
                while (value >= 10)
                {
                    carry++;
                    value -= 10;
                }
                result += (char)('0' + value);
            }
            result = result.TrimEnd('0');
            string nonReversed = new string(result.Reverse().ToArray());

            return(new StringNum(nonReversed));
        }
Пример #4
0
        public override string GetAnswer()
        {
            // yet another BigInteger=Cheating problem.
            // (then again, at this point, I'm reusing the same library I wrote
            // so I'm not sure there's even a point to it...)
            StringNum a      = new StringNum("1");
            StringNum b      = new StringNum("1");
            StringNum c      = new StringNum("2");
            int       cIndex = 3;

            while (c.ToString().Length < 1000)
            {
                a = b;
                b = c;
                c = StringNum.Add(a, b);
                cIndex++;
            }
            return(cIndex.ToString());
        }
Пример #5
0
        private int GetAnswerForFactorial(int factorial)
        {
            StringNum num = new StringNum("1");

            for (int i = 2; i <= factorial; i++)
            {
                num = StringNum.Multiply(num, i);
            }

            string finalFactorial = num.ToString();

            int answer = 0;

            foreach (char c in finalFactorial)
            {
                answer += (c - '0');
            }

            return(answer);
        }
Пример #6
0
        public override string GetAnswer()
        {
            List <string> possibilities = new List <string>();

            Func <string, char, string> AppendNewChar = (s, c) => s + c;
            Func <string, char, string> PrefixNewChar = (s, c) => c + s;

            possibilities.Add("");

            possibilities = ApplyEachPossibleDigit(possibilities, AppendNewChar);
            possibilities = ApplyEachPossibleDigit(possibilities, AppendNewChar);
            possibilities = ApplyEachPossibleDigit(possibilities, AppendNewChar);
            possibilities = RemoveEntriesWhereLastThreeDontDivideBy(possibilities, 2);

            possibilities = ApplyEachPossibleDigit(possibilities, AppendNewChar);
            possibilities = RemoveEntriesWhereLastThreeDontDivideBy(possibilities, 3);

            possibilities = ApplyEachPossibleDigit(possibilities, AppendNewChar);
            possibilities = RemoveEntriesWhereLastThreeDontDivideBy(possibilities, 5);

            possibilities = ApplyEachPossibleDigit(possibilities, AppendNewChar);
            possibilities = RemoveEntriesWhereLastThreeDontDivideBy(possibilities, 7);

            possibilities = ApplyEachPossibleDigit(possibilities, AppendNewChar);
            possibilities = RemoveEntriesWhereLastThreeDontDivideBy(possibilities, 11);

            possibilities = ApplyEachPossibleDigit(possibilities, AppendNewChar);
            possibilities = RemoveEntriesWhereLastThreeDontDivideBy(possibilities, 13);

            possibilities = ApplyEachPossibleDigit(possibilities, AppendNewChar);
            possibilities = RemoveEntriesWhereLastThreeDontDivideBy(possibilities, 17);

            possibilities = ApplyEachPossibleDigit(possibilities, PrefixNewChar);

            StringNum answer = possibilities.Select(s => new StringNum(s)).Aggregate((x, y) => StringNum.Add(x, y));


            return(answer.ToString());
        }
Пример #7
0
        private long GetAnswerForSpecificPower(int powerOfTwo)
        {
            // I couldn't figure out any mathematical trick to derive this other than
            // simply multiplying out the number 2^1000 and then checking.
            // Also, once again, using BigInteger seemed way too much like cheating
            // (pretty sure these problems were written before the BigInteger library was available?)
            StringNum myVal = new StringNum("2");

            for (int i = 2; i <= powerOfTwo; i++)
            {
                myVal = StringNum.Add(myVal, myVal);
            }

            string result  = myVal.ToString();
            int    charSum = 0;

            foreach (char c in result)
            {
                charSum += (c - '0');
            }

            return(charSum);
        }