I'm attempting to create a program where given N, I need Q = 0...N.
So if given N = 1: Q = 0. If given N = 5: Q = 0, Q = 1,...,Q = 4
My attempt so far:
values(N,Q) :- values_helper(0,N,Q).
values_helper(N, N, Q).
values_helper(X,N,Q) :- X0 is X + 1, X0 < N, values_helper(X0,N,X0).
My logic behind it is that I increment X until it reaches the value of N, at which point the program stops. However, I'm not getting any bindings for Q, just an empty set. I also know that I'm neglecting to stop at N - 1.
EDIT: Fixed up ambiguities with the description.
Both of your values_helper clauses don't do what you expect. The first one succeeds if the first 2 arguments are the same, and doesn't impose any constraints on Q. What you actually want is that Q is set equal to the first argument, as long as it's smaller than the second argument:
values_helper(Q, N, Q) :- Q < N.
In the second clause, again you don't use Q anywhere. The recursive call should be values_helper(X0, N, Q), giving:
values_helper(X, N, Q) :- X0 is X + 1, X0 < N, values_helper(X0, N, Q).
These clauses give the expected output:
?- values(5,Q).
Q = 0 ? ;
Q = 1 ? ;
Q = 2 ? ;
Q = 3 ? ;
Q = 4 ? ;
no
Note that for any N <= 0, this terminates without finding any value for Q, which I believe is the expected behavior.
Related
How can I solve x ^ ( 1 / y ) mod m fast, where x, y, m are all positive integers?
This is to reverse the calculation for x ^ y mod m. For example
party A hands party B agree on positive integer y and m ahead of time
party A generates a number x1 (0 < x1 < m), and hands party B the result of x1 ^ y mod m, call it x2
party B calculates x2 ^ ( 1 / y ) mod m, so that it gets back x1
I know how to calculate x1 ^ y mod m fast, but I don't know how to calculate x2 ^ (1 / y) mod m fast. Any suggestions?
I don't know how to call this question. Given x ^ y mod m is called modular exponentiation, is this called modular root?
I think you're asking this question: Given y, m, and the result of x^y (mod m) find x (assuming 0 <= x < m).
In general, this doesn't have a solution -- for example, for y=2, m=4, 0^2, 1^2, 2^2, 3^2 = 0, 1, 0, 1 (mod 4), so if you're given the square of a number mod 4, you can't get back the original number.
However, in some cases you can do it. For example, when m is prime and y is coprime to m-1. Then one can find y' such that for all 0 <= x < m, (x^y)^y' = x (mod m).
Note that (x^y)^y' = x^(yy'). Ignoring the trivial case when x=0, if m is prime Fermat's Little Theorem tells us that x^(m-1) = 1 (mod m). Thus we can solve yy' = 1 (mod m-1). This has a solution (which can be found using the extended Euclidean algorithm) assuming y and m-1 are coprime.
Here's working code, with an example with y=5, m=17. It uses the modular inverse code from https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
def egcd(a, b):
if a == 0: return b, 0, 1
g, x, y = egcd(b%a, a)
return g, y - (b//a) * x, x
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise AssertionError('no inverse')
return x % m
def encrypt(xs, y, m):
return [pow(x, y, m) for x in xs]
def decrypt(xs, y, m):
y2 = modinv(y, m-1)
return encrypt(xs, y2, m)
y = 5
m = 17
e = encrypt(range(m), y, m)
print decrypt(e, y, m)
RSA is based on the case when m is the product of two distinct primes p, q. The same ideas as above apply, but one needs to find y' such that yy' = 1 (mod lcm((p-1)(q-1))). Unlike above, one can't do this easily only given y and m, because there are no known efficient methods for finding p and q.
L = {a^n b^k | 2n >= k}
For example.: abb is element of L, aabbb is element of L, ε is element of L, but babbb is not element of L, abbb is not element of L
The shortest string in L is the empty string, e. Given a string s in the language, the following rules hold:
as is in L
asb is in L
asbb is in L
We can combine these observations to get a context-free grammar:
S := aSbb | aSb | aS | e
By our observations, every string generated by this grammar must be in L. To show that this is a grammar for L, we must show that any string in L can be generated. To get a string a^n b^k we can do the following:
use rule #1 above x times
use rule #2 above y times
use rule #3 above z times
ensure x + y + z = n
ensure y + 2z = k
Setting y = k - 2z and substituting we find x + k - 2z + z = n. Rearranging:
if k > n, then z and x can be chosen however desired so long as k - n = z - x.
if k < n, then z and x can be chosen however desired so long as n - k = x - z.
If k = n, observe we might as well just choose y = n.
Note that we can always choose z and x in our above example since 0 <= x, z <= n and 0 <= k <= 2n.
So I'm trying to work out in Prolog how to take an input, and return whether it is a perfect number or not. Currently i'm close but can't figure out why it isn't working the way I want.
I am approaching this through recursion. If X %N == 0, then N and TT(total) is added to TT1. N will decrease by one and the function will be called recursively until it reaches 1, then the total will be compared to the input number X.
My code is:
factors(X,N,TT) :-
( 0 is X mod N -> TT1 is N + TT ),
TT = TT1,
( N > 1 -> N1 is N-1, factors(X, N1, TT1)
; TT1 = X
).
perfect(X) :-
factors(X, X-1, 0).
Currently it is returning that all inputs are false. I hope this isn't majorly incorrect and only needs to be tweaked.
Cheers
Here a solutions, hope it helps
factors(_,N,N,_).
factors(X,N,NT,Sum):-
N < NT,
( 0 is X mod N ->
Sum1 = Sum + N
; Sum1 = Sum),
N1 is N + 1,
factors(X,N1,NT,Sum1).
perfect(X):-
XT is X/2 + 1,
factors(X,1,XT,0).
can anyone explain this concept to me? and help me out in figuring out the answer? as I don't quite get it yet but any help will do, thank you
There exists two really cool primitive recursive functions:
T(z, x1, x2, ..., xn, y) returns 0 if z is an encoding of a Turing Machine, and its computation with
inputs x1, ... xn encodes to y. The function returns 1 otherwise. U(y) returns the result of the computation y.
Goal:Use these two functions and the µ(mu) operator to get a definition of a function f(x1, x2, ...xn) which returns the output of the TM encoded by z
what I have so far:
using these two functions and the µ operator we can then generate a definition for the function f(x1,x2, ...xn) that returns the output of the TM encoded by z such that we have a TM that we can define as T(n) where this computes the number of primes which are less than or equal n. z = 0 which is the number of primes found, y = 1 which is for each y we test whether it is prime, while k <= n:, j = 1 are the possible divisors of y, d = 0 the number of divisors of k found, while j <= n: if y % j == 0:, d = d + 1, j = j + 1, if d== 2:, z= z+1,y=y+1, then return z which is the output of the TM encoded by z
In the below division algorithm, I am not able to understand why multiplying q and r by two works and also why r is incremented if x is odd.
Please give a theoretical justification of this recursive division algorithm.
Thanks in advance.
function divide(x, y)
if x = 0:
return (q, r) = (0, 0)
(q, r) = divide(floor(x/2), y)
q = 2q, r = 2r
if x is odd:
r = r + 1
if r ≥ y:
r = r − y, q = q + 1
return (q, r)
Let's assume you want to divide x by y, i.e. represent x = Q * y + R
Let's assume that x is even. You recursively divide x / 2 by y and get your desired representation for a smaller case: x / 2 = q * y + r.
By multiplying it by two, you would get: x = 2q * y + 2r. Looking at the representation you wanted to get for x in the first place, you see that you have found it! Let Q = 2q and R = 2r and you found the desired Q and R.
If x is odd, you again first get the desired representation for a smaller case: (x - 1) / 2 = q * y + r, multiply it by two: x - 1 = 2q * y + 2r, and send 1 to the right: x = 2q * y + 2r + 1. Again, you have found Q and R you wanted: Q = 2q, R = 2r + 1.
The final part of the algorithm is just normalization so that r < y. r can become bigger than y when you perform multiplication by two.
Algorithm PuzzleSolve(k,S,U) :
Input: An integer k, sequence S, and set U
Output: An enumeration of all k-length extensions to S using elements in U without repetitions
for each e in U do
Add e to the end of S
Remove e from U /e is now being used/
if k == 1 then
Test whether S is a configuration that solves the puzzle
if S solves the puzzle then
return "Solution found: " S
else
PuzzleSolve(k-1,S,U) /a recursive call/
Remove e from the end of S
Add e back to U e is now considered as unused
This algorithm enumerates every possible size-k ordered subset of U, and tests each subset for being
a possible solution to our puzzle. For summation puzzles, U = 0,1,2,3,4,5,6,7,8,9 and each position
in the sequence corresponds to a given letter. For example, the first position could stand for b, the
second for o, the third for y, and so on.