0

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.

1
  • 3
    set theMod to x mod y (with x ≥ y and x,y=integers)! If theMod=0 x is divisible by y
    – klanomath
    Commented Mar 25, 2020 at 2:16

1 Answer 1

2

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.

2
  • 1
    AppleScript does indeed have a modulo operator, for example 6894381 mod 2.
    – red_menace
    Commented Mar 25, 2020 at 4:23
  • would you mind accepting my answer?
    – Natsfan
    Commented Mar 25, 2020 at 15:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .