Project Euler – Problem #’s 1, 2, 3, 4, 5, 6, 7 – Solved with Javascript

js

Project Euler – Problem # 1 – Solved with Javascript:

Project Euler – Problem # 2 – Solved with Javascript:

Project Euler – Problem # 3 – Solved with Javascript:

Project Euler – Problem # 4 – Solved with Javascript:

Project Euler – Problem # 5 – Solved with Javascript:

Project Euler – Problem # 6 – Solved with Javascript:

Project Euler – Problem # 7 – Solved with Javascript:

Advertisements

Project Euler – Problem # 6 – Solved with Go

What is the difference between the sum of the squares and the square of the sums?

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)2 = 552 = 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.

One Possible Solution: Go

package main

import "fmt"
import "math"

var (
	sum_of_squares	float64	= 0
	square_of_sum	float64	= 0
	Sum		float64	= 0
)

func main() {
	for i := 1; i < 101; i++ {
		square := math.Pow(float64(i), 2)
		sum_of_squares += square
		Sum += float64(i)
	}
	square_of_sum = math.Pow(float64(Sum), 2)
	fmt.Println("sum_of_squares = ", int(sum_of_squares))
	fmt.Println("square_of_sum = ", int(square_of_sum))
	Difference := square_of_sum - sum_of_squares
	fmt.Println("Difference = ", int(Difference))

}

Project Euler – Problem # 6 – Solved with Java & Python

What is the difference between the sum of the squares and the square of the sums?

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)2 = 552 = 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.

One Possible Solution: Java


public class Problem_06 {
	public static void main(String args[]){
		int sum_of_squares = 0;
		int square_of_sum = 0;
		int Sum = 0;
		for(int i = 1; i < 101; i++){
			int square = (int)Math.pow(i, 2);
			sum_of_squares += square;
			Sum += i;
		}
		square_of_sum = (int)Math.pow(Sum, 2);
		int Difference = square_of_sum - sum_of_squares;
		System.out.println("sum_of_squares = " + sum_of_squares);
		System.out.println("square_of_sum = " + square_of_sum);
		System.out.println("Difference is = " + Difference);
	}

}

One Possible Solution: Python

# Python version: 2.7.2
# Platform = win32

def main():
    """Finds the difference between the sum of the
    squares of the first one hundred natural numbers
    and the square of the sum."""
    sum_of_squares = 0
    square_of_sum = 0
    Sum = 0
    for i in range(1, 101):
        square = i ** 2
        sum_of_squares += square
        Sum += i
    square_of_sum = Sum ** 2
    print "sum_of_squares = ", sum_of_squares
    print "square_of_sum = ", square_of_sum
    Difference = square_of_sum - sum_of_squares
    print "Difference = ", Difference

if __name__ == '__main__':
    main()