Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Computing Large Numbers and Squares

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
09-09-2013, 07:01 AM   PM User | #1
tjgibbs92
New to the CF scene

Join Date: May 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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.

Quote:
 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?
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.

Code:
```public static int LHS(int num)
{
int sum=0;

for(int i=1; i<=num; i++)
{
sum = sum + (i*i);
}

System.out.print("LHS = ");
return sum;
}

public static int RHS(int num)
{
int sum = num*(num + 1)*(2*num+1);
sum = sum / 6;
System.out.print("RHS = ");
return sum;

}```
When I use num = 2000, the answer should be 26968667000, but my output that happens is LHS = -1626300296 & RHS = -194644530.

Does anyone have any idea why this is? Thanks for any help you can offer.

 09-09-2013, 06:32 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,599 Thanks: 4 Thanked 2,597 Times in 2,566 Posts Yep, that's correct. Integers are 32-bits in size, and java does not allow you to specify as an unsigned integer, so the maximum size you have available is 1 << 31 - 1 or 2,147,483,647. Any number larger than this uses the 32nd bit, which dictates the signed of the number and it becomes negative (see two's complement notation for information on this) effectively counting up towards 0. This is integer overflow. I can't give the answer for the special question though as that would defeat the purpose. You can write an algorithm that effectively converges a known high and a known low though. Choose a divide and conquer approach and it can calculate the max correct in both the algorithms. They have potential for difference since one utilizes a division which may at one point truncate the values when stuffing the float into an int. __________________ PHP Code: ``` header('HTTP/1.1 420 Enhance Your Calm');  ```

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 10:22 PM.