Esempio n. 1
0
// ================================================================================================
// Problem 13
// ================================================================================================
sint32 Problem13() {
    CBigInt sum;
    for(uintn i = 0; i < kNumbersSize; ++i)
        sum += CBigInt(kNumbers[i]);

    // -- iterate over the ten most significant digits
    uintn numdigits = sum.NumDigits();
    Assert_(numdigits >= 10, "Not enough digits : " UintNFmt_, numdigits);
    flagn correct = true;
    for(uintn i = numdigits; i > 0; --i) {
        CLog::Write("%c", sint8(sum.Digit(i-1) + '0'));
        correct = correct && (kAnswer[numdigits - i] - '0' == sint8(sum.Digit(i-1)));
    }
    CLog::Write("\n");

    Assert_(correct, "Sum should have been \n%s", kAnswer);

    return 0;
}
// ================================================================================================
// Problem 48
// ================================================================================================
int32 Problem48() {
    CBigInt result;
    result.SetDigitLimit(kNumDigits);

    CBigInt temp;
    temp.SetDigitLimit(kNumDigits);

    for(nuint i = 1; i <= kLastNumber; ++i) {
        // -- we don't need to bother with multiples of 10, since
        // -- they'll all end up with at least ten zeros in their least significant digits
        if((i % 10) == 0)
            continue;

        // -- initialize temp to 1
        temp = 1;
        for(nuint j = 0; j < i; ++j) {
            temp *= i;
        }
        result += temp;
    }

    CLog::Write("The last " NUintFmt_ " digits of the sum are \"", kNumDigits);
    nflag correct = true;
    for(nuint i = kNumDigits; i > 0;) {
        --i;
        CLog::Write(NUintFmt_, result.Digit(i));
        if(int8(result.Digit(i) + '0') != kAnswer[kNumDigits-i-1])
            correct = false;
    }
    CLog::Write("\"\n");

    Assert_(correct, "The answer should have been \"%s\"", kAnswer);

    return 0;
}