Project Euler 06. Sum square difference with F#

Problem Definition

Available at Sum square difference

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.

Implementation

open System
let squareOfSum n =
let i = n*(n+1)/2
i*i
let sumOfSquare n =
[1..n]
|> List.fold (fun acc x -> acc + x*x) 0
let solveEuler6 N =
(squareOfSum N)- (sumOfSquare N)
view raw euler06.fs hosted with ❤ by GitHub

Project Euler 04. Largest Palindrome Product with F#

Problem Definition

Available at Largest Palindrome Product

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Implementation

We have to find out a palindrome p such that —

where both  a and b are  three digit number.  To do so, we first define a function that checks whether a  number (e.g., p) is a palindrome.

let ispalindrom (x:int):bool =
let s = x.ToString()
s = (s |> (fun x -> new string(x.ToCharArray() |> Array.rev)))
view raw ispalindrom.fs hosted with ❤ by GitHub

Then, we  iterate over all the tuples (a,b) of three digit numbers  e.g.,  [100..999]  that satisfy the  following equation.

Finally, we check if a*b is a palindrome and get the largest palindrome, as outlined below.

seq{
for x in 100..999 do
for y in x..999 do
if ispalindrom (x*y) then yield (x*y)}
|> Seq.max
view raw euler04.fs hosted with ❤ by GitHub

Would/did you solve it differently? Please let me know your opinion in the comment section below.

Happy problem solving …

problem solving

Euler Problem 002 with F#

Problem Statement

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.

Solutions

Recursive Solution:

// Recursive solution
let eulerProblem2Answer'' =
let maxF = 4000000
let rec computeSum f1 f2 sum =
match f1 + f2 with
| n -> computeSum f2 n sum
| n when n%2 = 0 -> if (n<maxF) then (computeSum f2 n (sum+n)) else sum
computeSum 0 1 0
view raw euler02.fsx hosted with ❤ by GitHub

Using infinite Fibonacci sequence:

let eulerProblem2Answer =
(0,1)
|> Seq.unfold (fun (f1,f2) -> Some(f2, (f2, f1+f2))) // Fibonacci Sequence
|> Seq.takeWhile(fun x -> x < 4000000) // Taking upto Max Fibbonacci
|> Seq.filter (fun x -> x%2=0) // Filtering only even numbers
|> Seq.sum // computing sum
view raw euler02.fsx hosted with ❤ by GitHub

A bit shorter version can be derived using Seq.SumBy:

let eulerProblem2Answer' =
(0,1)
|> Seq.unfold (fun (f1,f2) -> Some(f2, (f2, f1+f2)))
|> Seq.takeWhile(fun x -> x < 4000000)
|> Seq.sumBy (fun x -> if (x%2)=0 then x else 0) // using SumBy with a projection
view raw euler02.fsx hosted with ❤ by GitHub

Result

> 
val eulerProblem2Answer : int = 4613732
val eulerProblem2Answer' : int = 4613732
val eulerProblem2Answer'' : int = 4613732