In a past lesson you used the % (or mod) operator to test numbers for divisibility. The mod operator makes this easy, since if it's zero, it means a number divided into another with no remainder. And, no remainder means the original number is divisibile by the number being tested for divisibility.
Let's look again at divisibility using Prolog. This approach will allow us to think more about the logic of divisibility, rather than just blindly using the power of the % operator.
Move the mouse over a dotted box for more information.
We'll use a list here to contain the digits of a 7-digit number that you want to test for divisibility.
Now you try. Test a few numbers for divisibility
Type your code here:
See your results here:
In the goal: always put the digits of your number into a list, and be sure you always have 7-positions in thre list. So, if you want to test 6 for divisibility, put it in as [0,0,0,0,0,0,6]. If you want to test 674,434, put it in as [0,6,7,4,4,3,4].
When done, see if you can continue implementing divisibility rules from Wikipedia's page, starting with 6. (And note to implement 6, reuse the rules already implemented for 2 and 3.)
See how easy Prolog's list structure makes accessing the digits of a number in the divisible_by clauses? Just fill in an (uppercase) variable where you need to access a digit, and a _ when you don't!
The underbar, or _ is the "anonymous" or "I don't care variable."
Share your code
Show a friend, family member, or teacher what you've done!