Author |
Message |
PeterNewton
|
Posted: Sat Jun 12, 2010 12:08 pm Post subject: How to do Modular Arithmetic? |
|
|
What is it you are trying to achieve?
I'm trying to apply modular arithmetic to a certain variables. The purpose of the overall program is irrelevant.
If there is a function modar(x,y), x is my variable and y is the modulo, and I want the modar fucntion to output x mod y. I'm working in only modulo 2 if that simplifies anything.
What is the problem you are having?
I do not know the necessary function. Is there a preprogrammed one or would I have to make it? If it is not preprogrammed, how would I go about making it?
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
TerranceN
|
Posted: Sat Jun 12, 2010 12:26 pm Post subject: RE:How to do Modular Arithmetic? |
|
|
I believe you want the mod command. Here is a small example on how to use it:
Turing: | for i : 1..100
put i..
if (i mod 2 = 0) then
put ": Even"
else
put ": Odd"
end if
end for |
Hope that helps. |
|
|
|
|
 |
DtY

|
Posted: Sat Jun 12, 2010 3:56 pm Post subject: RE:How to do Modular Arithmetic? |
|
|
Since you're doing only mod 2, there's a faster way,
Turing: | function modar(x)
result x & 1
end modar |
This is the equivalent to doing mod 10 arithmetic by only keeping the least significant digit. |
|
|
|
|
 |
TheGuardian001
|
Posted: Sat Jun 12, 2010 4:01 pm Post subject: Re: How to do Modular Arithmetic? |
|
|
Turing uses and as the and operator. it doesn't have & or && |
|
|
|
|
 |
Tony

|
Posted: Sat Jun 12, 2010 7:10 pm Post subject: RE:How to do Modular Arithmetic? |
|
|
It does. They are both and
Quote:
The and operator can also be applied to natural numbers. The result is the natural number that is the bit-wise and of the operands. See nat (natural number).
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
DtY

|
Posted: Sat Jun 12, 2010 8:33 pm Post subject: RE:How to do Modular Arithmetic? |
|
|
It doesn't use &? I'm sure I used that last semester, weird
[edit] Actually, now that I think about it, I might not have, I remember having to look up how to do bit shifts too, was something awful like lhs and rhs or something. |
|
|
|
|
 |
TheGuardian001
|
Posted: Sun Jun 13, 2010 12:45 am Post subject: Re: How to do Modular Arithmetic? |
|
|
Huh. Well what do you know, the and operator actually will do a bitwise and. Didn't know it was multi-functional.
And apparently both & and | are valid. Dunno why I thought they weren't... |
|
|
|
|
 |
PeterNewton
|
Posted: Sun Jun 13, 2010 1:08 am Post subject: RE:How to do Modular Arithmetic? |
|
|
Thanks guys! The mod command works perfectly. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
|