This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MrScissors's profile picture

Get if a given number is multiplier of another number.

Started by MrScissors, 19 October 2015 - 02:59 PM
MrScissors #1
Posted 19 October 2015 - 04:59 PM
So, i am writing a program, and a key to make it work is by dividing content in to pages. What i need to have, is a function where 1st input is number you don't know if is multiplier and second input is to tell function of what number the multiplier should be. What i want this func to do is to output 1 if 1st input IS multiplier of 2nd input and 0 if it is not.
Example of ussage:

if function.name(1,8) == 1 then print("Yas!") else print("Nope") end
KingofGamesYami #2
Posted 19 October 2015 - 05:45 PM
I think you're looking for %.


1 % 2 -> 1 //when dividing 1 by 2, the remainder is 1
2 % 1 -> 0 //when dividing 2 by 1, the remainder is 0
8 % 1 -> 0 //when dividing 8 by 1 the remainder is 0
1 % 8 -> 1 //when dividing 1 by 8 (or any number other than 1), the remainder is 1

Edit: Fixed comment (thanks BB)
Edited on 19 October 2015 - 10:21 PM
Anavrins #3
Posted 19 October 2015 - 05:51 PM
By using the modulo operator, you can easily do this, to check if 36 is a multiple of 6 you can do " 36 % 6 == 0 "

function isMultiplier(a, c)
  return a%c==0
end
Edited on 19 October 2015 - 03:55 PM
Bomb Bloke #4
Posted 19 October 2015 - 10:22 PM
//when dividing 8 by 1 (or really, any other number) the remainder is 0

Erm, no. 1, 2, 4 and 8 produce 0. 7 produces 1. Anything else results in a higher value. Likewise, 2 % 3 == 2, and so on.

Anavrins' function looks like it'd be correct for the job, were it to be renamed "isFactor".
Edited on 19 October 2015 - 08:23 PM
MrScissors #5
Posted 20 October 2015 - 02:23 PM
Thanks everyone! This a%c==0 works. I will give Anavrin credit in program that will use this func.

Message to mods: This topic can be closed!