#J, 69 chars#
(([*[:+/[:%[:q:0-])`1:`(1:`([*[:+/[:%[:q:])@.([:*[:<:]))@.([:>:[:*]))
Sample input:
(([*[:+/[:%[:q:0-])`1:`(1:`([*[:+/[:%[:q:])@.([:*[:<:]))@.([:>:[:*])) 8
12
Notes:
Too many exceptions to handle...
#J, 69 chars#
(([*[:+/[:%[:q:0-])`1:`(1:`([*[:+/[:%[:q:])@.([:*[:<:]))@.([:>:[:*]))
Sample input:
(([*[:+/[:%[:q:0-])`1:`(1:`([*[:+/[:%[:q:])@.([:*[:<:]))@.([:>:[:*])) 8
12
Notes:
Too many exceptions to handle...