Question

I'm trying to create a program that prints how many smooth numbers are within an interval. A part of the code is here:

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
   ).

There is also a predicate primes that creates all prime numbers from 2 to B.

For example, if B = 11, then FilPrimes = [2,3,5,7,11].

When I call countsmooth in SWI-Prolog like ?- countsmooth(1, [2,3,5,7,11,13,17,19,23], 1, 100000000, Count). I get a result.

But when I call smooth like ?- smooth(2,100,10000,Smooths). I get the following error:

ERROR: is/2: Arguments are not sufficiently instantiated

No correct solution

OTHER TIPS

I'm really sorry. I have been trying all day to find out what was going wrong and finally I saw that in same places I had written "FilPrimes" and in some other places "Filprimes".

I'm such an idiot!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top