'Project Euler #1 in JavaScript on HackerRank
I'm having some trouble with the test cases for Project Euler #1 on HackerRank and was hoping someone with some JS experience on HackerRank could help out.
Below is my code. As far as I can tell, I can't spot any obvious logic errors so I would appreciate if someone could shed some light on why I'm not passing all the test cases and why the status is "abort called". All the code above "ignore above this line" is provided by HackerRank and is not part of the solution.
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function threeFiveMultiples(num) {
let array = [];
for (let i = 0; i < num; i++) {
if (i % 3 === 0 ||
i % 5 === 0) {
array.push(i);
}
}
return array.reduce(function(accum, currVal) {
return accum + currVal;
});
}
function main() {
var t = parseInt(readLine());
for(var a0 = 0; a0 < t; a0++){
var n = parseInt(readLine());
let res = threeFiveMultiples(n);
console.log(res);
}
}
I've also tried using a BigNumbers library (see below).
process.stdin.resume();
process.stdin.setEncoding('ascii');
var BigNumber = require('bignumber.js');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function threeFiveMultiples(num) {
let array = [];
for (let i = 0; i < num; i++) {
if (i % 3 === 0 ||
i % 5 === 0) {
array.push(i);
}
}
return parseInt(array.reduce(function(accum, currVal) {
accum = new BigNumber(accum);
currVal = new BigNumber (currVal);
return accum.plus(currVal);
}));
}
function main() {
var t = parseInt(readLine());
for(var a0 = 0; a0 < t; a0++){
var n = parseInt(readLine());
let res = threeFiveMultiples(n);
console.log(res);
}
}
UPDATE:
Per the suggestion of @5ar, I replaced my reduce function with the following; however, test cases #2 and #3 are still failing:
return array.reduce(function(accum, currVal) {
return accum.plus(currVal);
}, new BigNumber(0)).toString();
Solution 1:[1]
Your code is fine. If you simply call console.log(threeFiveMultiples(1000));, it will give you the solution. Not sure what all the other code is about, but that will work.
Solution 2:[2]
Try to use Math.round()
on your values (after summing them), JavaScript treats all numbers as doubles so some may not be showing correctly.
If big numbers are the problem, try to find a library that you can use that can handle big numbers. If you can't, you can implement them yourself via strings, considering that you only need summing it shouldn't be that difficult.
UPDATE:
I think the return might be the problem (you're using parseInt
which probably converts the BigNumber to a String and then tries to parse it as a Number), try to do this:
return array.reduce(function(accum, currVal) {
return accum.plus(currVal);
}, new BigNumber(0)).toString();
Solution 3:[3]
The problem is that for really large numbers the calculation takes up a quite significant amount of time and hackerrank has a timeout of 10s
for the calculation. You have to find a faster way of calculation the correct output.
A small hint: Arithmetic progression (its possible in O(1)
)
Solution 4:[4]
There are two possible reasons why your code is failing
1) Timeout error The code logic you have written iterates each number and checks if it is divisible by 3 or 5. This logic causes the time out error when the number in the testcase is huge number.
2) Number Cannot be represented in JS The number used in Test Case 3 & Test Case 2 is a big number. So using a library like 'bignumber.js' is necessary, since JS does not support long integers.
Below is the code snippet which will help pass all the Test cases:
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var BigNumber = require('bignumber.js');
var t = new BigNumber(readLine()).toNumber();
var n;
for (var a0 = 0; a0 < t; a0++) {
n = new BigNumber(readLine());
sumOfNumberDivisibleBy3and5(n);
}
}
function sumOfNumberDivisibleBy3and5(n) {
const a = n.minus(1).dividedBy(3).floor();
const b = n.minus(1).dividedBy(5).floor();
const c = n.minus(1).dividedBy(15).floor();
const sumThree = a.times(3).times(a.plus(1)).dividedBy(2);
const sumFive = b.times(5).times(b.plus(1)).dividedBy(2);
const sumFifteen = c.times(15).times(c.plus(1)).dividedBy(2);
const sumOfAll = sumThree.plus(sumFive).minus(sumFifteen);
console.log(sumOfAll.toString());
}
Solution 5:[5]
This worked for me
function main() {
var t = parseInt(readLine());
for(var a0 = 0; a0 < t; a0++){
var n = parseInt(readLine());
/////////////////they forgot to tell us to put code below here
let N=BigInt(n)-BigInt(1)
let i3=BigInt(N)/BigInt(3)
let i5=BigInt(N)/BigInt(5)
let i15=BigInt(N)/BigInt(15)
let m3=BigInt(3)*BigInt(i3)*(BigInt(i3)+BigInt(1))
let m5=BigInt(5)*BigInt(i5)*(BigInt(i5)+BigInt(1))
let m15=BigInt(15)*BigInt(i15)*(BigInt(i15)+BigInt(1))
let sum=(BigInt(m3)+BigInt(m5)-BigInt(m15))/BigInt(2)
console.log(sum.toString())
/////////////////they forgot to tell us to put code above here
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Marco Principio |
Solution 2 | |
Solution 3 | |
Solution 4 | Ivix4u |
Solution 5 | V1N1V |