# Euler Solution 6

### From ProgSoc Wiki

## Contents |

# Solutions for Problem 6

Work out the difference between 1^{2}+2^{2}+...+100^{2} and (1+2+...+100)^{2}.

## C by ctd

Runtime: 2ms

#include <stdio.h> #include <math.h> #define MAXIMUM_NUMBER 100 double sumbetween (int, int); int main () { double r1 = sumbetween(MAXIMUM_NUMBER, 1); double r2 = sumbetween(MAXIMUM_NUMBER, 0); r2 = r2 * r2; printf ("%.0f\n", fabs(r1-r2)); return 0; } double sumbetween (int i, int squared) { double sum = 0; for (; i > 0; i--) { if (squared) sum += (i * i); else sum += i; } return sum; }

## Caml by SanguineV

Runtime: 7.9ms

let diff ns = let sumofsquares = List.fold_left (fun x y -> x + (y*y)) 0 ns and sum = List.fold_left (fun x y -> x + y) 0 ns in (sum * sum) - sumofsquares ;; let rec nns n = match n with | 1 -> [1] | _ -> n :: (nns (n-1)) ;; Printf.printf "%d\n" (diff (nns 100));;

## Python by Althalus

Runtime: 0.0 seconds

import time start_time = time.time() sum=0 square=0 i=1 while i<=100: sum=(i*i)+sum i=i+1 i=1 while i<=100: square=i+square i=i+1 square=square*square print(square-sum) run_time = time.time() - start_time print (run_time)

## Haskell by SanguineV

Runtime: 4 ms

main = print ((\f -> (f (sum [1..100])) - (sum (map f [1..100]))) (\x -> x*x))

## Ruby by tomchristmas

Another one-line wonder in Ruby. Injection methods FTW!

Runtime: 25ms

puts ((1..100).inject(0){|x,y| x + y**2} - ((1..100).inject(0){|x,y| x + y})**2).abs

## VBA by mmaster

Runtime: fast

Sub ProblemSix() Dim i Dim j Dim k Dim l Dim m j = 0 k = 0 For i = 1 To 100 j = j + i k = k + i ^ 2 Next i l = j ^ 2 m = l - k MsgBox m End Sub