Harrierfalcon’s z80 Programmer
My games, code snippets, etc.

A small math prog…

Here’s a small program that will find all whole factors of a number. The bigger the number, the longer it takes, though…

:ClrHome
:Repeat Z and Z≤999 not(fPart(Z
:Input "Number? ",Z
:End
:ClrHome
:Output(8,1,"Calculating...
:D elVar L1Z→dim(L1
:D elVar L2Ans→dim(L2
:For(θ,1,Z
:Z/θ
:If not(fPart(Ans
:Then
:Ans→L1
:θ→L2
:End
:End
:If not(max(L1
:Then
:ClrHome
:D isp "It's prime!
:Stop
:End
:Output(1,1,Z
:Output(2,1,"divided by
:Output(4,1,"is
:For(θ,1,Z
:"
:Output(3,1,Ans
:Output(5,1,Ans
:Output(8,1,"Calculating
:If L2
:Then
:Output(3,1,L2
:Output(5,1,L1
:Output(8,1,"Press Enter
:P ause
:End
:End
:ClrHome
:D elVar L1DelVar L2Output(1,1,"

Obviously there are some optimizations, but seeing as how the program is less than 500 bytes anyways…

Just stick in the number and away you go! Note that some factors it gives you are simply reciprocals of an earlier one. i.e. You entered 60. An early factor was 3*20. Later you get 20*3.

Preventing this would add some 200 bytes to the prog, something I’m not willing to do.

I’ve found this extremely useful when factoring quadratic equations.

4 Responses to “A small math prog…”

  1. >>Preventing this would add some 200 bytes to the prog, something I’m not willing to do.

  2. I think the following would be equally useful…

    Input X
    For(A,1,√(X
    If not(fPart(X/A
    Then
    ClrDraw
    Text(0,0,X,”/”,A,”=”,X/A
    Pause
    End
    End
    Disp

  3. You don’t have to show the reciprocals.
    Just stop looking for factors once you reach the halfway mark.
    For example, if you use 20: 2*10,4*5,5*4…halfway…Done!


Leave a Reply