This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Program | |
open System | |
let problem2 = // 4613732 | |
let mutable n1,n2,sum=1,2,0 | |
while n1<4000000 do | |
if n1%2=0 then | |
sum <- sum + n1 | |
n2 <- n1+n2 | |
n1 <- n2-n1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.IO | |
open System.Collections.Generic | |
open System.Linq | |
let fib = Seq.unfold(fun(a,b)->Some(a,(b,a+b))) | |
// defining tail optimized fibonacci sequence | |
let rec fib2(a,b)=seq{yield a; yield! fib2(b,a+b)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// recursion of pure function | |
let factorize (x:int64) = | |
let rec f (x:int64) m i= seq { | |
if x%m=0L then yield! f (x/m) m (i+1) | |
else | |
if i<>0 then yield (m,i) | |
if x<>1L then yield! f x (m+1L) 0 } | |
f x 2L 0 |> Seq.toList | |
/// nested while loop | |
let factorize2 (x:int64) = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* using unfold and reduce *) | |
let reverseDigit = | |
Seq.unfold(fun x->if x=0 then None else Some(x%10,x/10)) | |
>> Seq.toList | |
>> List.reduce(fun a b ->a*10+b) | |
(* using while loop *) | |
let reverseDigit2 x= | |
let mutable x,acc=x,0 | |
while x<>0 do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let primes = | |
let primes = new List<int>() | |
primes.Add 2 | |
seq { | |
yield 2 | |
for n in 3.. Int32.MaxValue do | |
if primes.All(fun p->n%p<>0) then | |
yield n | |
primes.Add n } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* Read Me | |
I'm solving project Euler problem for various ways. | |
I'm intensively examining how to write F# code efficiently. | |
Do you find better way to solve this problem? | |
Please teach me how to write or another advice. | |
*) | |
(* Result | |
Answer : 40824 | |
Answer : 40824 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ProjectEulerSolutions | |
open System | |
open System.Collections.Generic | |
open System.IO | |
open System.Linq | |
open ProjectEulerLib | |
let problem1 = [1..999] |> List.filter(fun x->x%3=0 || x%5=0) |> List.sum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ProjectEulerLib | |
open System | |
open System.IO | |
open System.Collections.Generic | |
open System.Linq | |
//let ( ** )(x:int64) n = | |
// let mutable r,n = 1L,n | |
// while n>0 do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
F# solution of below URL | |
http://gushwell.ifdef.jp/etude/RisingEmirp.html | |
*) | |
let isPrime n = | |
seq{yield 2;yield![3..2..int<<sqrt<|float n]} | |
|> Seq.forall(fun d->n%d<>0) | |
let rec revds a n= if n=0 then a else revds(a*10+n%10)(n/10) | |
let upNums = List.fold(fun ns d->List.collect(fun n->[n;n*10+d])ns)[0][1..9] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* Result | |
[[1; 3]; [1; 4]; [2; 3]; [2; 4]] | |
[[3; 1]; [4; 1]; [3; 2]; [4; 2]] | |
seq [[1; 3]; [1; 4]; [2; 3]; [2; 4]] | |
seq [[1; 3]; [1; 4]; [2; 3]; [2; 4]] | |
*) | |
(* | |
let rec cartesian5 (ls: seq<seq<'a>>) : seq<seq<'a>> = seq{..} | |
I couldn't make cartesian from sequence of sequence |