Project Euler 1 - Multiples of 3 or 5 - Solved with Matlab

The first Project Euler problem - Multiples of 3 and 5 - is stated as follows. Find the sum of all the multiples of 3 or 5 below 1000.

% Create a range from 1 to 999.
range = [1:999];

% Find all values that are multiples of 3 and/or 5.
multiples = find(mod(range,3) == 0 | mod(range,5) == 0);

% Sum multiples and echo.
sum(multiples)

Project Euler 3 - Largest Prime Factor - Solved with C#

The third Project Euler problem - Largest Prime Factor - is stated as follows. What is the largest prime factor of the number 600851475143?

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
	public static IEnumerable<long> Primes()
	{
		// Create a range between 2 and the closest we can get Infinity.
		return Enumerable.Range(2, Int32.MaxValue - 1)
			// Get all values which are only divisible by themself and 1.
			.Where(x => Enumerable.Range(2, x-2).All(y => x % y != 0))
      		// Cast the values to long.
			.Select(n => (long)n);
	}

	public static IEnumerable<long> PrimeFactors(long input)
	{
		// Get all primes...
		long next = Primes()
			// ... below the square root of input.
			.TakeWhile(x => x <= Math.Sqrt(input))
			// Find the first prime that input is divisble by.
			.FirstOrDefault(x => input % x == 0);

		if(next == default(long)){
			// If we can't find a new factor, then input is the last factor.
			return new long[]{input};
		} else {
			// Add the found factor to the list and recurse.
			return new long[]{next}.Concat(PrimeFactors(input / next));
		}
	}

	public static void Main()
	{
		long factor = PrimeFactors(600851475143).Max();
		Console.WriteLine(factor);
	}
}

Project Euler 2 - Even Fibonacci Numbers - Solved with C#

The second Project Euler problem - Even Fibonacci Numbers - is stated as follows - By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

using System;
using System.Linq;

public class Program
{
	// Returns true if if N is a perfect square,
	// i.e. N can be expressed as the product
	// of two equal integers.
	static bool isPerfectSquare(long x)
	{
		long s = (long)Math.Sqrt(x);
		return (s * s == x);
	}

	// Returns true if N is in the Fibonacci sequence.
	static bool isFibonacci(long n)
	{
		// M is Fibonacci if and only if one or both of
		// 5*n*n + 4 or 5*n*n - 4 is a perfect square.
		// https://en.wikipedia.org/wiki/Fibonacci_number#Recognizing_Fibonacci_numbers
		return isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4);
	}

	// Returns true if N is even.
	static bool isEven(int n)
	{
		return n % 2 == 0;
	}

	public static void Main()
	{
		// Generate a range from 1 to 4 million.
		long sum = Enumerable.Range(1, 4000000)
			// Take only fibonacci numbers...
			.Where(n => isFibonacci((long)n))
			// ...that are even.
			.Where(isEven)
			.Sum();

		Console.WriteLine(sum);
	}
}

Project Euler 1 - Multiples of 3 or 5 - Solved with C#

The first Project Euler problem - Multiples of 3 and 5 - is stated as follows. Find the sum of all the multiples of 3 or 5 below 1000.

using System;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		long sum = Enumerable.Range(1, 999)
			.Where(n => (n % 3 == 0 || n % 5 == 0))
			.Sum();
		Console.WriteLine(sum);
	}
}

Project Euler 2 - Even Fibonacci Numbers - Solved with F#

The second Project Euler problem - Even Fibonacci Numbers - is stated as follows - By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

// Generate a sequence thar produces the Fibonacci numbers.
// Fn = Fn-1 + Fn-2
let sequence = Seq.unfold (fun (a,b) -> Some( a+b, (b, a+b) ) ) (0,1)

let sum = 
    sequence
    // Keep even numbers.
    |> Seq.filter (fun n -> n % 2 = 0)
    // Only take the values below four million.
    |> Seq.takeWhile (fun n -> n < 4000000)
    // Return the sum of the elements in the sequence. 
    |> Seq.sum

printfn "%i" sum