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

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 创建所有素数 2B.

例如,如果 B = 11, , 然后 FilPrimes = [2,3,5,7,11].

当我打电话时 countsmooth 在SWI-Prolog中就像?- 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

没有正确的解决方案

其他提示

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

我真是个白痴!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top