Number Divisibility Rules Snippet

ArmellineArmelline Member, PRO Posts: 5,371
edited January 2014 in Working with GS (Mac)
Hey all,

I've just been fine-tuning the last bits of a new game and a part of the game has actors spawn every other "point" scored. Upon testing, though, I found that this felt too frequent. This led me to test every 3rd, every 4th, every 5th.

In doing so, I found I had to calculate if a particular number was divisible by 2, 3, 4, 5, 6 etc.

I thought I'd share the (very simple) methods I used in case they're of help to anyone. (Though most here will be able to work this out for themselves, easily enough.)

Divisibility Rules:

Divisible by 2:

If game.attribute%2 = 0 then true

Divisible by 3:

If (game.attribute/3)%2 = 0 or (game.attribute/3)%2 = 1 then true

Divisible by 4:

If (game.attribute/2)%2 = 0 then true

Divisble by 5:

If (game.attirbute/5)%2 = 0 or (game.attribute/5)%2 = 1 then true

Etc. for higher numbers.

Would love to get rid of the or's needed for 3 and 5 so shout if you know a better way!

Comments

  • ArmellineArmelline Member, PRO Posts: 5,371
    Pah, posted by mistake in the Pro section! Why does it default to that :(

    Any chance a mod can move it to the Working with GameSalad forum?
  • slowcutslowcut Member, PRO Posts: 164
    I always use:

    Divisible by 2
    If mod(game.attribute,2) = 0

    Divisible by 3
    If mod(game.attribute,3) = 0

    Divisible by 4
    If mod(game.attribute,4) = 0

    Divisible by 5
    If mod(game.attribute,5) = 0

    and so on

  • ArmellineArmelline Member, PRO Posts: 5,371
    Oooh, love it slowcut. I'm pretty sure they're depricating % so probably best to get used to using mod instead! Your method is definitely more elegant for 3 and 5!
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    [moved]

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • SocksSocks London, UK.Member Posts: 12,822
    [moved]
    Yah !!!

    : )

    Divide the mystery number by 5 - then check the result is equal to ceil(result) - if it is, then it's divisible by 5.

    So basically divide the mystery number by the check number and see if the result is a whole number, if it is a whole number then the mystery number is divisible by the check number.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited January 2014
    So sorry! I can't seem to resist!

    (mod(self.myNum,10)==0)and(10)or(mod( self.myNum,9)==0)and(9)or(mod( self.myNum,8)==0)and(8) or(mod(self.myNum,7)==0)and(7)or(mod(self.myNum ,6)==0)and(6)or(mod( self.myNum,5)==0)and(5) or(mod(self.myNum ,4)==0)and(4)or(mod(self.myNum,3)==0)and(3)(mod(self.myNum,2)==0)and(2)
  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2014
    Check if divisible by X

    Is the value = (ceil(value/X))*X ?

    If the they are the same, then the value is divisible by X.
  • SocksSocks London, UK.Member Posts: 12,822
    So sorry! I can't seem to resist!

    (mod(self.myNum,10)==0)and(10)or(mod( self.myNum,9)==0)and(9)or(mod( self.myNum,8)==0)and(8) or(mod(self.myNum,7)==0)and(7)or(mod(self.myNum ,6)==0)and(6)or(mod( self.myNum,5)==0)and(5) or(mod(self.myNum ,4)==0)and(4)or(mod(self.myNum,3)==0)and(3)(mod(self.myNum,2)==0)and(2)

    :-B 8-X <:-P

    :))
  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2014
    Here's a demo project:

    Link: https://www.mediafire.com/?255pxd8757g4a0x

    Enter a value - and it will quickly check whether it is divisible by every number from 1 to 950 (I couldn't fit any more on the screen !) - the green numbers are numbers that your input value is divisible by ('hits').

    To check divisibility this project uses: is the value = (ceil(value/X))*X

    Obviously big numbers at the input stage result in prettier pattens with lots of 'hits'.


    Youtube: lots of small type: Hit that 720pHD button or risk blindness :-B

  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2014
    Ok, I think I've taken this all too far @-) this one goes up to 2582 and looks like something out of The Matrix 8-X

    Link: https://www.mediafire.com/?j7b8bj3n728i57u
  • JSprojectJSproject Member Posts: 730
    Ok, I think I've taken this all too far @-) this one goes up to 2582 and looks like something out of The Matrix 8-X

    Link: https://www.mediafire.com/?j7b8bj3n728i57u
    Nice one :)
    And no, you haven't taken it too far - there is still room for going up to 2583 in your demo... :D
  • SocksSocks London, UK.Member Posts: 12,822
    Ok, I think I've taken this all too far @-) this one goes up to 2582 and looks like something out of The Matrix 8-X

    Link: https://www.mediafire.com/?j7b8bj3n728i57u
    Nice one :)
    And no, you haven't taken it too far - there is still room for going up to 2583 in your demo... :D
    That was a test . . . you have passed . . . you are The One.
  • JSprojectJSproject Member Posts: 730
    I've transcended and am already "in there", no longer known by the name of Neo :P
  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2014
    I've transcended and am already "in there", no longer known by the name of Neo :P
    The name 'Neo' was already taken I'm afraid, the elders of the Matrix has decreed you shall have a new name, you shall be known as "Interpolate"*


    *(or 'Tony', Tony is also free, and Barbara, so basically 'interpolate', Tony or Barbara)**


    **(actually 'change attribute' has also just become free, he was killed by one of those robot spider things, may 'change attribute' rest in peace)***


    ***(oh, nearly forgot, we don't currently have a 'Mary' either, so 'interpolate', Tony, Barbara, 'change attribute' or Mary, your choice, no pressure, but we must have an answer by Monday)
  • JSprojectJSproject Member Posts: 730
    Holy Mary! For a more calculated and smoother change attribute process Barbara would have to interpolate into Tony or vice versa.
  • SocksSocks London, UK.Member Posts: 12,822
    : )
Sign In or Register to comment.