Created
March 14, 2015 13:19
-
-
Save plasma-effect/0e0483f7b6d3179cfc82 to your computer and use it in GitHub Desktop.
Project Euler F#
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
//problem 1 | |
let rec problem1 n = | |
match n with | |
| 0 -> 0 | |
| i -> if (i%3 = 0)||(i%5 = 0) then i + problem1(n-1) else problem1(i-1) | |
//problem 2 | |
let rec problem2 first second = | |
if first > 4000000 | |
then 0 | |
else if first%2 = 0 | |
then first + (problem2 (first+second) first) | |
else problem2 (first+second) first | |
//problem 3 | |
let rec problem3 (n:int64) (p:int64) = | |
if n = p | |
then n | |
else if n%p = 0L | |
then problem3 (n/p) p | |
else problem3 n (p+2L) | |
//problem 4 | |
let rec problem4il n = | |
if n < 10 | |
then 0 | |
else 1 + (problem4il (n/10)) | |
let rec problem4i n m = | |
if (problem4il n) <> m then false | |
else if n < 10 | |
then true | |
else | |
let t = (pown 10 (problem4il n)) | |
if (n%10) = (n/t) | |
then problem4i (((n - (n/t)*t))/10) (m-2) | |
else false | |
let rec problem4 first second= | |
if second = 1000 | |
then 0 | |
else if first | |
= 1000 | |
then problem4 (second+1) (second+1) | |
else if problem4i (first*second) (problem4il (first*second)) | |
then max (problem4 (first+1) second) (first*second) | |
else problem4 (first+1) second | |
//problem 5 | |
let rec problem5i n i = | |
if i = 0 then true | |
else (n%i = 0) && (problem5i n (i-1)) | |
let rec problem5 n = | |
if problem5i n 20 then n else problem5 (n + 2) | |
[<EntryPoint>] | |
let main argv = | |
System.Console.WriteLine(problem1 999) | |
System.Console.WriteLine(problem2 2 1) | |
System.Console.WriteLine(problem3 600851475143L 3L) | |
System.Console.WriteLine(problem4 100 100) | |
System.Console.WriteLine(problem5 20) | |
0 // 整数の終了コードを返します |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment