Skip to content

Instantly share code, notes, and snippets.

@Nicknyr
Created February 9, 2021 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicknyr/def74c2e1634ed807281f9f3ff5b95b0 to your computer and use it in GitHub Desktop.
Save Nicknyr/def74c2e1634ed807281f9f3ff5b95b0 to your computer and use it in GitHub Desktop.
CodeSignal: Magic Well
/*
You are standing at a magical well. It has two positive integers written on it: a and b. Each time you cast a magic marble into the well, it gives you a * b dollars and then both a and b increase by 1. You have n magic marbles. How much money will you make?
Example
For a = 1, b = 2, and n = 2, the output should be
magicalWell(a, b, n) = 8.
You will cast your first marble and get $2, after which the numbers will become 2 and 3. When you cast your second marble, the well will give you $6. Overall, you'll make $8. So, the output is 8.
*/
function magicalWell(a, b, n) {
let total = 0;
for(let i = 0; i < n; i++) {
total = a * b + total;
a++;
b++;
}
return total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment