Given the positive integers 1:n, can you:
1. Compute twice the sum of the cubes of those numbers. 2. Subtract the square of the sum of those numbers. 3. Divide that result by n/2.
So, for n = 3, we might compute a result like this:
((1^3 + 2^3 + 3^3)*2 - (1 + 2 + 3)^2)/(3/2) ans = 24
Yes, you probably can do all of this, but be careful on this problem, as n may be somewhat large, and I am expecting to see the correct result, not just an approximate value. Remember there are always different ways one may solve a problem.
I point out the Project Euler reference because PE problem 6 is what made me think of this problem, and because the test cases will push the limits of what you can do if you are not careful.
I did not originally look to see exactly how far one could go. It looks like, IF one is careful in the important expression, you should be able to get to 3329020. I'd expect that the code I'd write that would solve this to go as far as 3329020 would employ an initial test to know if n is even or odd, changing the expression I'd write depending on the parity of n. Of course that branch would also increase the complexity, so raising the Cody score.
it is exactly the right way to solve this one.
works for n up to 3329020
works for n up to 2642245, so it doesn't use the full potential of the uint64
isequal ignores the data type of the values in determining whether they are equal.
isequal(uint64(15503197751395200),15503197751395199,15503197751395201,15503197751395200)
flintmax('double') = 2^53 < 15503197751395200
eps(15503197751395200) = 2
Big letters, small letters and a bit of luck.
522 Solvers
Back to basics 9 - Indexed References
338 Solvers
Matrix indexing with two vectors of indices
375 Solvers
115 Solvers
190 Solvers