LMC: base conversion from decimal to base 9 included - little-man-computer

I am trying to create an LMC assemble code that will allow a user to put two inputs: a decimal number and a base that the number should be converted to. The code should be able to convert any decimal number to any base between 2 and 9 included.
I am aware that the division is not available on LMC: I can use subtractions. Lets say, from 12 decimal to base 3, I am able to get 12-3-3-3-3= 4, but how can I make the code understand that the rest of the division 12/3= 4 and rest of the division is 0 and 4-3=1 but 4/3=1,... so the rest of the division is 1-3=-2 and 1/3 is 0,33... so the rest is 1. Now reading it in opposite sense, 12 decimal to base 2 is 110.
But again, even the 1-3= -2: how can I make it understand that the rest is 1?
Also, how can I make the code understand it's within which base? Do I create a long code first for detecting the base? And let's say it's going to BRA depending which base it is, then I'm not even sure if it's the same algorithm for all the bases...
I am a bit confused here, but even if someone can help me understand how to make the code for getting the remainder of the division, that's gonna help me a lot.

I'll assume that the output should be a series of single-digit numbers representing the given decimal number in the given base notation.
You could use a data-based approach: store all relevant powers of 2, 3, 4, ... and 9 in memory (mailboxes). We only need powers that are not greater than 999, so this list is limited:
Base 2: 1 2 4 8 16 32 64 128 256 512
Base 3: 1 3 9 27 81 243 729
Base 4: 1 4 16 64 256
Base 5: 1 5 25 125 625
Base 6: 1 6 36 216
Base 7: 1 7 49 343
Base 8: 1 8 64 512
Base 9: 1 9 81 729
This also has the advantage that you don't have to perform that many subtractions. Imagine the difference when the input is 999 and base 2. If you have the powers of 2 already available (up to 512), you'll only do about 9 subtractions, while if you try to do it with only 2, you'll do hundreds of subtractions...
So, given these powers, use a "pointer" in that list (through self modifying code) that will first find the range of powers that belong to the given base,
and then will take it from there to perform repeated subtractions of a power (greatest first) from the original number to determine each output digit.
With some care you can avoid that zeroes are output as long as no one has been output.
Here is how that could be coded:
#input: 12 2
INP // number
STA DECIMAL
INP // base
SUB ONE
STA BASE
LOOPBASE LDA BASE // count down to find powers of base
SUB ONE
STA BASE
BRZ DIVIDE
LOOPPOW LDA CODE1 // take next power
ADD ONE
STA CODE1 // self-modifying
LDA ONE // is it 1?
CODE1 SUB POWER
BRZ LOOPBASE // yes...
BRA LOOPPOW // no...
DIVIDE LDA CODE1
ADD ONE
STA CODE2
BRA ENTRY
LOOP STA DECIMAL
LDA DIGIT
ADD ONE
STA DIGIT
ENTRY LDA DECIMAL
CODE2 SUB POWER
BRP LOOP
LDA FIRST // do not output prepadded 0
BRZ OUTPUT // not the first digit
LDA DIGIT
BRZ SKIP
OUTPUT LDA DIGIT
OUT
SUB DIGIT
STA FIRST // no longer first digit
SKIP STA DIGIT
LDA CODE2
ADD ONE
STA CODE2 // self-modifying
STA CODE3 // self-modifying
LDA ONE // is power 1?
CODE3 SUB POWER
BRP FINISH // yes
BRA ENTRY
FINISH LDA DECIMAL
OUT
HLT
DECIMAL DAT
BASE DAT
DIGIT DAT
FIRST DAT 1
POWER DAT
DAT 512 // powers of 2
DAT 256
DAT 128
DAT 64
DAT 32
DAT 16
DAT 8
DAT 4
TWO DAT 2
ONE DAT 1
DAT 729 // powers of 3
DAT 243
DAT 81
DAT 27
DAT 9
DAT 3
DAT 1
DAT 256 // powers of 4
DAT 64
DAT 16
DAT 4
DAT 1
DAT 625 // powers of 5
DAT 125
DAT 25
DAT 5
DAT 1
DAT 216 // powers of 6
DAT 36
DAT 6
DAT 1
DAT 343 // powers of 7
DAT 49
DAT 7
DAT 1
DAT 512 // powers of 8
DAT 64
DAT 8
DAT 1
DAT 729 // powers of 9
DAT 81
DAT 9
DAT 1
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc#v0.7/lmc.js"></script>
This uses almost all available mailboxes. Probably some optimisation in space use is still possible.

Related

Program to sum input numbers is not working

I am trying to make a program that first takes n inputs from the user, and then calculates the sum of those numbers. Then I want the program to print if the sum is an even or an odd number.
For example if the user types in 3, he/she will have to type in 3 numbers (for example 3, 2, 5): then the program will calculate the sum of those (3 + 2 + 5) and print out if the answer (10) is an odd or an even number.
I thought I coded it right, but it doesn't run in the LMC simulator, can someone please help me finding the error?
My code:
INP
STA b
ab INP
STA a
LDA total
ADD a
STA total
STA count
LDA b
SUB one
STA b
BRZ number
BRP loop
bc LDA count
SUB two
STA count
BRZ evennumber
BRP number
LDA total
OUT
LDA space
OTC
OTC
LDA o
OTC
LDA d
OTC
OTC
LDA e
OTC
HLT
cd LDA total
OUT
LDA space
OTC
OTC
LDA p
OTC
LDA A
OTC
LDA r
OTC
HLT
a DAT 0
b DAT 0
total DAT 0
one DAT 1
two DAT 2
count DAT 0
o DAT 111
space DAT 32
d DAT 100
e DAT 101
p DAT 112
A DAT 97
r DAT 114
The main problem in your code is that the labels mismatch.
On the one hand you have defined the following labels:
ab
bc
cd
...but you have referenced the following labels:
loop
number
evennumber
As a consequence your code is not valid ... it will not parse.
The second set of labels make more sense, while "ab", "bc", "cd" are meaningless: they don't help the viewer of your code to understand what they are about. So align your code with the second set.
Also, it is not defined whether LMC is case sensitive, so using a variable name a and another A is not certain to be supported. Instead, give meaningful names. The first a is in fact the number you input and need to add to the sum, so maybe call it summand instead of a. The other A could then be called a, as it really represents the letter "a". b is also meaningless. It represents the number of inputs that are expected, so maybe call it inputs.
Taking that together, your code would look like this:
#input: 2 4 5
INP
STA inputs
loop INP
STA summand
LDA total
ADD summand
STA total
STA count
LDA inputs
SUB one
STA inputs
BRZ number
BRP loop
number LDA count
SUB two
STA count
BRZ evennumber
BRP number
LDA total
OUT
LDA space
OTC
OTC
LDA o
OTC
LDA d
OTC
OTC
LDA e
OTC
HLT
evennumber LDA total
OUT
LDA space
OTC
OTC
LDA p
OTC
LDA a
OTC
LDA r
OTC
HLT
summand DAT 0
inputs DAT 0
total DAT 0
one DAT 1
two DAT 2
count DAT 0
o DAT 111
space DAT 32
d DAT 100
e DAT 101
p DAT 112
a DAT 97
r DAT 114
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc#v0.72/lmc.js"></script>

Algorithm for finding primitive roots from number theory

i would like to implement program for finding primitive number, for given prime number, for this one, i wrote following three program
function primitive_roots=primitive_root(p)
if ~isprime(p)
error(' p must be prime number ');
end
primitive_roots=[];
n=phi(p);
k=p-1;
for ii=2:n
if power_mod(ii,k,p)==1
primitive_roots=[primitive_roots,ii];
end
end
end
there is also power_mod function
function modresult=power_mod(a,b,c)
% this program will calculate a^b mod c
i=0;
result=1;
while i<b
result=mod(result*a,c);
i=i+1;
end
modresult=result;
end
and euler totient function
function phin=phi(n)
% this function will calculates how many coprime number exist for given n, coprime number must be
%less then n
if isprime(n) % if number is prime
phin=(n-1);
end
factors=unique(factor(n));% will printt unique prime divisors of given n
k=1; % counter
for ii=1:length(factors)
k=k*(1-1/factors(ii));
end
phin=k*n;
end
but first programs give me incorrect result, for instance
>> primitive_roots=primitive_root(19)
primitive_roots =
Columns 1 through 14
2 3 4 5 6 7 8 9 10 11 12 13 14 15
Columns 15 through 17
16 17 18
>>
while wolfram alhpa gives me different result
https://www.wolframalpha.com/widgets/view.jsp?id=ef51422db7db201ebc03c8800f41ba99
please help me
i have solved this program, for this one i introduced additional function which will calculate all possible powers and then i am checking for smallest ones
function all_powers=powers_list(a,p)
all_powers=[];
for ii=1:p-1
if power_mod(a,ii,p)==1 % finding all powers
all_powers=[all_powers,ii];
end
end
end
function primitive_roots=primitive_root(p)
if ~isprime(p)
error(' p must be prime number ');
end
primitive_roots=[];
n=phi(p);
k=p-1;
for ii=2:p-1
if power_mod(ii,k,p)==1
all_powers=powers_list(ii,p);
if (min(all_powers)==(p-1))
primitive_roots=[primitive_roots,ii];
end
end
end
>> primitive_roots=primitive_root(5)
primitive_roots =
2 3
>> primitive_roots=primitive_root(7)
primitive_roots =
3 5
>> primitive_roots=primitive_root(19)
primitive_roots =
2 3 10 13 14 15
>> primitive_roots=primitive_root(23)
primitive_roots =
5 7 10 11 14 15 17 19 20 21
>>

How to increment this vector: v = [1ˆ2, 3ˆ2, 5ˆ2, ..., (2n+1)ˆ2] with given pattern (in description)

I am lost as to how to increment this vector. I know that the value from each number squared increase by odd numbers starting from 3. From 1^2 to 2^2 we have a space of three, from 2^2 to 3^2 we have a space of 5 in between and then 7 in between for 3^2 to 4^2 and 9 in between 4^2 and 5^2 and so on and so forth. But I just can't think of how I would write those increments for a general case as I have to do in this given problem.
You cannot define d in the a:d:b vector with one formula because it changes constantly. Therefore, you need to define your vector as [1 3 5 7 ... 2n+1] and square it.
(1:2:2*n+1).^2
ans =
1 9 25 49 81 121

LMC to find prime numbers by user input

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.
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):

Matlab simulation: Query regarding generating random numbers

I am doing some simulations studies and for initial stuides I am trying to simulate 100 gas particles and then grouping of these gas particles in 5 groups randomly for 10 or 100 times (non zero values in any groups). after that i have to find the group with highest particle and the number.
for example
100 gas particles
1 2 3 4 5(groups) Total particle group/Highest number
20|20|20|20|20 100 1-2-3-4-5/20
70|16|04|01|09 100 1/70
18|28|29|10|15 100 3/29
.
.
etc
i have used this to generate 5 random numbers for a single time
for i=1:1
randi([1,100],1,5)
end
ans =
50 41 9 60 88
but how will i find the highest number and group?
Use the max function :
a = [50 41 9 60 88];
[C,I] = max(a)
C should be equal to 88 and I to 4.
For the special case of equality (first line in your code), you have to read the documentation to see the result of max. I think the index returned will be the first max.