Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…powers
  • Loading branch information...
commit a142277a4b9ed0f388f3b26d6459c4c41b6e6ccb 1 parent c9df780
Nathan Broadbent authored

Showing 1 changed file with 27 additions and 0 deletions. Show diff stats Hide diff stats

  1. +27 0 6/solution.rb
27 6/solution.rb
... ... @@ -0,0 +1,27 @@
  1 +#!/usr/bin/env ruby
  2 +# coding: utf-8
  3 +# -----------------------------------------
  4 +# | A solution to a Project Euler problem |
  5 +# -----------------------------------------
  6 +require 'rubygems'
  7 +require 'pp'
  8 +problem_no = File.dirname(File.expand_path(__FILE__)).split('/').last
  9 +puts "=== Solving Project Euler problem ##{problem_no}\n\n"
  10 +question = %Q{
  11 +
  12 +The sum of the squares of the first ten natural numbers is,
  13 +1^(2) + 2^(2) + ... + 10^(2) = 385
  14 +
  15 +The square of the sum of the first ten natural numbers is,
  16 +(1 + 2 + ... + 10)^(2) = 55^(2) = 3025
  17 +
  18 +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.
  19 +
  20 +Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
  21 +
  22 +
  23 +}
  24 +puts "? #{question.strip}\n\n"
  25 +# --------------------------------------------------------------------
  26 +
  27 +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.
Something went wrong with that request. Please try again.