Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411426 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 09:29:31 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[GML] How to check if a number is divisible by 5?
Pages: [1] 2
Print
Author Topic: [GML] How to check if a number is divisible by 5?  (Read 9218 times)
Canned Turkey
Guest
« on: November 17, 2014, 07:33:58 PM »

I know the rule, if the last digit is 0 or 5, but I need something that uses math,
i.e.

if x/5 = 1 or 2 or 3 or 4 or 5...
then
do the thing
else
don't do that thing

But, I don't want to check the specific number,
i.e.

if x = 5 or 10 or 15 or 20...
then
do the thing
else
don't do that thing

I just need a function that if the input is divisible by 5, than it returns true.
I'm using GML, but basic math should translate anyway.
Logged
Glyph
Level 10
*****


Relax! It's all a dream! It HAS to be!


View Profile
« Reply #1 on: November 17, 2014, 07:42:50 PM »

if ((x mod 5) == 0) ...
Logged


Canned Turkey
Guest
« Reply #2 on: November 17, 2014, 07:50:10 PM »

if ((x mod 5) == 0) ...

Thank you!
Logged
Cheezmeister
Level 3
***



View Profile
« Reply #3 on: November 17, 2014, 11:30:03 PM »

The "remainder" operation is called modulo, and it's usually present as either mod or "%" (percent) in just about every programming language ever. See http://en.wikipedia.org/wiki/Modular_arithmetic
Logged

෴Me෴ @chzmstr | www.luchenlabs.com ቒMadeቓ RA | Nextris | Chromathud   ᙍMakingᙌCheezus II (Devlog)
Canned Turkey
Guest
« Reply #4 on: November 18, 2014, 10:40:35 AM »

The "remainder" operation is called modulo, and it's usually present as either mod or "%" (percent) in just about every programming language ever. See http://en.wikipedia.org/wiki/Modular_arithmetic

Thanks for letting me know, no wonder I was having a hard time finding anything other than hard math.
Logged
Columbo
Level 0
***


View Profile
« Reply #5 on: November 18, 2014, 11:39:51 AM »

I suppose you could also do something like:

if ((x/5)*5 == x)

If your % key was broken or something  Wink

Logged

Glyph
Level 10
*****


Relax! It's all a dream! It HAS to be!


View Profile
« Reply #6 on: November 18, 2014, 11:59:20 AM »

Yeah, or even

if (abs(cos(x/5 * pi)) == 1) ...

 Giggle
Logged


oahda
Level 10
*****



View Profile
« Reply #7 on: November 18, 2014, 12:03:06 PM »

Code:
for (int i = 0; true; i += 5) 
    if (x == i || x == -i)
        break;
Logged

Rarykos
Level 1
*



View Profile WWW
« Reply #8 on: November 18, 2014, 12:33:44 PM »

Yeah, or even

if (abs(cos(x/5 * pi)) == 1) ...

 Giggle

Wow, this must be the most complicated way! I'm so writing this on my next job interview when I'm asked this question  Wink
Logged

DocProctopus
Level 0
**



View Profile WWW
« Reply #9 on: November 18, 2014, 01:12:59 PM »

Code:
for (int i = 0; true; i += 5) 
    if (x == i || x == -i)
        break;

This will work but it's very inefficient. A single modulo operation is the best way to do this.
Logged
Marchal_Mig12
Level 0
**


View Profile
« Reply #10 on: November 18, 2014, 01:46:57 PM »

Binary operations would be more efficient imo.
Logged
Christian Knudsen
Level 10
*****



View Profile WWW
« Reply #11 on: November 18, 2014, 02:09:48 PM »

Code:
for (int i = 0; true; i += 5) 
    if (x == i || x == -i)
        break;

This will work but it's very inefficient. A single modulo operation is the best way to do this.

Lips Sealed
Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
Sik
Level 10
*****


View Profile WWW
« Reply #12 on: November 18, 2014, 06:56:45 PM »

Yeah, or even

if (abs(cos(x/5 * pi)) == 1) ...

 Giggle

...I hate you. So much.
Logged
Gtoknu
Level 0
***


View Profile
« Reply #13 on: November 18, 2014, 07:25:41 PM »

some go with human-thinking:

Code:
int lastDigit = x % 10;
if(lastDigit == 5 or lastDigit == 0)
{
    functionCall();
}

EDIT:
The "remainder" operation is called modulo, and it's usually present as either mod or "%" (percent) in just about every programming language ever. See http://en.wikipedia.org/wiki/Modular_arithmetic

By the way, I think this: http://en.wikipedia.org/wiki/Modulo_operation is far more informative for a beginner.
Logged

wut
alvarop
Level 9
****


ignorant


View Profile WWW
« Reply #14 on: November 18, 2014, 07:28:10 PM »

Can anyone explain why anyone would use anything but modulo?
Logged

i make games that can only ever be played once on http://throwaway.fun
Gtoknu
Level 0
***


View Profile
« Reply #15 on: November 18, 2014, 07:30:47 PM »

Can anyone explain why anyone would use anything but modulo?

We wouldn't. But it's funny to point out other ways to solve a problem.
Logged

wut
Sik
Level 10
*****


View Profile WWW
« Reply #16 on: November 18, 2014, 09:53:32 PM »

Can anyone explain why anyone would use anything but modulo?

Reason #1: you don't know it even exists (which seems to be the case here), although I have to admit it's usually one of the first things you're taught (alongside + - * /) because of how easy and useful it is.

Reason #2: your language doesn't have that operator (or you don't know if it does), you need to come up with a workaround in that case (I believe some Basic variants have this issue).
Logged
Kyle Preston
Level 2
**



View Profile WWW
« Reply #17 on: November 18, 2014, 10:38:46 PM »

Quote
Yeah, or even

if (abs(cos(x/5 * pi)) == 1) ...

Just as an fyi, this will work with some languages/versions of languages but it's probably not a good practice to implement. As an example, this works with Python 2 point whatever, but in Python 3, fractions < 1 aren't rounded to 0 and hence will not give you the same as % 5.  

Just my two cents.
Logged

oahda
Level 10
*****



View Profile
« Reply #18 on: November 18, 2014, 10:55:30 PM »

Quote
Yeah, or even

if (abs(cos(x/5 * pi)) == 1) ...

Just as an fyi, this will work with some languages/versions of languages but it's probably not a good practice to implement. As an example, this works with Python 2 point whatever, but in Python 3, fractions < 1 aren't rounded to 0 and hence will not give you the same as % 5.  

Just my two cents.
if (abs(cos(round(x/5) * pi)) == 1) ...

Just keep on adding those functions.
Logged

Ads
TIGBaby
*


View Profile
« Reply #19 on: November 19, 2014, 12:54:33 AM »

Another method:

Code:
double mod(double value, double m) {
  return value - m * Math.floor(value / m);
}

Test output:
Code:
-10 mod 3 = 2.0
-9 mod 3 = 0.0
-8 mod 3 = 1.0
-7 mod 3 = 2.0
-6 mod 3 = 0.0
-5 mod 3 = 1.0
-4 mod 3 = 2.0
-3 mod 3 = 0.0
-2 mod 3 = 1.0
-1 mod 3 = 2.0
0 mod 3 = 0.0
1 mod 3 = 1.0
2 mod 3 = 2.0
3 mod 3 = 0.0
4 mod 3 = 1.0
5 mod 3 = 2.0
6 mod 3 = 0.0
7 mod 3 = 1.0
8 mod 3 = 2.0
9 mod 3 = 0.0

Smiley
Logged
Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic