Omnimaga
Calculator Community => TI Calculators => TI-BASIC => Topic started by: Munchor on February 11, 2011, 11:17:27 am
-
I have a TI-Basic doubt:
How to check if a number is divisible by another?
So, for example 9 is divisible by 3, but 10 isn't divisible by 3.
So, check if a number, when divided by another will return an integer.
Thanks.
-
N-A*int(N/A) will work. Where N mod A = 0. (Note that this is just the remainder of N/A)
-
If N-A*int(N/A)=0
Disp "When I divide N by A, I get an integer"
End
So, would this rationalization be correct?
-
How about not(fPart(N/A)) ? It simply does the division itself, and if there is a decimal, it returns false, otherwise true.
I think you guys are overthinking it :P
-
So, not(fPart(N/A)) will return what kind of stuff?
Sorry, I could test it but I don't have my calculator nearby.
-
It returns a 0 if the argument is not an integer, 1 if it is.
-
fPart will return the decimal of any number, and not() will turn any non-zero number into 0, and vica versa, so if there is a decimal, the fpart extracts that, and the Not() inverts it :)
-
fPart will return the decimal of any number, and not() will turn any non-zero number into 0, and vica versa, so if there is a decimal, the fpart extracts that, and the Not() inverts it :)
If fPart(A/2)
// CODE
End
This will return 1 if divisible and 0 if not divisible, according to what I got :)
-
almost ;) test it and see that it will be reversed. The If statement will execute if its not divisible, and skip if it is.
-
if you have a calc with 2.53 or higher, you can use the function remainder( like this:
If not(Remainder(N,A
Then
//code
End
Remainder gives the what is left after dividing so you can also use it like this:
Remainder(N,A->X
If X=Y
bla bla bla...
remainder can be found in the math menu
but i wouldn't recommend it since it doesn't work on OS 2.43 or lower...
-
but i wouldn't recommend it since it doesn't work on OS 2.43 or lower...
Right, which means that it wouldn't work on any of the 83s. :(
fPart(A/B's the way to go. :)
-
To do Builder's version of the code it would looks something like this.
Prompt N,A
If not(fPart(N/A
Then
Disp "DIVISIBLE
Else
Disp "NOT DIVISIBLE
End
Or...
Prompt N,A
not(fPart(N/A
Disp sub("NOT DIVISIBLE",1+4Ans,13-4Ans
(Only saves five bytes but every byte counts in TI-BASIC ;))
-
Just 'cause I'm bored :D
Prompt N,A
"DIVISIBLE
If fPart(N/A
"NOT "+Ans
Disp Ans
It takes off 7 more bytes :D
(I believe it can be optimised more)