-2

I need to know the factorization of numbers. Why? Well I am planning on writing a story called Math World and for the base population I have these gender and factor rules:

If male factors outweigh female factors(so like 4 male factors and 3 female factors), the number is male.

If female factors outweigh male factors, the number is female.

If male factors equal female factors, the number is hermaphroditic(both male and female).

If you take a male number to an integer power, you get a hermaphroditic number.

If you take a female number to an integer power, you get another female number.

Primes alternate between male and female(so like 2 is male, 3 is female and so on)

But this is only for the base population.

Anyway,I don't have a factorization program on my calculator. I need all the factors, not just prime factors. How can I do that?

Calculator model:

TI-84 Plus Silver Edition

Code:

:Input "TYPE NUMBER", X
:FOR(A,1,X)
:IF remainder(X,A)=0
:Disp A
:End
:End
:End

Here is the code that is giving me errors on my calculator. My calculator is in FUNC mode, thus the X that I put in via the variable button(different variable for each mode).

Caters
  • 147
  • 1
  • 10
  • We can't help you if you don't have any code written. – kamoroso94 Oct 21 '16 at 23:10
  • I added the code that gives me the errors so hopefully you can help me make this factoring program work. – Caters Oct 22 '16 at 21:52
  • 1
    You have too many `End`s. – kamoroso94 Oct 22 '16 at 21:52
  • But I read I need 1 end for the `FOR`, 1 end for the `IF` and 1 end for the program itself. – Caters Oct 22 '16 at 22:04
  • 1
    The program does not require an `End`. The `For(` loop does, but the `If` does not since there is no `Then`. – kamoroso94 Oct 22 '16 at 22:20
  • IF statements can follow 2 formats: IF Condition:CMD and IF Condition:Then:CMDS:END. Since you did not include a "THEN", there is no matching "END" statement. – Julian Lachniet Oct 28 '16 at 01:12
  • Programs automatically quit in TI-Basic. There is no need for `Stop`, `Return`, or `End` at the end of a program by default. Programs behave (with one exception) as if they had `Return` at the end (that exception is if something is evaluated on the last line, where it will be displayed instead of `Done`). – Timtech Oct 28 '16 at 14:26

3 Answers3

5

Let's take a look at your code:

:Input "TYPE NUMBER", X
:FOR(A,1,X)
:IF remainder(X,A)=0
:Disp A
:End
:End
:End

First off, programs automatically return in TI-Basic. There is no need for Stop, Return, or End at the end of a program by default. Programs behave (with one exception) as if they had Return at the end (that exception is if something is evaluated on the last line, where it will be displayed instead of Done).

Second, unless an If statement is accompanied by Then, an End is not necessary (it will be only the next line that is conditionally evaluated).

Thus, you should only have one End token instead of three. Also, this may have been a typo on your part, but you should not have a space after the comma in the first line. You probably wanted it before the end-quote on the first line.

Now your code works, and it looks like this:

:Input "TYPE NUMBER ",X
:For(A,1,X)
:If remainder(X,A)=0
:Disp A
:End

There are still optimizations, however. Because of the quirk in the For loop, you should leave the closing parenthesis. But for the if statement, you could do If 0=remainder(X,A to leave off the closing parenthesis. Additionally, instead of 0= you can simply use not(. Lastly, remainder(X,A doesn't work with all of the TI series and is one byte longer than fPart(X/A. There are no downsides to this replacement, and it will save you a little space (1 byte), time, and compatibility.

Lastly, if X is a number and A is a factor of that number, X/A is also a factor. Thus, we only need to loop up to sqrt(X), which is much more efficient. Here is the final code:

:Input "TYPE NUMBER ",X
:For(A,1,√(X))
:If not(fpart(X/A
:Disp A,X/A
:End

Possible optimizations (up to you):

  • 50/50 - calculate X/A separately and use Ans twice. A little save in speed, no size change, a little less readability.
  • Would not recommend - leave off the )) in the For loop to save a little space (2 bytes), but there is a noticeable decrease in speed for large numbers, thanks to the quirk with For( loops.
  • Up to you - since you already know that 1 and X are factors, you could start the loop from 2. It just depends how you're recording or displaying these values.
Timtech
  • 1,224
  • 2
  • 19
  • 30
0
Input "TYPE NUMBER:",N
For(A,1,N)
If remainder(N,A)=0
Disp A
End

I wasn't able to test his program, but I'm fairly sure that it works. This program plainly displays the factors, but it can easily be changed into a format that can be outputted, like maybe a list.

If the remainder of N divided by A is equal to zero, then A is a factor of N.

Douglas
  • 1,304
  • 10
  • 26
  • What's the error? I can't test it, because I have a TI-89, but if I know the error, I'll be able to fix it. Also, press "Goto" after the program crashes and tell me where the cursor is placed. Sorry for the inconvenience. – Douglas Oct 19 '16 at 13:15
  • That is all it says is ERR:SYNTAX. The cursor is placed on the S in the name FACTORS. – Caters Oct 19 '16 at 14:49
  • Oops, I forgot the parentheses next to For. Make sure that you aren't just typing in the letters, you are typing the actual command. If all else fails, you can find all of these in the catalog. – Douglas Oct 19 '16 at 17:58
  • I made sure to have 3 end statements, 1 for the for, 1 fur the if and 1 for the program. It is giving me 2 different errors, an undefined error when I put the number right after the program name and a general syntax error when I add parentheses around the number or a space. I also made sure the for statement had parentheses. So I don't know what is going on. – Caters Oct 19 '16 at 19:42
  • You only need one End statement. "If" doesn't need an end statement unless it also has a "Then". An If statement can only have one line of code following it, unless it has "Then". All the If statement needs to do in this program is "Disp A", which is only one line of code. Also, an End statement is not needed to end the program. – Douglas Oct 25 '16 at 13:13
0

You can improve the speed of the algorithm from O(n) to O(√n) with this if you don't care about the order of the factors.

For(A,1,√(X))
If remainder(X,A)=0
Disp A,X/A
End
kamoroso94
  • 1,713
  • 1
  • 16
  • 19