Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to do Modular Arithmetic?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
PeterNewton




PostPosted: 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
Sponsor
sponsor
TerranceN




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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).
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
DtY




PostPosted: 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




PostPosted: 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




PostPosted: Sun Jun 13, 2010 1:08 am   Post subject: RE:How to do Modular Arithmetic?

Thanks guys! The mod command works perfectly.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: