Permalink
Browse files

Solved 6 - Learnt that ruby uses '**' instead of the assumed '^' for …

…powers
  • Loading branch information...
1 parent c9df780 commit a142277a4b9ed0f388f3b26d6459c4c41b6e6ccb @ndbroadbent committed Oct 13, 2010
Showing with 27 additions and 0 deletions.
  1. +27 −0 6/solution.rb
View
27 6/solution.rb
@@ -0,0 +1,27 @@
+#!/usr/bin/env ruby
+# coding: utf-8
+# -----------------------------------------
+# | A solution to a Project Euler problem |
+# -----------------------------------------
+require 'rubygems'
+require 'pp'
+problem_no = File.dirname(File.expand_path(__FILE__)).split('/').last
+puts "=== Solving Project Euler problem ##{problem_no}\n\n"
+question = %Q{
+
+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.
+
+
+}
+puts "? #{question.strip}\n\n"
+# --------------------------------------------------------------------
+
+puts ((1..100).inject(0){|r,i| r += i} ** 2) - (1..100).inject(0){|r,i| r += (i ** 2)}

0 comments on commit a142277

Please sign in to comment.