我正在尝试创建一个程序来打印一个区间内有多少平滑数字。部分代码在这里:
countsmooth(_, [], _, _, Count) :-
Count is 0.
countsmooth(X, [H|T], Min, Max, Count) :-
( Y is X*H,
Y =< Max
-> ( Y >= Min
-> NewX is X*H,
countsmooth(X, T, Min, Max, NCount1),
countsmooth(NewX, [H|T], Min, Max, NCount2),
Count is (1+NCount1+NCount2)
; NewX is X*H,
countsmooth(X, T, Min, Max, NCount1),
countsmooth(NewX, [H|T], Min, Max, NCount2),
Count is (NCount1+NCount2)
)
; Count is 0
).
smooth(B, I, J, Smooths) :-
( B =< 1
-> Smooths is 0
; I =:= 1
-> primes(B, FilPrimes),
countsmooth(1, Filprimes, I, J, Count),
Smooths is (1+Count)
; primes(B, FilPrimes),
countsmooth(1, Filprimes, I, J, Count),
Smooths is Count
).
还有一个谓词primes
可以创建从2
到的所有素数B
。
例如,如果B = 11
,那么FilPrimes = [2,3,5,7,11]
。
当我countsmooth
像
?- countsmooth(1, [2,3,5,7,11,13,17,19,23], 1, 100000000, Count)
. 我得到一个结果。
但是当我打电话时smooth
,我?- smooth(2,100,10000,Smooths).
收到以下错误:
ERROR: is/2: Arguments are not sufficiently instantiated