After I was denied on my first attempt, I replaced i^2 with i*i and sum^2 with sum*sum. I'm probably going to have to start using some sort of numerical package if I continue to program these in Java. Octave (the language I used to do my Numerical Computation assignments at CU) was not listed in the preferred language section of the profile, but since I only need to submit answers for these, I may switch to using Octave soon.
These have been way too easy so far. I'm looking forward to when they get harder.
Solution for Problem 6 below:
/*
* The sum of the squares of the first ten natural numbers is,
* 1^(2) + 2^(2) + ... + 10^(2) = 385
*
* The square of the sum of the first ten natural numbers is,
* (1 + 2 + ... + 10)^(2) = 55^(2) = 3025
*
* Hence the difference between the sum of the squares of the first ten natural
* numbers and the square of the sum is 3025 − 385 = 2640.
*
* Find the difference between the sum of the squares of the first one hundred
* natural numbers and the square of the sum.
*
*/
public class PE0006
{
public static void main(String[] args)
{
int sumOfTheSquares = 0;
int sum = 0;
for (int i = 1; i <= 100; i++)
{
sumOfTheSquares += i * i;
sum += i;
}
System.out.println(sum * sum - sumOfTheSquares);
}
}
On a different note, my Hibernate book is at the Loveland post office and should be delivered soon. I'll probably take a break from Project Euler and make posts related to the things I learn in Hibernate. I know the basics, and can get by with what I know for work, but I'm really interested in learning the more arcane aspects of Hibernate...