Project Euler #402 - number-theory

I'm surprised at the sheer number of questions from PE in here.
Well I don't want a solution, but I would like some sort of a hint in the right direction with problem 402.
I haven't been able to find a closed form solution for S(N). This is about as far as I got which is quite a loop.
Now I know a couple more things like that Fib(N) mod 1e9 is periodic. Look at Pisano Periods and S(N) has a period as well.

It's pretty easy to figure out how S(n) behaves once you fully understand M(a, b, c). So my suggestion is if you're looking for a closed form expression for S(n), find one for M(a, b, c) first.
Caveat: I figured out a good function for S(n) days ago. Once I did, I said to myself, "great, I got the hard part done." I was very wrong. I am having a huge amount of trouble wrapping my head around solving the rest of the problem.

Related

Problem in finding all prime factors of a number in prolog

I have written the following prolog code to find all the prime factors of a number.
find_factors(0).
find_factors(N):- N>0, isPrime(N), write(N),nl, S is N-1, find_factors(S).
where the predicate isPrime(N) checks whether a number is prime or not and returns true or false accordingly.
However with my logic, I always get false as output. Where is the problem?? I am very new to prolog programming, so I am struggling a little bit.
If further clarifications needed, drop a comment below.
I think, you should not start with prime factorization to learn Prolog, because your code does not suggest you have a clear idea of that problem. I have a hard time interpreting the intention/idea behind your code.
I recommend to start learning prolog by picking a problem that you already understand.
Then maybe start thinking about how a suitable predicate for your solution should look like.
In the case of prime factorization, it could be maybe
prime_factors(N, List), which is supposed to be provable if N is a number and List is the list of all of its prime factors.
Or it could be
prime_factor(N, F), which is supposed to be provable if N is a number and F is one of its prime factors.
In other words: what questions do you want to ask the prolog system?

How to make a maze in Prolog?

For a project, I have to write a basic maze in Prolog. The only problem is that I do not know how to write a KB that represents the maze in the below picture.
This is what I currently have, but I should be able to find pats from letter to letter.
% size of maze, including barriers.
mazeSize(7,7)
barrier(1,6).
barrier(2,2).
barrier(2,3).
barrier(3,2).
barrier(3,6).
barrier(4,1).
barrier(5,4).
barrier(5,6).
barrier(6,1).
barrier(7,4).
And this is what it should look like:
Hopefully someone can help me! Thanks in advance!
Your approach is reasonable, but you would also need facts something like node_name(1, 1, a) to map between node names and coordinates. Also, a 7x7 maze will clearly not correspond to the shown 4x4 maze.
A simpler solution that would not need coordinates at all would be one that only enumerates connected nodes, but not barriers:
connection(a, b).
connection(b, f).
and so on, but not connection(b, c) for example. Note that you will probably need to express that connections are two-way, so b and a are also connected.

Why does it say my predicate block/3 is not called?

So I've been learning Prolog, and to test myself I wanted to solve a certain puzzle. I think I'm pretty close to solving it, but I seem to be doing something wrong. It seems my predicate block/3 is not called.
maybe it's a syntax error, or I'm doing something that doesn't work in Prolog. I can't see it.
block/3 is supposed to give all possible combinations of sets in block/2.
I'm not sure if it's entirely relevant but I'll include the goal of the puzzle:
There's four cubes, with different combination of four images on their faces. (Kim,Lab,Hail and Com)
The goal is to align the cubes in such a way that if you put them together, all the sides next to each other should be the same. So it's four of the same rows going around each cube, and then two sides that should also be matching.
I wrote the program to just solve the rows, and disregarding the orientation of the images and the two sides. Should that give more than one answer it shouldn't give too many to manually try.
anyway, somehow solve(X) completely ignores my predicate block/3. I've been staring at it for a long time and I can't find the issue.
member( block(1, _, Row), X )
is equivalent to
E = block(1, _, Row), member( E, X )
so it does not in fact call block/3 as a predicate, it just uses it as a compound term, symbolically.

Prolog factorial predicate

I have a factorial predicatefact(N,F), where either N or F or both are bounded to a number.
For example I can have fact(3,F) or fact(N,6).
Here is my predicate, which works but I don't really understand how. I used trace but still have problems understanding it.
fact(0,1).
fact(N,F) :-
fact(N1,F1),
N is N1 + 1,
F is N * F1.
You can try to go through your program step-by-step to understand what's going on. This will be very slow indeed, and quite unreliable. Or, you can instead let Prolog do (part of) the work. So the idea is to modify the program a bit and then look what Prolog thinks about it.
This is what I see when I look at your program - this is called a failure-slice
fact(0,1) :- false.
fact(N,F) :-
fact(N1,F1), false,
N is N1 + 1,
F is N * F1.
When will this fragment terminate? Look at the remaining visible part! The N only occurs once in the head: nobody is interested in the first argument! Same for the F. Therefore: No matter what arguments you have, this program will not terminate. And thus the same holds for your original program!
In the original version this wasn't that clear. Watch out:
?- fact(29,F).
F = 8841761993739701954543616000000
At first this looks nice, but if you ask for the next answer (with SPACE or ;), you end up looping, i.e. waiting for an answer that never comes. Worse, false queries loop right away:
?- fact(29,1).
loops.
So how can you find these problems, without understanding precisely what is going on? This is what false is for. A goal that is never true. If you add it like fact(29,F), false. you will never be distracted by beautiful answers.
Why have you put all your arithmetics at the end? I suspect because you got some errors before. There is an easy way out to avoid all such errors:
:- use_module(library(clpfd)).
You can now write #= instead of is, and you need some restriction like N #>= 1. Can I leave you there?

Forward Chaining First Order Logic (Unification)

I'm studying for my final exam and I'm having trouble understanding this FC algorithm:
I understand it up to the part where you standardize each rule. Then I think the next line is saying for each theta that satisfied the Generalized Modus Ponens (p'_iTheta = p_iTheta), do... something. What is that something? I don't really understand what is happening after that part.
Any help is appreciated. Thanks for reading.
Basically, Theta is a substitution that can make some set of terms you know to be true (the p's in the KB) equal to the ps in the rule, then you can conclude that q' (q with the same Theta applied to it) is also true. If that q' isn't already known, then it goes into new; if it unifies w/ our query, then we've succeeded.

Resources