Skip to content

Commit

Permalink
from a kitty
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgian committed Sep 26, 2012
1 parent 47d9b47 commit 1d67871
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions problem40.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
let int_pow a b = truncate ((float a) ** (float b));;

let digitize number =
let rec aux num acc =
match num with
| n when n<10 -> (n::acc)
| n -> let digit = n mod 10 in
aux (n/10) (digit::acc)
in
aux number [];;


let rec digcount diglst digsum numsum digs acc =
let nextnumsum = 9*(int_pow 10 (digs-1)) in
match diglst with
| [] -> acc
| x::xs when ((digsum<x) && (x<(digsum+digs*nextnumsum))) ->
let dig = x-digsum in
let num = numsum+(dig / digs)+1 in
let digno = dig mod digs in
begin
match digno with
| 0 -> digcount xs digsum numsum digs ((List.nth (digitize num) (digs-1))*acc)
| digno -> digcount xs digsum numsum digs ((List.nth (digitize num) (digno-1))*acc)
end
| x::xs when (x>(digsum+digs*nextnumsum)) ->
digcount diglst (digsum+digs*nextnumsum) (numsum+nextnumsum) (digs+1) acc
| _ -> failwith "You have failed"
;;

let solve = digcount [100;1000;10000;100000;1000000] 9 9 2 1;;

0 comments on commit 1d67871

Please sign in to comment.