-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem36.fsx
More file actions
31 lines (23 loc) · 738 Bytes
/
problem36.fsx
File metadata and controls
31 lines (23 loc) · 738 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(* Project Euler Problem 36
* By Weisi Dai <weisi@x-research.com>
*)
let ubound = 1000000
let isPalindrome (str: string) =
let arr = str.ToCharArray()
arr = Array.rev arr
let dec2bin x =
let rec div x current =
if x = 0 then current else
div (x / 2) ((x % 2) :: current)
div x []
|> List.map (fun digit -> digit.ToString())
|> String.concat ""
let testNumber x =
let decStr = x.ToString()
let binStr = dec2bin x
(isPalindrome decStr) && (isPalindrome binStr)
let palindromes = seq { 1 .. ubound }
|> Seq.filter testNumber
let problem36 = Seq.sum palindromes
let main = printfn "The answer is %d." (problem36)
main