0
function sum_of_mul_3_5(n) {
  const max_3 = Math.floor((n - 1) / 3);
  const max_5 = Math.floor((n - 1) / 5);
  const max_15 = Math.floor((n - 1) / 15);
  const total_3 = (3 * max_3 * (max_3 + 1)) / 2;
  const total_5 = (5 * max_5 * (max_5 + 1)) / 2;
  const total_15 = (15 * max_15 * (max_15 + 1)) / 2;
  console.log(total_3 + total_5 - total_15);
}

sum_of_mul_3_5(1000000000);
233333333166666700

The output is not correct.

Using c++ long data type, the correct answer is:

233333333166666668

How do I increase the max value of javascript Number data type so that the function sum_of_mul_3_5 will output the correct result?

  • 1
    `function sum_of_mul_3_5(n) { const max_3 = (n - 1n) / 3n; const max_5 = (n - 1n) / 5n; const max_15 = (n - 1n) / 15n; const total_3 = (3n * max_3 * (max_3 + 1n)) / 2n; const total_5 = (5n * max_5 * (max_5 + 1n)) / 2n; const total_15 = (15n * max_15 * (max_15 + 1n)) / 2n; console.log(total_3 + total_5 - total_15); } sum_of_mul_3_5(1000000000n);` – User May 04 '22 at 16:27
  • It worked. Can you explain? Is there a way to remove the `n` when printing to console? – Bear Bile Farming is Torture May 04 '22 at 16:31
  • 1 is a [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number). 1n is a [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt). You cannot mix them. "1 + 1n" will throw an error. – User May 04 '22 at 16:40
  • Also, since you mentioned the **long** integer data type in your post, you could've searched for "[Javascript **long** integer](https://stackoverflow.com/questions/17320706/javascript-long-integer)". – User May 04 '22 at 16:45

0 Answers0