recursive sum using prolog - recursion

I have a question about how can I Sum recursively using prolog, I been trying to do it, but it not count the first element I mean for example if a select the distance between cdmx and nuevoleon it only sums 6 and 7 but 3 and 5 they get lost someone knows how can I sum recursively the distance? thanks so much :D
distance(cdmx,michoacan,3).
distance(michoacan,jalisco,5).
distance(jalisco,durango,6).
distance(durango,nuevoleon,7).
connected(X,Y,Distance):-
distance(X,Y,Distance).
connected(X,Y,Distance):-
distance(X,Z,Distance1),
connected(Z,Y,Distance2),
Sum is Distance1+Distance2,
write(Sum).`

You were so close. In your second connected predicate you just needed to set the value of Distance.
connected(X,Y,Distance):-
distance(X,Y,Distance).
connected(X,Y,Distance):-
distance(X,Z,Distance1),
connected(Z,Y,Distance2),
Distance is Distance1 + Distance2.
Now the query ?- connected(cdmx,nuevoleon,Distance). works fine and results in Distance = 21.

Related

Using python to find the limit of a recusive function

Assume I had the following iterative fuction:
f(z) = z^2 + c
z initally equal to 0
and each answer of the function becomes z for the next iteration. i.e. if c is 1 then the fist iteration gives 1, the second gives 2 and so fourth.
Now assuming I already set a value for c, I would like to be able to use Python to find the limit as this function approaches an infinite number of iterations. How would I best be able to do that? Would Sympy be a good tool?
editied to clearify what I man by iterative function.

Calculating the index of an element in non-repetitive permutation

The following question is about math. The matter is, how to calculate the index of an element in a non-repetitive permutation. Example,
A={a,b,c} The permutation is then 3!=6 therefore: (a,b,c);(a,c,b);(b,a,c);(b,c,a);(c,a,b);(c,b,a)
I researched for algorithm to get the index of an element in this permutation. In internet there are only repetitive permutation algorithms.
The index of (b,c,a) is in this zero-based list, obviously 3. Is there an easy way to calculate the position directly by formula ?
I do not need the itertools from python. Because i use very large permutations.(Example 120!) I messed once with python's itertools' permutations function to get the index of an element over the list iterator. But the results were weary. I need a mathematical solution to get the index directly.
Thanks for reading.
Some clues:
You have n! permutations. Note that (n-1)! permutations start from the first element (a), next (n-1)! permutations start from the second element (b) and so on.
So you can calculate the first term of permutation rank as (n-1)! * Ord(P[0]) where Ord gives ordering number of the first element of permutation in initial sequence (0 for a, 1 for b etc).
Then continue with the second element using (n-2)! multiplier and so on.
Don't forget to exclude used elements from order - for your example b is used, so at the second stage c has index 1 rather 0, ad rank is 2!*1 + 1!*1 + 0! * 0 = 3

Find a function between two arrays that minimises distance between pairs

I will explain my problem in general setting (as I am interested in a general algorithm), then decline it to my particular case.
Say we have two finite sets, A and B, both subsets of X and a distance function d that assigns a distance between any two points of X.
What is an algorithm to find two functions: f1 from A to B and f2 from B to A such that f1(a) is the element in B that is closest to a and the same viceversa for f2.
My special case is in R language, where I have two sets of points on earth (lat, lon) and I need to pair them up (from A to B and viceversa) according to their distance.
For reference, I am using the Haversine distance from geosphere package.
Thanks in advance.
Just mentioning, this is an algorithmic solution for an algorithmic problem.
Lets begin with a solution in O(n^2) time and memory complexity. For each element in A remember the distance from each element in B. Then iterate over this 2 dimensional array and for each row find its minimum - these elements are the image of f1, f2 is always the reverse function from f1.
Now we can create a similar solution in O(n log n) time complexity and O(n) memory complexity. Using a binary search.
Let's sort the elements in A in a way we can say what is the closest item to some item out of the set in O(log n). With numbers it can be done just by sorting them, with lon & lat you just need to sort them first by lon than by lat.
Now for each element in A search what is the closest item in B using binary search. It will take O(log n) per question. Now for each element we know which is the closest. O(n log n).

Set Theory & Geometry: Two arcs on the same circle overlap with wrapping values

As a background, I'm a computer programmer and I'm working on a software library that allows a computer to quickly search through all dates to find a set of dates that satisfies a criteria. For example:
I want a list of every possible time that has ever occurred that has occurred on a friday or a saturday that is in April or May during the first week of the month.
My library uses numerical sets to efficiently represent ranges of dates that satisfy a criteria.
I've been thinking about ways to improve the performance of some parts of the app and I think that by combining sets and some geometry, I can really improve my results. However, my geometry is a bit rusty and I was hoping you might could help.
Here's my thought:
Certain elements of time can be represented as a circular dial. For example, Minutes can be positioned on a clock with values between 0...59. We could store valid ranges as a list of arcs. For example, If we wanted all times that ended with 05..10, we could store [5,10]. If we wanted all times that end with :45-59 or :00-15, we could store [45, 15]. Notice how this last arc "loops around" the dial. Here's a mockup showing different ranges intersecting on a dial
My question is this:
Given a set of whole numbers between N...M arranged into a circle.
Given Arc1 which is representing by [A, B] and Arc2 which is represented by [C, D] where A, B, C, and D are all within in range N...M
How do I determine:
A. Whether the arcs intersect.
B. If they do, what their intersection is.
C. If they do, what their union is.
Thank you so much for your help. If you're not able to help, if you can point me in the right direction, that would be great.
Thanks!
A simple and safe approach is to split the intervals that straddle 0. Then you perform pairwise interval intersection/union (for instance if A < D and C < B then [max(A,C), min(B,D)] for the intersection), and merge them if they meet at 0.
It seems the primitive operation to implement would be something like 'is the number X contained in the arch [A,B]'. Once you have that, you could implement an [A,B]/[C,D] arch-intersection predicate by something like -
Arch intersection means exactly that at least one of the following conditions is met:
C is contained in [A,B]
D is contained in [A,B]
A is contained in [C,D]
B is contained in [C,D]
One way to implement this contained-in-arch test without any branches is with some trigonometry and vector cross product. Not sure it would be faster (the math/branches performance tradeoff is entirely empiric), but it might be worth a try.
Denote Xa = sin(X/N * 2PI), Ya = cos(X/N * 2PI) and similarly for Xb,Yb etc.
C is contained in [A,B] is equivalent to:
Xa * Yc - Ya * Xc > 0
AND
Xc * Yb - Yc * Xb > 0
You can complete the other 3 conditions in an identical manner.
Hope this turns out useful.

Program asked in a online hiring challenge

Given N integers in the form of Ai where 1≤i≤N, the goal is to find the M that minimizes the sum of |M-Ai| and then report that sum.
For example,
Sample Input: 1 2 4 5
Sample Output: 6
Explanation: One of the best M′s you could choose in this case is 3.
So the answer = |1−3|+|2−3|+|4−3|+|5−3| = 6.
The approach I used is sort the given input and take the middle number as M.
But I was not able to solve all the test cases. I am unable to find any other approach for this question. Where did I go wrong?(Please help me this question has been bugging me from the past 2 days.Thanks)
Can M be any real number or must it be an integer?
If there are no constraints on M your algorithm must work fine.
If M must be an integer then you have to choose M among floor(The Middle Number) and ceiling(The Middle Number).
In which language did you code up the algorithm?

Resources