Last active
July 31, 2018 13:25
-
-
Save mathiasose/c27010b1dfb417803f82789293ad09df to your computer and use it in GitHub Desktop.
Trying ReasonML
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
/* | |
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. | |
*/ | |
let rec evenFibSum = (max_value, prev_num, prev_prev_num, even_sum) => { | |
let new_num = prev_num + prev_prev_num; | |
if (new_num > max_value) { | |
even_sum; | |
} else { | |
let new_sum = switch new_num { | |
| _ when (new_num mod 2 == 0) => even_sum + new_num | |
| _ => even_sum | |
}; | |
evenFibSum(max_value, new_num, prev_num, new_sum); | |
} | |
}; | |
Js.log(evenFibSum(4000000, 0, 1, 0)); |
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
/* | |
The prime factors of 13195 are 5, 7, 13 and 29. | |
What is the largest prime factor of the number 600851475143 ? | |
*/ | |
let divisible_by = (dividend, divisor) => (dividend mod divisor) == 0; | |
let rec prime_factors = (n, i, l) => { | |
switch i { | |
| _ when (n == i) => [n, ...l] | |
| _ when (n <= 2) => l | |
| _ when divisible_by(n, i) => prime_factors(n / i, 2, [i, ...l]); | |
| 2 => prime_factors(n, 3, l); | |
| _ => prime_factors(n, i + 2, l); | |
}; | |
}; | |
Js.log(prime_factors(87625999, 2, [6857])); /* the proper input number is bigger than an int32 :O */ |
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 rec fibonacci = fun(n) => switch n { | |
| _ when (n < 2) => 1 | |
| _ => (fibonacci (n - 1)) + (fibonacci (n - 2)) | |
}; | |
for (x in 1 to 10) { | |
Js.log(fibonacci(x)); | |
} |
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 fizzbuzz = (n) => switch n { | |
| _ when (n mod 15) == 0 => "FizzBuzz" | |
| _ when (n mod 3) == 0 => "Fizz" | |
| _ when (n mod 5) == 0 => "Buzz" | |
| _ => string_of_int(n) | |
}; | |
for (x in 1 to 20) { | |
Js.log(fizzbuzz(x)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment