This is the problem
Can someone please help me, I really do not understand how on earth did they get x*y in this procedure ... Question asks what does procedure do ?
PROGRAM iz52(output);
VAR pom: integer;
FUNCTION calc(x,y: integer): integer;
BEGIN
IF (y=1) THEN
calc:= x
ELSE
calc:= calc(x, y-1) + x
END;
A) x+y
B) x*y
C) x^y
Related
We try to translate a very simple program in pseudo-code to Predicate Logic.
The program is straightforward and does not contain loops. (sequential)
It only consists of assignments of variables and if-else statements.
Unfortunately we do not have any good information provided to solve the problem. It would be great if someone has some
examples "conversions" of simple 5liner code snippets or
links to sources for free information, which describe the topic on the surface level. ( We only do predicate and prepositional logic and do not want to dive much deeper in the logic space. )
Kind regards
UPDATE:
After enough research I found the solution and can share it inc. examples.
The trick is to think of the program state as a set of all our arbitrary variables inc. a program counter which stands for the current instruction to be executed.
x = input;
x = x*2;
if (y>0)
x = x∗y ;
else
x = y;
We will form the Predicate P(x,i,y,pc).
From here we can build promises e.g.:
∀i∀x∀y(P (x, i, y, 1) => P (i, i, y, 2))
∀i∀x∀y(P (x, i, y, 2) => P (x ∗ 2, i, y, 3))
∀i∀x∀y(P (x, i, y, 3) ∧ y > 0 =⇒ P (x ∗ y, i, y, 4))
∀i∀x∀y(P (x, i, y, 3) ∧ ¬y > 0 =⇒ P (y, i, y, 4))
By incrementing the Program counter we make sure that the promises follow in order. Now we are able to define make a proof when given a premise for the Input e.g. P(x,4,7,1).
I'm facing this problem, I want to derivate an expression with respect to a variable depending on another variable (not the actual expressions I'm using, they're way more long and complex):
y := x^2 + x + 1;
z := sqrt[y];
D[z, y]
General::ivar: 1+x+x^2 is not a valid variable.
I saw I can solve the problem if I expand the variables like this
D[sqrt[1 + x + x^2], x]
but for long expressions it doesn't seem viable. Is there a simpler way to solve this problem?
Thank you.
You are not consistent with your notations. If you replace D[z, y] by D[z, x], you will get the desired answer.
y := x^2 + x + 1;
z := Sqrt[y];
D[z, x]
I am supposed to show that two algorithms execute identical statements in identical order. One is a tail recursive version of the other. They are written in Eiffel.
tail_rec(x:instance_type):result_type is
local
y:instance_type;
do
if b(x) then
Result :=c(x)
else
y:=d(x);
Result:=tail_rec(y)
end
end
Then the non-tail recursive version.
non_rec(x:instance_type):result_type is
do
from until b(x) loop
x:= d(x)
end;
Result:= c(x)
end
where b(x), c(x), and d(x) are any functions of type BOOLEAN, result_type, and instance_type respectively.
How are these two algorithms similar and how do they execute identical statements in identical order?
By substituting all flow-of-control constructs with gotos, (essentially turning the code from Eiffel into pseudocode,) and allowing if statements to execute only gotos, it can be shown that both functions end up consisting of the exact same set of instructions.
Let me begin by copying the original tail_rec here for convenience:
tail_rec(x:instance_type):result_type is
local
y:instance_type;
do
if b(x) then
Result := c(x)
else
y := d(x);
Result := tail_rec(y)
end
end
First, get rid of Eiffel's silly Result := construct and replace it with return for convenience. (Otherwise, we are going to have to add more gotos, and frankly, the fewer the better.)
tail_rec(x:instance_type):result_type is
local
y:instance_type;
do
if b(x) then
return c(x)
end
y := d(x);
return tail_rec(y)
end
Substitute the if-then-end with if-then goto:
tail_rec(x:instance_type):result_type is
local
y:instance_type;
do
if not b(x) then goto label1
return c(x)
label1:
y := d(x);
return tail_rec(y)
end
Replace tail recursion with another goto:
tail_rec(x:instance_type):result_type is
do
label0:
if not b(x) then goto label1
return c(x)
label1:
x := d(x);
goto label0
end
Replace if not b(x) with if b(x):
tail_rec(x:instance_type):result_type is
do
label0:
if b(x) then goto label1
x := d(x);
goto label0
label1:
return c(x)
end
Similar transformations to tail_rec should turn it into the exact same thing.
I have a quick question. Say you got x+y=2x, and you want to solve this equation in terms of y as some function of x in mathematica, how do you do that?
just use the Solve function. In your case it becomes :
Solve[x + y == 2 x, y]
that gives:
{{y -> x}}
How can I get the leftover of dividing 2 ints?
When using Java I use the % operator, but what can I do in Pascal?
Use mod operator as described here. http://www.tutorialspoint.com/pascal/pascal_operators.htm
A mod B
You can use n mod 2 the same way you'd use n % 2 in Java (when n>=0 anyway...not sure what Pascal does with negative numbers, but Java does the wrong thing.)
However, the most common reason for doing that is to test whether the number is even or odd, and Pascal has the built-in function odd(n) to do just that. On many compilers ord(odd(n)) is a faster way to get the remainder of n mod 2.
Regrettably Pascal mod cannot be used that way. The reason is that I did an incomplete job cajoling the Pascal standards committee.
I lobbied and begged the standards committee to do mod the right way until eventually they relented. So, for example -5 mod 2 equals 1. To my horror, they did integer division the wrong way. I never imagined they would not make the two match up. To this day, in Pascal (-5 mod 2) + (-5 / 2) equals -4. I blame myself.
Pascal's modulo operator is mod. It works just like the % operator in Java and C/C++:
var
X, Y: Integer;
begin
X := 10;
Y := X mod 4; // result: Y = 2
Y := X mod 3; // result: Y = 1
end;
In delphi there is the MOD operator aka x = Y MOD Z. should work in pascal