0
00: 599  
01: 298  
02: 738  
03: 598   
04: 297  
05: 395 
06: 730 
07: 825
08: 597 
09: 295
10: 717
11: 597
12: 196
13: 397
14: 592
15: 393
16: 600 
17: 598
18: 902
19: 598
20: 196 
21: 398
22: 594 
23: 397  
24: 600   
25: 593
26: 196 
27: 393 
28: 595  
29: 604  
30: 593  
31: 717
32: 598
33: 196 
34: 398
35: 594
36: 397
37: 600
38: 000  
91: 005  
92: 000   // DAT 000
93: 000   // Counter
94: 002   // DAT 002
96: 001   // DAT 001 - plus 1  
97: 002   // DAT 002 - dividor
98: 002   // DAT 001 - incrementor
99: 050   // DAT 10  - max

Hi guys,

I have a code to find the prime numbers between 1-100, but I'm struggling to recreate this into a program that finds only those between user input.

I had a plan to subtract one number from another, and then to divide that number by 2, 3, 4 and 5.

Do you guys have any advice how to go about this? I apologize for the lack of comments.

l33tspeak
  • 95
  • 3
  • 13

1 Answers1

2

Disclaimer: I don't know what your original code does, as I don't read numeric codes that well. The following is from primes.lmc, which I wrote myself.


Code (heavily commented):

# Prime number finder. Prints all prime numbers between the numbers the user inputs (min, then max).

# Min
        INP
        SUB ONE
        STA NUM

# Max
        INP
        STA MAX

# Main checking loop. Check each number from NUM to MAX.
TLOOP   LDA NUM

# Have we done all MAX numbers?
        SUB MAX
        BRZ HALT

# Increment to next number to check.
        LDA NUM
        ADD ONE
        STA NUM

# Reset divisor.
        LDA ONE
        STA DIV

# Check NUM for primeness by dividing all numbers from 2 to NUM - 1 into it.
DLOOP   LDA DIV

# Increment to next divisor.
        ADD ONE
        STA DIV

# Have we checked up to the number itself?
        LDA DIV
        SUB NUM
        BRZ PRIME

# Setup for divide function.
        LDA NUM

# Modulus function: accumulator % DIV.

MODULUS SUB DIV
        BRP MODULUS
        ADD DIV

# Remainder is now in the accumulator. If its zero, NUM is not prime.
        BRZ NPRIME
        BRA DLOOP

# If its prime, print it.
PRIME   LDA NUM
        OUT

# Go back to the top.
NPRIME  BRA TLOOP

# End of program.
HALT    HLT

NUM     DAT 1
DIV     DAT 1
ONE     DAT 1
MAX     DAT

First user input is the minimum, second is the maximum (inclusive).


Running (on specter, from 13 to 23):

Running

matsjoyce
  • 5,744
  • 6
  • 31
  • 38