/// <summary> /// https://projecteuler.net/problem=56 /// </summary> /// <param name="arguments"></param> /// <returns></returns> public static Result PowerfulDigitSum(Problem arguments) { var limit = arguments.IntNumber; long max = 100; // include all 100 * 1^b to begin with for (int a = 2; a < limit; a++) { for (int b = 1; b < limit; b++) { var ab = BigInteger.Pow(a, b); var sum = UtilityMath.DigitsSum(ab.ToString()); if (sum > max) { max = sum; } } } var answer = max.ToString(); var message = string.Format("The maximum digital sum of natural numbers of the form, a^b, where a, b < {0}, is {1}.", limit, answer); if (Answers[arguments.Id] != answer) { message += string.Format(" => INCORRECT ({0})", Answers[arguments.Id]); } var r = new Result(arguments.Id, message) { Answer = answer }; return(r); }
public static Result PowerDigitSum(Problem arguments) { var e = Convert.ToInt16(arguments.LongNumber); var b = arguments.IntNumber; var p = UtilityMath.PowOverflow(b, e); var s = UtilityMath.DigitsSum(p); var m = string.Format("Sum of digits of {0}^{1} is {2}.", b, e, s); var r = new Result(arguments.Id, m); return(r); }
/// <summary> /// https://projecteuler.net/problem=80 /// </summary> /// <param name="arguments"></param> /// <returns></returns> public static Result SquareRootDigitalExpansion(Problem arguments) { var count = (int)arguments.Numbers[0]; var digits = (int)arguments.Numbers[1]; long sum = 0; #region Sqrt Test for (double expected_number = 0.001; expected_number < 100; expected_number += 0.5d) { double actual_number = UtilityMath.Sqrt_By_Substraction(expected_number); if ((expected_number - (actual_number * actual_number)) > .0005) { throw new ArithmeticException(string.Format("Sqrt by substraction differed with margin of error for {0}", expected_number)); } } #endregion for (var number = 1; number <= count; number++) { if (!UtilityMath.IsPerferctSquare((double)number)) { var sqrt = UtilityMath.SqrtDigitExpansion(number, digits); sum += UtilityMath.DigitsSum(sqrt.ToString()); } } var answer = sum.ToString(); var message = string.Format("The total of the digital sums of the first {0} decimal digits for all the irrational square roots of the first {1} natural numbers is {2}.", digits, count, answer); if (Answers[arguments.Id] != answer) { message += string.Format(" => INCORRECT ({0})", Answers[arguments.Id]); } var r = new Result(arguments.Id, message) { Answer = answer }; return(r); }