Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Divisible numbers

Author
Message
Don3784
13
Years of Service
User Offline
Joined: 15th Apr 2013
Location:
Posted: 7th Sep 2013 20:28
ok I have been trying to figure out a simple way to find if a number is divisible by five. I have found a couple but it all seems so complex for such a simple thing i was wondering if anyone else might know a simple easy way to get my program to define if a number is divisible by 5000 or not. the reason for this is i wanted an animated sprite to play every 5 seconds and then return to the first frame. Thanks in advance.
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 7th Sep 2013 20:39
Modulus.

For example: If 10 Mod 5 = 0 Then 'Is divisible by 5.

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 7th Sep 2013 20:47 Edited at: 7th Sep 2013 20:48
Or basically the same thing: if int(q/p)=(q/p) then ...
That is, if the integer result of the division is the same as the float result then the division result is an integer and therefore the divisor is a factor of the dividend; In this case p is a factor of q.

That's how we did it in DBC, I'm sure modulo is quicker though.


Formerly OBese87.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2013 21:57
Quote: " if int(q/p)=(q/p) then "


That will probably need at least one of p and q to be floats.



Powered by Free Banners
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 7th Sep 2013 22:58 Edited at: 7th Sep 2013 22:58
Quote: "if int( q / p ) = ( ( q + 0.0 ) / ( p + 0.0 ) ) then"

Problem solved.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2013 23:35
True - but your first solution is the sensible one. Assuming p and q are integers of course.



Powered by Free Banners
sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 7th Sep 2013 23:54
Quote: "if int(q/p)=(q/p) then ..."


if q=(q/p)*p then q is exactly divisible by p

Just for fun, since as stated, mod is the better way
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 8th Sep 2013 00:04
Mobiius is right. The MOD command is the best. If 9 mod 5 = 0. There are specific CPU instructions for this operation so it should be immensely faster than anything else.

Basically you are checking to see if there is a remainder from the division.

Login to post a reply

Server time is: 2026-07-07 00:09:26
Your offset time is: 2026-07-07 00:09:26