MacOS – How to Check if a Number is Divisible by Another Number

applescriptmacos

I am currently creating an AppleScript program that lets you see if the number you enter is divisible by another number. The code I use to see if my number is divisible by two (I think) works just fine:

set twoCheck to (round (myNumber / 2))
set twoCheck to (twoCheck * 2)
if twoCheck = myNumber then
    set twoCheck to true
else
    set twoCheck to false
end if

To determine if a number is divisible by three, you add up all of the digits in the number, and if the sum is divisible by three, so is the original number. I use this code to find the sum of the digits from the my number:

set numString to (myNumber as string)
set numCount to (number of characters of numString)
set threeCheck to 0
set numSelect to 1
repeat numCount times
    set threeCheck to (threeCheck + (character numSelect of numString))
    set numSelect to (numSelect + 1)
end repeat

This code works just fine. I figured that if I used the same code to see if my number is divisible by two and just replace the two with threes, it would execute just fine and tell me if the sum of the digits is divisible by three. However, this not the case. It always returns either "true" or "false", and it's almost never accurate.

Maybe this is super obvious, and I apologize, but I'm stumped here. Keep in mind that I want to find if the number I enter is divisible by any number, not just the number three (although in this case I do want to find if it's divisible by three, but in other words, this code needs to be expandable to other numbers).

I'm fairly new to AppleScript and it'd be great if I could get some help on this. Thanks.

Best Answer

Numbers don't work like that. Sorry. There is a function that does exactly what you want but I don't know if it's available in Apple Script. The function is called mod. I know it's available in python. The function works like so. enter 2 numbers into the mod function. An example is mod(12,2). what you are asking is 12 divisible by 2. so you could have mod(14,2) mod(6894381,2). if the number is divisible by 2 the function is zero. Else its the remainder of the division as if you did it yourself. This also works for any number. mod(12,3) tells you if 12 is divisible by 3. mod(27,3) = 0, mod(26,3) = 2. so mod(300,50) is zero because 300 is divisible by 50.