1

我正在尝试创建一个程序来打印一个区间内有多少平滑数字。部分代码在这里:

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
4

1 回答 1

1

我真的很抱歉。我整天都在试图找出问题所在,最后我看到在同一个地方我写了“FilPrimes”,在其他一些地方写了“Filprimes”。

我真是个白痴!

于 2010-07-21T15:24:52.873 回答