Modulo operator in Pascal? - pascal

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

Related

The way of thinking in multiply 2 natural numbers (problem solving”)

Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants c ≥ 2.
function multiply(y,z) comment Return the product yz.
1. if z = 0 then return(0) else
2. return(multiply(cy, z/c) + y · (z mod c))
I saw this algorithm in “Algorithm Design Manual”.
I know why it works correctly, but I want to know how this algorithm came to be. Is that a good way to think of multiply two natural number with a constant c?
(multiply(cy, z/c) + y · (z mod c))
When c is the base of your representation (like decimal), then this is how multiplication can be done "manually". It's the "shift and add" method.
In c-base cy is a single shift of y to the left (i.e. adding a zero at the right); and z/c is a single shift of z to the right: the right most digit is lost.
That lost digit is actually z mod c, which is multiplied with y separately.
Here is an example with c = 10, where the apostrophe signifies the value of variables in a recursive call.
We perform the multiplication with y for each separate digit of z (retrieved with z mod c). Each next product found in this way is written shifted one more place to the left. Usually the 0 is not padded at the right of this shifted product, but it is silently assumed:
354 y
x 29 z
----
3186 y(z mod c) = 354·9 = 3186
+ 708 y'(z' mod c) = yc(z/c mod c) = 3540·2 = 7080
------
10266
So the algorithm just relies on the mathematical basis for this "shift and add" method in a given c-base.

Define operator in prolog

I am doing a lot of prolog exercises to improve my logic skills. But i'm stuck with the request of an exercise.
What i have to do is to define an operator i , in a way that: if the user inputs a complex number with this syntax , via the prompt ( so i use the read(X) operator)
(4+ i 7) - (2+ i 3).
i get as result
2+ i 4
I've understood how to define an operator in Prolog,i've studied the op operator but i dont know how make that subtraction operation really happen
Your first problem is that xfx defines a binary operator, and you want a unary operator, so you need a declaration like this:
:- op(600, xf, i).
Your second problem is that, there is no circumstance in which entering arithmetic expressions at the Prolog query prompt will result in anything like reduction happening automatically. See:
?- 3 + 4 * 7.
ERROR: Undefined procedure: (+)/2 (DWIM could not correct goal)
?- X = 3 + 4 * 7.
X = 3+4*7.
In order to cause arithmetic to be evaluated, you have to use the is/2 operator:
?- X is 3 + 4 * 7.
X = 31.
Try and think of is/2 as just another predicate, relating a numeric value with an expression. There is no way in ISO Prolog to modify the behavior of is/2, so you'll have to make an evaluation predicate of your own and use that:
eval((A + B i) + (C + D i), E + F i) :-
E is A + C,
F is B + D.
Once you have that, you can use it the usual way:
?- eval((3 + 4 i) + (7 + 8 i), X).
X = 10+12 i.
As you can see, this is probably going to get tedious, but it will work. If you want to gin up more comprehensive support for complex numbers by hand, you should consider making a metainterpreter.

Prolog, check divisibility in Peano arithmetic

I need to check if first given term (for example s(s(nul)) (or 2)) is dividable by the second term, (for example s(nul) (or 1)).
What I want to do is multiply given term by two and then check if that term is smaller or equal to the other term (if it is equal - problem is solved).
So far I got this:
checkingIfDividable(X,X).
checkingIfDividable(X,Y) :-
X > Y,
multiplication(X,Y).
/* multiplication by two should occur here.
I can't figure it out. This solution does not work!*/
multiplication(Y):-
YY is Y * 2,
checkingIfDividable(X,YY).
I can't seem to figure out how to multiply a term by 2. Any ideas?
If a = n*b, n > 0, it is also a = n*b = (1+m)*b = b + m*b, m >= 0.
So if a is dividable by b, and a = b+x, then x is also dividable by b.
In Peano encoding, n = 1+m is written n = s(m).
Take it from here.

Calculating product by addition

This is an algorithm question that I've been struggling with. I figured I could get some insight here. I need to make the following function in Haskell:
Declare the type and define a function that takes two numbers as input and finds their product by addition. That is, add the first number, as many times as second number, to itself.
My problem is that this is basically just multiplying two numbers together, but it says that I need to do it with addition. Does anyone have any clue on how to do this?
This is all I can come up with (it's not right): (x + x) * y
Thank you
if a is the first number and b the second
sum $ take a $ cycle [b]
should do ot
mult (x, y):
sum = 0
for 1 to y:
sum = sum + x
return sum
This is just the algorithm. I do not know Haskell. So the lambda expression in the other answer may be more appropriate. Also, I use an intermediate variable.
PS: forget the previous embarrassing recursive algorithm
Work it out by induction.
We know the answer to one simple (the simplest) problem: multiplying anything by 0 yields 0. So we write:
mul x 0 = 0
Now, the inductive step: we can build a solution to a bigger problem, if we know a solution to the smaller problem; that way we can always reduce any big problem to the smallest problem, for which we know the solution. So, for any y, the solution for y+1 can be found by adding x to the solution for y: mul x (y+1) = x + (mul x y). In Haskell we can't write (y+1) on the left hand side, so we write equivalently:
mul x y = x + (mul x (y-1))
This function will keep adding x until y is zero.
Try this also
multiply::(Num a,Eq a) => a -> a -> a
multiply a 0 = 0
multiply a b = a + multiply a (b - 1)
main = print $ multiply 5 7

Obtain x as result for Re[x] in mathematica

I'm trying to obtain the real part of the result of an operation which involves an undefined variable (let's say x).
How can I have Mathematica return x when I execute Re[x] if I know that x will never be a complex number? I think this involves telling Mathematica that x is a real, but I don't know how.
In my case the expression for which I want the real part is more complicated than a simple variable, but the concept will remain the same.
Some examples:
INPUT OUTPUT DESIRED RESULT
----- ------ --------------
Re[x] Re[x] x
Re[1] 1 1
Re[Sin[x]] Re[Sin[x]] Sin[x]
Re[1+x+I] 1 + Re[x] 1+x
Re[1 + x*I] 1-Im[x] 1
You can use for example the input Simplify[Re[x], x \[Element] Reals] which will give x as output.
Use ComplexExpand. It assumes that the variables are real unless you indicate otherwise. For example:
In[76]:= ComplexExpand[Re[x]]
Out[76]= x
In[77]:= ComplexExpand[Re[Sin[x]]]
Out[77]= Sin[x]
In[78]:= ComplexExpand[Re[1+x+I]]
Out[78]= 1+x
Two more possibilities:
Assuming[x \[Element] Reals, Refine[Re[x]]]
Refine[Re[x], x \[Element] Reals]
Both return x.
It can at times be useful to define UpValues for a symbol. This is far from robust, but it nevertheless can handle a number of cases.
Re[x] ^= x;
Im[x] ^= 0;
Re[x]
Re[1]
Re[1 + x + I]
Re[1 + x*I]
x
1
1 + x
1
Re[Sin[x]] does not evaluate as you desire, but one of the transformations used by FullSimplify does place it in a form that triggers Re[x]:
Re[Sin[x]] // FullSimplify
Sin[x]

Resources