You are viewing a single comment's thread. Return to all comments →
const sumMul = (n) => { const _mul = (x) => { const _t = BigInt(Math.floor(Number((BigInt(n) - 1n) / BigInt(x)))); return (BigInt(x) * _t * (_t + 1n)) / 2n; }; return _mul(3) + _mul(5) - _mul(3 * 5); }; function main() { var t = parseInt(readLine()); for (var a0 = 0; a0 < t; a0++) { var n = parseInt(readLine()); n >=1 && console.log(sumMul(Number(BigInt(n))).toString()); } } Use BigInt for testCase 2 and 6 for large number in javascript.
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #1: Multiples of 3 and 5
You are viewing a single comment's thread. Return to all comments →
const sumMul = (n) => { const _mul = (x) => { const _t = BigInt(Math.floor(Number((BigInt(n) - 1n) / BigInt(x)))); return (BigInt(x) * _t * (_t + 1n)) / 2n; }; return _mul(3) + _mul(5) - _mul(3 * 5); }; function main() { var t = parseInt(readLine()); for (var a0 = 0; a0 < t; a0++) { var n = parseInt(readLine()); n >=1 && console.log(sumMul(Number(BigInt(n))).toString()); } } Use BigInt for testCase 2 and 6 for large number in javascript.