Author Topic: [TI-Basic] Divide Numbers and Rest of Division  (Read 3812 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
[TI-Basic] Divide Numbers and Rest of Division
« 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.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #1 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)
« Last Edit: February 11, 2011, 11:19:04 am by graphmastur »

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #2 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?

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #3 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

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #4 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.

Offline Xanwell

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-0
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #5 on: February 11, 2011, 12:42:32 pm »
It returns a 0 if the argument is not an integer, 1 if it is.


Spoiler For My current projects:
Kalima's Quest: An Axe RPG [5%]
--Sprites: 40%
--Battle engine: 0%
--Map engine: 0%
--Story: 10%

Avoider [50%]
--Engine: 85%
--Enemy AI: 15%
--Graphics: 50%

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #6 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 :)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #7 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 :)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #8 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.

Offline ferox

  • LV3 Member (Next: 100)
  • ***
  • Posts: 94
  • Rating: +3/-0
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #9 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...
« Last Edit: February 11, 2011, 01:02:23 pm by ferox »
on hold:

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #10 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. :)

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #11 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 ;))
« Last Edit: February 11, 2011, 05:01:37 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [TI-Basic] Divide Numbers and Rest of Division
« Reply #12 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)