Task

The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n.

Show output for n = 1000.

This is is the same as Project Euler problem 1.

Extra credit: do this efficiently for n = 1e20 or higher.




$ include "seed7_05.s7i";
  include "bigint.s7i";
 
const func bigInteger: sum35 (in bigInteger: n) is func
  result
    var bigInteger: sum35 is 0_;
  local
    const func bigInteger: sumMul (in bigInteger: n, in bigInteger: f) is func
      result
        var bigInteger: sumMul is 0_;
      local
        var bigInteger: n1 is 0_;
      begin
        n1 := pred(n) div f;
        sumMul := f * n1 * succ(n1) div 2_;
      end func;
   begin
     sum35 := sumMul(n, 3_) + sumMul(n, 5_) - sumMul(n, 15_);
   end func;
 
const proc: main is func
  begin
    writeln(sum35(1000_));
    writeln(sum35(10_ ** 20));
  end func;