Skip to content

Instantly share code, notes, and snippets.

@mebsout
Created September 4, 2014 11:35
Show Gist options
  • Save mebsout/9550734e59e44aeebbc4 to your computer and use it in GitHub Desktop.
Save mebsout/9550734e59e44aeebbc4 to your computer and use it in GitHub Desktop.
Gas prices palindromes finder ;)
let is_palindrome f =
let s = Format.sprintf "%.2f" f in
let pos_dot = String.index s '.' in
String.blit s (pos_dot + 1) s pos_dot 2;
let s = String.sub s 0 (String.length s - 1) in
let l, u = ref 0, ref (String.length s - 1) in
while !l < !u && s.[!l] = s.[!u] do
incr l; decr u;
done;
!l >= !u
let find_palindromes l_min l_max price =
let l = ref l_min in
while !l <= l_max do
if is_palindrome !l && is_palindrome (!l *. price) then
Format.printf "Litres : %.2f --- Prix : %.2f@." !l (!l *. price);
l := !l +. 0.01;
done
let () =
try
let l_min = float_of_string Sys.argv.(1) in
let l_max = float_of_string Sys.argv.(2) in
let price = float_of_string Sys.argv.(3) in
find_palindromes l_min l_max price
with Invalid_argument _ | Failure _ ->
Format.eprintf "Usage: %s l_min l_max price@." Sys.argv.(0)
(*
Local Variables:
compile-command: "ocamlopt.opt -o gaspal gaspal.ml"
End:
*)
@backtracking
Copy link

Excellent!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment