Computing Large Numbers and Squares
I'm just starting out Computer Science again and I'm working on my first assignment however, for some reason I can't get the numbers to work out because I know what should happen and what the output should be but the number is turning negative.
The issue I think is that since I have to use INT it's too big of a number? But I'm not entirely sure.
Squares: In the world of Mathematics, the following identity is valid for all positive integers N:
12 + 22 + 32 + ... + N2 = N(N + 1)(2N + 1)/6
However, when a math model is implemented as a computer program, the results often depend on what data types and algorithms are used in the program. Write a program that receives the positive integer N as command-line input. Use the int data type for all integer variables. Your program should calculate the values of two arithmetic expressions:
(a) LHS = 12 + 22 + 32 + ... + N2
(b) RHS = N(N + 1)(2N + 1)/6
Compile and run your program using the following input values:
N = 1000 N = 1250 N = 1500 N = 1750 N = 2000
Special questions: For each formula (LHS and RHS), what is the largest value of N that gives the correct output? Why are the two largest values different?
When I use num = 2000, the answer should be 26968667000, but my output that happens is LHS = -1626300296 & RHS = -194644530.
public static int LHS(int num)
for(int i=1; i<=num; i++)
sum = sum + (i*i);
System.out.print("LHS = ");
public static int RHS(int num)
int sum = num*(num + 1)*(2*num+1);
sum = sum / 6;
System.out.print("RHS = ");
Does anyone have any idea why this is? Thanks for any help you can offer.