Finding the Factors of… (LMC Challenge)

The aim of this challenge is to write a program using LMC to list all the factors of a given number.

The program will:

  • Ask the user to enter a positive number (e.g. 12)
  • Display all the factors of this number (e.g. 1,2,3,4,6,12)

High Level Flowchart


Here is the high-level flowchart for this algorithm. You can use it to first implement this algorithm using a high level language such as Python.
flowchart-factors-of-a-number

LMC Code


In order to implement this code in LMC you will first need to work out how to calculate the remainder (mod) of a division.

To complete this challenge you will need to use one of the following online LMC Simulators:

Test Plan


Here is a test plan to test your algorithm

Test # Type of Test Input Values Expected Output Actual Output
#1 Valid 12 1,2,3,4,6,12
#2 Valid 21 1,3,7,21
#3 Valid 48 1,2,3,4,6,8,12,16,24,48
#4 Valid 13 1,13

LMC Instruction Set


Note that in the following table “xx” refers to a memory address (aka mailbox) in the RAM. The online LMC simulator has 100 different mailboxes in the RAM ranging from 00 to 99.

Mnemonic Name Description Op Code
INP INPUT Retrieve user input and stores it in the accumulator. 901
OUT OUTPUT Output the value stored in the accumulator. 902
LDA LOAD Load the Accumulator with the contents of the memory address given. 5xx
STA STORE Store the value in the Accumulator in the memory address given. 3xx
ADD ADD Add the contents of the memory address to the Accumulator 1xx
SUB SUBTRACT Subtract the contents of the memory address from the Accumulator 2xx
BRP BRANCH IF POSITIVE Branch/Jump to the address given if the Accumulator is zero or positive. 8xx
BRZ BRANCH IF ZERO Branch/Jump to the address given if the Accumulator is zero. 7xx
BRA BRANCH ALWAYS Branch/Jump to the address given. 6xx
HLT HALT Stop the code 000
DAT DATA LOCATION Used to associate a label to a free memory address. An optional value can also be used to be stored at the memory address.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 8

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!