Problem 1 "Multiples of 3 or 5"

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

問 1 「3と5の倍数」

10未満の自然数のうち, 3 もしくは 5 の倍数になっているものは 3, 5, 6, 9 の4つがあり, これらの合計は 23 になる.

同じようにして, 1000 未満の 3 か 5 の倍数になっている数字の合計を求めよ.

struct ArithmeticProgression {
    first: i32,
    last: i32,
    diff: i32,
}

impl ArithmeticProgression {
    fn arithmetic_series(&self) -> i32 {
        assert!(self.diff != 0);
        assert!(
            (self.first > self.last) == (0 > self.diff),
            "The relationship between the first element and the last element \
            with the negativity of the common difference is incoherent."
        );
        let terms = (self.last - self.first) / self.diff + 1;
        assert!(
            self.first + self.diff * (terms - 1) == self.last,
            "The length between the first element, \
            the common difference and the last element is incoherent."
        );
        (self.first + self.last) * terms / 2
    }
}

fn main() {
    let a = ArithmeticProgression {
        first: 0,
        last: 999,
        diff: 3,
    }
    .arithmetic_series();
    let b = ArithmeticProgression {
        first: 0,
        last: 995,
        diff: 5,
    }
    .arithmetic_series();
    let ab = ArithmeticProgression {
        first: 0,
        last: 999 - 999 % 15,
        diff: 15,
    }
    .arithmetic_series();
    println!("{}", a + b - ab);
    assert_eq!(a + b - ab, 233168);

    assert_eq!(
        ArithmeticProgression {
            first: 2,
            last: 14,
            diff: 3
        }
        .arithmetic_series(),
        2 + 5 + 8 + 11 + 14
    );
    assert_eq!(
        ArithmeticProgression {
            first: 3,
            last: 12,
            diff: 3
        }
        .arithmetic_series(),
        3 + 6 + 9 + 12
    );
    assert_eq!(
        ArithmeticProgression {
            first: 1,
            last: 10,
            diff: 1
        }
        .arithmetic_series(),
        (1 + 10) * (10 / 2)
    );
    assert_eq!(
        ArithmeticProgression {
            first: -10,
            last: 10,
            diff: 1
        }
        .arithmetic_series(),
        0
    );
    assert_eq!(
        ArithmeticProgression {
            first: -4,
            last: 2,
            diff: 1
        }
        .arithmetic_series(),
        -4 - 3
    );
    assert_eq!(
        ArithmeticProgression {
            first: 5,
            last: -1,
            diff: -2
        }
        .arithmetic_series(),
        5 + 3 + 1 - 1
    );
    assert_eq!(
        ArithmeticProgression {
            first: 6,
            last: 0,
            diff: -2
        }
        .arithmetic_series(),
        6 + 4 + 2
    );
    assert_eq!(
        ArithmeticProgression {
            first: 6,
            last: 2,
            diff: -2
        }
        .arithmetic_series(),
        6 + 4 + 2
    );
    assert_eq!(
        ArithmeticProgression {
            first: -6,
            last: 0,
            diff: 2
        }
        .arithmetic_series(),
        -6 + -4 + -2
    );
    assert_eq!(
        ArithmeticProgression {
            first: -6,
            last: -2,
            diff: 2
        }
        .arithmetic_series(),
        -6 + -4 + -2
    );
}
package main

import (
	"fmt"
)

func arithmeticSeries(n uint32) uint32 {
	d := 999 / n
	return n * d * (d + 1) / 2
}

func Example() {
	ans := arithmeticSeries(3) + arithmeticSeries(5) - arithmeticSeries(15)
	fmt.Println(ans)
	// Output: 233168
}
→ Go playground
function assert(condition: any, msg?: string): asserts condition {
  if (!condition) {
    throw new Error(msg)
  }
}

function arithmeticSeries(n: number): number {
  assert(Number.isInteger(n));
  assert(Math.sign(n) === 1, "Tried a negative n or division by zero.");
  const d = 999 / n | 0;
  return n * d * (d + 1) / 2 | 0;
}

const ans = arithmeticSeries(3) + arithmeticSeries(5) - arithmeticSeries(15);
console.log(ans);
assert(ans === 233168);
→ TypeScript playground