Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: Munchor on February 11, 2011, 11:17:27 am

Title: [TI-Basic] Divide Numbers and Rest of Division
Post 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.
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: jnesselr on February 11, 2011, 11:18:43 am
N-A*int(N/A) will work.  Where N mod A = 0.  (Note that this is just the remainder of N/A)
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: Munchor on February 11, 2011, 11:21:04 am
Code: [Select]
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?
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: Builderboy on February 11, 2011, 12:09:24 pm
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
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: Munchor on February 11, 2011, 12:32:44 pm
So, not(fPart(N/A)) will return what kind of stuff?

Sorry, I could test it but I don't have my calculator nearby.
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: Xanwell on February 11, 2011, 12:42:32 pm
It returns a 0 if the argument is not an integer, 1 if it is.
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: Builderboy on February 11, 2011, 12:44:14 pm
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 :)
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: Munchor on February 11, 2011, 12:49:05 pm
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 :)

Code: [Select]
If fPart(A/2)
// CODE
End

This will return 1 if divisible and 0 if not divisible, according to what I got :)
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: Builderboy on February 11, 2011, 12:50:20 pm
almost ;) test it and see that it will be reversed.   The If statement will execute if its not divisible, and skip if it is.
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: ferox on February 11, 2011, 01:01:43 pm
if you have a calc with 2.53 or higher, you can use the function remainder( like this:
Code: [Select]
If not(Remainder(N,A
Then
//code
End
Remainder gives the what is left after dividing so you can also use it like this:
Code: [Select]
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...
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: ztrumpet on February 11, 2011, 04:49:13 pm
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. :)
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: meishe91 on February 11, 2011, 04:49:32 pm
To do Builder's version of the code it would looks something like this.

Code: [Select]
Prompt N,A
If not(fPart(N/A
Then
Disp "DIVISIBLE
Else
Disp "NOT DIVISIBLE
End

Or...

Code: [Select]
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 ;))
Title: Re: [TI-Basic] Divide Numbers and Rest of Division
Post by: Xeda112358 on February 11, 2011, 05:17:51 pm
Just 'cause I'm bored :D
Code: [Select]
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)