006 Sum square difference.sf 445 B

12345678910111213141516171819202122
  1. #!/usr/bin/ruby
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
  6. # https://projecteuler.net/problem=6
  7. # Runtime: 0.148s
  8. func sum_pow_2(n) {
  9. 1/3 * [n**3, (3/2)*(n**2), (1/2)*n].sum;
  10. }
  11. func sum_pow_1(n) {
  12. 1/2 * [n**2, n].sum;
  13. }
  14. say int(sum_pow_1(100)**2 - sum_pow_2(100));