log2b.pl

loading
details
attribute value
description
owner Johannes Waldmann
uploaded 2017-08-17 03:45:07.0
disk size 705 Bytes
downloadable true
type
attribute value
name no_type
processor id 1
description this is the default benchmark type for rejected benchmarks and benchmarks that are not associated with a type.
owning community none
loading contents
%query: log2(i,o).
/* author: Michael Codish
   date:    March 2003

 log2(X,Y) :- Y equals the integer log base 2 of X.

 Namely, the largest integer smaller or equal to the 
 log base 2 of X
*/


log2(X,Y) :- log2(X,0,s(0),Y).


% log2(X,H,A,Y) :- computes Y equals the integer log base 2 of X.
% H accumulates "half" of X and A accumulates the number of times
% X was halved.

log2(s(s(X)),Half,Acc,Y) :- log2(X,s(Half),Acc,Y).

log2(X,s(s(Half)),Acc,Y) :- small(X), log2(Half,s(0),s(Acc),Y).

log2(X,Half,Y,Y) :- small(X),small(Half).

small(0). 
small(s(0)).


/*

    ?- log2(s(s(s(s(s(s(s(s(0)))))))), Y).
    Y = s(s(s(0)))

    ?- log2(s(s(s(s(s(s(s(s(s(s(0)))))))))), Y).
    Y = s(s(s(0)))

*/
popout

content may be truncated. 'popout' for larger text window.

actions get anonymous link download benchmark