Advertisement
Guest User

Untitled

a guest
Jan 12th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.90 KB | None | 0 0
  1. //  A solution to Project Euler Challenge #2, written in D.
  2. //  <http://projecteuler.net/problem=2>
  3.  
  4. //  Used for printing the final answer.
  5. import std.stdio;
  6.  
  7. void main() {
  8.     //  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)
  9.     int square_sum, square_each = 0;
  10.     //  Defines square_each as the sum of the square of every number 1 to 100.
  11.     foreach(i; 1 .. 100) square_each += (i * i);
  12.     //  Defines square_sum as the sum of al of the numbers 1 to 100.
  13.     foreach(i; 1 .. 100) square_sum += i;
  14.    
  15.     //  Squares the sum of all numbers 1 to 100.
  16.     square_sum = square_sum * square_sum;
  17.    
  18.     //  Calculates and prints the correct answer (with formatting).
  19.     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, ".");
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement