Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A solution to Project Euler Challenge #2, written in D.
- // <http://projecteuler.net/problem=2>
- // Used for printing the final answer.
- import std.stdio;
- void main() {
- // Declares and initializes square_sum (for the suqare of the sum of numbers 1 to 100), and suqare_each (the sum of the squares of each number 1 to 100)
- int square_sum, square_each = 0;
- // Defines square_each as the sum of the square of every number 1 to 100.
- foreach(i; 1 .. 100) square_each += (i * i);
- // Defines square_sum as the sum of al of the numbers 1 to 100.
- foreach(i; 1 .. 100) square_sum += i;
- // Squares the sum of all numbers 1 to 100.
- square_sum = square_sum * square_sum;
- // Calculates and prints the correct answer (with formatting).
- writeln("The difference between the sum of the squares of the first one hundred natural numbers and the square of the sum is ", square_sum - square_each, ".");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement