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 # 2 – Solved with the Go programming language

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

One Possible Solution: Go

package main

import "fmt"

func fib(n int) int {
	thesum := 0
	a := 1
	b := 1
	for a < n {
		if a%2 == 0 {
			thesum = thesum + a
		}
		a, b = b, a+b

	}
	return thesum

}

func main() {
	fmt.Println(fib(4000000))
}

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

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

One Possible Solution: Java

public class Problem2 {
	public static void main(String args[]){
		int sum = 0;
		int a = 0;
		int b = 1;
		int c = a + b;
		while (c < 4000000){
			if (c % 2 == 0){
				sum = sum + c;
			}
			a = b;
			b = c;
			c = a + b;
		}
		System.out.println(sum);
	}

}

One Possible Soulution: Python

# Python version = 2.7.2
# Platform = win32

def fib(n):
    """Determines the sum of the even numbers in the fibonacci sequence
    up to value n"""
    thesum = 0
    a, b = 0, 1
    while a < n:
        if a % 2 == 0:
            thesum = (thesum + (a))
        a, b = b, a + b
    return thesum

def main():
    """The main program - calls the fib function and prints the value
    returned from the function --> x"""
    x = fib(4000000)
    print x
    
if __name__ == '__main__':
    main()