I believe that the test cases have been luckily tailored to allow this solution. 9 mangos that cost 5 each cost 30 in total. But if you apply the above with 8 mangos costing 5 each you should get the same result (you are not taking advantage of 1 free mango) but instead you get a float which makes no sense because the result should always be a multiple of 5.
Damn I love this! (wouldn't actually want it in a codebase at work without comments but still).
For anyone confused here's the implicit reasoning:
If sieve is [false, false, true, false] then the even number marked by true is the one to pick out.
We can detect this with sieve.count(true) < 2
The neat thing is that the answer to this boolean is the same as the actual value we want to get the position of in the array. If it's true then we're looking for the one true, if not then it's the only false.
"Out of interest do you know if this kind of data structure has been used anywhere?"
I second that! Searched around the web for clues but found none so far.
Also wonder what's the optimal data structure for implementing it - some kind of DAG? I'd very much like to see a purely functional solution.
That's true but the description never promises more than "Define a function ... that takes one integer argument...". Don't mind me, I'm just a math nerd :)
Prime numbers are natural numbers greater than 1 (kata's description even mentions that), so there's no need to check divisors of negative numbers, just return false.
I'm not sure what you mean with your question :) While this:
quantity-quantity/3
means you drop every third mango from the calculation (because it's free by definition), your snippet - ((quantity / 3 ) * 2) * 2 - is not mathematically equivalent to the former (hint: integer division is not exactly like float division).
I like your solution best (dunno if it's most idiomatic, being Elixir newbie myself), it's most readable and goes the farthest (along with g964's) before it hits some Erlang integer minimum (namely, -1.797e+308) when calculating the partial sum (according to my naive test set):
test"fortune billion years, interest 1, inflation 1"doassert(
Bankerplan.fortune(100000, 1, 1000, 69525, 1) ==false
)
end
Interestingly enough, the solution using Stream gives up some ~500 years earlier :) (probably due to the suboptimal interest rate calculation)
why is it that this does pass the Kata but #((quantity / 3 ) * 2) * 2 * price will not do the trick if they return the same value when calculated in a scientific calculator? Just curious I am new here.
All tests pass green but I can't submit the solution - I can click the "Submit" button multiple times and it just runs the tests over and over again, not letting me proceed. :(
@fran_Cesco: No, that's not the case, read about integer division.
I believe that the test cases have been luckily tailored to allow this solution. 9 mangos that cost 5 each cost 30 in total. But if you apply the above with 8 mangos costing 5 each you should get the same result (you are not taking advantage of 1 free mango) but instead you get a float which makes no sense because the result should always be a multiple of 5.
Damn I love this! (wouldn't actually want it in a codebase at work without comments but still).
For anyone confused here's the implicit reasoning:
If sieve is [false, false, true, false] then the even number marked by true is the one to pick out.
We can detect this with sieve.count(true) < 2
The neat thing is that the answer to this boolean is the same as the actual value we want to get the position of in the array. If it's true then we're looking for the one true, if not then it's the only false.
"Out of interest do you know if this kind of data structure has been used anywhere?"
I second that! Searched around the web for clues but found none so far.
Also wonder what's the optimal data structure for implementing it - some kind of DAG? I'd very much like to see a purely functional solution.
That's true but the description never promises more than "Define a function ... that takes one integer argument...". Don't mind me, I'm just a math nerd :)
This comment is hidden because it contains spoiler information about the solution
Prime numbers are natural numbers greater than 1 (kata's description even mentions that), so there's no need to check divisors of negative numbers, just return false.
Not best Ruby practices, since it's using "for" and unnecessary "return", otherwise - nice.
I'm not sure what you mean with your question :) While this:
means you drop every third mango from the calculation (because it's free by definition), your snippet -
((quantity / 3 ) * 2) * 2
- is not mathematically equivalent to the former (hint: integer division is not exactly like float division).I like your solution best (dunno if it's most idiomatic, being Elixir newbie myself), it's most readable and goes the farthest (along with
g964
's) before it hits some Erlang integer minimum (namely,-1.797e+308
) when calculating the partial sum (according to my naive test set):Interestingly enough, the solution using
Stream
gives up some~500
years earlier :) (probably due to the suboptimal interest rate calculation)why is it that this does pass the Kata but #((quantity / 3 ) * 2) * 2 * price will not do the trick if they return the same value when calculated in a scientific calculator? Just curious I am new here.
Although it doesn't work for negative integers :)
All tests pass green but I can't submit the solution - I can click the "Submit" button multiple times and it just runs the tests over and over again, not letting me proceed. :(
This comment is hidden because it contains spoiler information about the solution