Problem 42935. Sums of cubes and squares of sums
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.
Solution Stats
Problem Comments

3 Comments
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.
Can someone point me in the right direction?
n = 1e5;
y_correct = uint64(500010000050000);
I get 500010000050005. I do not know how to go beyond double precision.
function y = cubesLessSquare(n)
% Formula
N = (((sum((1:n).^3)*2  sum(1:n)^2)*2/n))
% double to char
C = sprintf('%f',N)
% char to vector
Y = C  '0'
% take out the dot and the numbers after the dot
Dot = find(Y<0)
Y([Dot:end]) = []
% join the the numbers
Z = sprintf('%d',Y)
% make to double
y = str2num(Z)
end
I was trying to be clever by subtracting off sum(a) to keep sum(a^3) low. Imagine my disappointment when this turned out to be an algebra problem!
Solution Comments
Show commentsProblem Recent Solvers251
Suggested Problems

3799 Solvers

16501 Solvers

Find state names that start with the letter N
1197 Solvers

802 Solvers

1473 Solvers
More from this Author4
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!