Ad

Revised to be done in Time Complexity of O(1)

Code
Diff
  • const multiple3And5 = (number) => {
      number--
      
      return multiplesSum(3, number) + multiplesSum(5, number) - multiplesSum(15, number)
    }
    const multiplesSum = (multiple, max) => {
      const n = Math.floor(max / multiple)
      
      return multiple * (n * (n + 1) / 2)
    }
    • const multiple3And5 = (number) => {
    • let sum = 0;
    • for(let i = 0; i < number; i++) {
    • if(i % 3 == 0 || i % 5 == 0) sum += i;
    • }
    • number--
    • return sum;
    • return multiplesSum(3, number) + multiplesSum(5, number) - multiplesSum(15, number)
    • }
    • const multiplesSum = (multiple, max) => {
    • const n = Math.floor(max / multiple)
    • return multiple * (n * (n + 1) / 2)
    • }