Skip to content

Instantly share code, notes, and snippets.

@mayomi1
Last active March 9, 2018 23:39
Show Gist options
  • Save mayomi1/82694120842866e2a841668f4fadebf2 to your computer and use it in GitHub Desktop.
Save mayomi1/82694120842866e2a841668f4fadebf2 to your computer and use it in GitHub Desktop.
NP-solutions
Question (1)
let sum = 0
for (let i = 0; i<= 1000; i++){
if(i % 3 === 0 || i%5 === 0) {
sum = sum + i;
}
}
console.log(sum)
Question 2)
let sum1 = 0;
let number = 1000;
for (let i = 1; i <= number; i++ ) {
sum1 = sum1+i
}
sum1 = sum1 * sum1;
let sum2 = 0
for(let j = 1 ; j <= number; j++ ){
sum2 = sum2 + j * j;
}
const differentInSum = sum1 - sum2;
console.log(differentInSum);
question 3;
question 4:
since there are 5 jars of pot and each of them weight 10g except 1 with 9g; then we can determine the 1 with 9g in one
measurement by subtract different amount of weight from each of them and sum it together and weigh it in a different jar
and multiple it by 10, it should give a multiple of 10 but since there's one with 9, then it will not give a multiple of 10
we can then track which jar is contaminated with that.
, for example we could label each of the jar A, B , C, D , E. and we remove 2 from A, 4 from B, 7 from C , 1 from D and
9 from E; if we add the removed weigth together , 2 + 4 + 7 + 1 + 9 = 23, if they multiple this by 10 it should by 230,
but since one the jars weigh 9g then the weight will by less than 230, if we weigh 228 for example it means the contaminated
jar is jar A, if the jar weigh 226 then the contaminated jar is B, if the jar weigh 223 then the contaminated jar is C
if the jar weigh weigh 229 then the contaminated jar is D, if the jar weigh 221 then the contaminated jar is E
since each of the others weigh 10g.
question 5;
function binaryGap(num) {
let bin = Math.abs(num).toString(2)
let max = 0;
let currentMax;
for (let i = 0; i < bin.length; i++) {
currentMax = 0;
while(bin[i] === '0'){
++currentMax && ++i;
}
if (bin[i] === '1'){
max = Math.max(max, currentMax);
}
}
return max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment