Marie program to identify prime numbers - marie

Write a MARIE program that accepts an integer from the user, and if it is a prime number the program will output 1, otherwise, the program will output 0. Examples:
If the user input is 17, the output would be 1
If the user input is 15, the output would be 0
can please some help me to develop this to get the answer

Here is the code :
Org 0
Input
Store A
Store Val2
Output
Subt one
jump testNeg
Loop, Load A
Subt Value
Skipcond 400
jump div
jump prime
div, Load Val2
Store A
if, Load A
Skipcond 400
jump else
jump prime
else, Skipcond 800
Jump Endif
Then, Load A
Subt Value
Store A
Jump if
Endif, Load Value
Add one
Store Value
jump div
testNeg, Skipcond 000
jump testZero
jump nonPrime
testZero, Skipcond 400
jump Loop
jump nonPrime
nonPrime, Load zero
Output
Halt
prime, Load Val2
Subt Value
Skipcond 400
jump nonPrime
Load one
Output
Halt
A, DEC 0
one, DEC 1
zero, DEC 0
Value, DEC 2
Val2, DEC 0

Related

MARIE instructions for sum of numbers from a to b

I am new at MARIE simulator and I need to write a code that reads "a" and "b" from the user, and calculates the summation from "a" to "b" and outputs it. Can you please help me with this? This is what i have done but the output is not correct:
ORG 100
Input
Store a
Input
Store b
Begin, Load a
Store sum
Add one
Store a
Subt b
Skipcond 800
Jump Begin
Output sum
Halt
a, Dec 0
b, Dec 0
one, Dec 1
sum, Dec 0

Lotto code,the previous number cannot appear again,how do i improve it

I use matlab to write this code,and it seems there is something wrong with logic,but i don't know where am i wrong and how to improve this.
i want to write a lotto code,and there are six numbers in it,the range of first six numbers is 1 to 38,the range of last number is 1 to 8.Here is my code
previous_number=randi([1,38],1,6)
last=randi([1,8],1,1) %produce the last number
for k =1:6
while last== previous_number %while that last number is the same as the value of one of the previous number
last=randi([1,8],1,1)%then produce the last number again,until the different value produce
end
end
ltto=[previous_number last]
but i found that the last number will still generate the same number as the first six numbers,for example,
"1" 2 33 55 66 10 "1"
1 "2" 33 55 66 10 "2"
Why?i have already said
while last==previous_number(k)
last=randi([1,8],1,1)
end
if i want to write the code in c or other program language,i think i can just use if ,while and loop,etc,like this basic loop,i can't use the "ismemeber"or randperm. how can i rewrite the code?
if i rewrite as
previous_number=randi([1,38],1,6)
last=randi([1,8],1,1) %produce the last number
for k =1:6
if last== previous_number(k) %while that last number is the same as the value of one of the previous number
last=randi([1,8],1,1)%then produce the last number again,until the different value produce
end
end
ltto=[previous_number last]
the result will also show me "1" 2 21 12 13 22 "1" sometimes
This occures because you first iterate over the numbers, then replace last according to the specific current iteration, without regarding the previous ones.
For example, in your example data, think that last = 10 so you get to the sixth iteration, find that last is equal to b(k) that is 10, so you replace it. But now it can generate 1, and you will finish the while loop and the for loop.
The solution is to compare last to all your vector, not iterate over it:
previous_number = b(1:6);
last = previous_number(1);
while ismember(last, previous_number)
last = randi(8); %produce the last number
end
[As of comments discussion:]
If you still want to compare each element separately, you can do it like that:
previous_number=randi([1,38],1,6)
last=randi(8)
k=0;
while k <= 5
k = k + 1;
if last == previous_number(k)
last = randi(8);
k = 0;
end
end
ltto=[previous_number last]

Can someone explain the TI BASIC 🔺List command?

I understand that the command compares and can subtract values, but I don't see exactly how that works. I've used a TI BASIC programming tutorial site (http://tibasicdev.wikidot.com/movement-explanation) and I need clarification on 🔺List as a whole.
This portion of the code with 🔺List is as follows,:
:min(8,max(1,A+sum(ΔList(Ans={25,34→A
:min(16,max(1,B+sum(ΔList(K={24,26→B
and the website explains the code like this.:
"This is how this code works. When you press a key, its value is stored to K. We check to see if K equals one of the keys we pressed by comparing it to the lists {24,26 and {25,34. This results in a list {0,1}, {1,0}, or {0,0}. We then take the fancy command Δlist( to see whether to move up, down, left or right. What Δlist( does is quite simple. Δlist( subtracts the first element from the second in the previous list, and stores that as a new one element list, {1}, {-1}, or {0}. We then turn the list into a real number by taking the sum of the one byte list. This 1, -1, or 0 is added to A."
The ΔList( command subtracts every element in a list from its previous element. This code uses some trickery with it to compactly return 1 if a key is pressed and -1
ΔList( calculates the differences between consecutive terms of a list, and returns them in a new list.
ΔList({0,1,4,9,16,25,36})
{1 3 5 7 9 11}
That is, ΔList({0,1,4,9,16,25,36}) = {1-0, 4-1, 9-4, 16-9, 25-16, 36-25} = {1 3 5 7 9 11}.
When there are only two elements in a list, ΔList({a,b}) is therefore equal to {b-a}. Then sum(ΔList({a,b})) is equal to b-a, since that's the only term in the list. Let's say that K is 26 in your example; that is, the > key is pressed.
B+sum(ΔList(K={24,26→B Result of expression:
K 26
K={24,26 {0,1}
ΔList(K={24,26 {1} = {0 - 1}
sum(ΔList(K={24,26 -1
B [current x-position of player]
B+sum(ΔList(K={24,26→B [add 1 to current x-pos. of player]
Similarly, B will be decreased if key 24, the left key, is pressed.

"/usr/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";?

I found this line in the if.c of unix version 6.
ncom = "/usr/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
Why are there so many x's?
And why would you set this?
The code you are talking of looks like this:
ncom = "/usr/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
while(c=nargv[0][i]) {
ncom[9+i++] = c;
}
ncom[9+i] = '\0';
All those x's act as a buffer, they are overridden by the following loop.
Therefore the code effectively adds "/usr/bin/" to the command in nargv[0].
With a little more context the code is doing this:
execv(nargv[0], nargv, np);
execv(ncom+4, nargv, np);
execv(ncom, nargv, np);
If the given command in nargv[0] is "foo" it will first try to run "foo" then "/bin/foo" and finally "/usr/bin/foo".
Be aware that above is a good example how to not do such things:
If the string in nargv[0] happens to be longer than the number of x's, the code will happily continue copying data. This will override other parts of the stack. The result is a good example of a buffer overflow. (You allocate a buffer of some size and write more data than allocated.)
This example will demonstrate the problem:
#include <stdio.h>
int main(){
char s[]="abcde";
int i;
for(i=0;i<100;i++){
printf("position %2d contains value %3d\n",i,s[i]);
s[i]=0;
}
puts(s);
return 0;
}
If you run it it will (most probably) output this:
position 0 contains value 97
position 1 contains value 98
position 2 contains value 99
position 3 contains value 100
position 4 contains value 101
position 5 contains value 0
position 6 contains value 0
position 7 contains value 0
position 8 contains value 0
position 9 contains value 0
position 10 contains value 0
position 11 contains value 0
position 12 contains value 12
position 1 contains value 0
position 2 contains value 0
position 3 contains value 0
position 4 contains value 0
position 5 contains value 0
position 6 contains value 0
position 7 contains value 0
[...]
It will fill the string (containing the ASCII values 97 to 101) with zeroes and continue writing the memory where it will find the position of the variable i it will also set it to zero. Now i is zero and therefore the loop starts again, overriding the the already overridden string again and again.
Not only local variables can be overriden, also the return address of a function might get overriden resulting in either a "segmentation fault" or execution of arbitrary code, which is often used by malware.

Multiply a number by 2 in Brainfuck?

Given an arbitrarily long number, how can I output its double? I know how to multiply small numbers together as long as the result is <10, but what about larger integers like 32984335, and doubling something like that? I don't know the right way to handle something like this.
This is the algorithm you need to implement:
Start the current count with 0;
Multiply the current count by ten: this can be achieved by dupping 10 times, and then adding all dupes together;
Read a digit;
If it's null proceed to 8;
Convert it to an actual number: this can be achieved by subtracting 48;
Add it to the current count;
Proceed to 2;
Duplicate the current count;
Adding the dupes together;
Divide by ten using repeated subtraction; keep quotient and remainder;
Grab the remainder;
Make it a digit (add 48);
Print it;
Grab the quotient from 10;
If it's not zero, goto 10;
The end.
All these steps consists of basic brainfuck idioms, so it should be easy to implement.
Here's a start. It will multiply a byte of input, but I think you can build off it to make it work for any number. Basically, you take in a number, and store the number to multiply by (2) in the next pointer. You loop decrementing the first number, and then nest a loop decrementing the second number; in each iteration of the inner loop, you increment the pointer to the right of your second operand. This is your result.
, take input to ptr 0
[
- decrement first operand (input)
>++ number to multiply by (2) at ptr 1
[
>+ result in ptr 2
<- decrement second operand (2)
]
<
]
>> move to result
Here is my BF code: http://ideone.com/2Y9pk8
->>+>,+
[
-----------
[
-->++++++[-<------>]<
[->+<[->+<[->+<[->+<[>----<-<+>[->+<]]]]]]>
[-<++>]<+
>,+
[
-----------
[->+<]
]
>[-<+>]<
]
<[<]
>-[<++++++++[->++++++<]>.[-]]
>[<++++++++[->++++++<]>-.[-]>]
++++++++++.[-]
<+[-<+]
->>+>,+
]
It reads each number in each line until EOF, and multiply all numbers by two..
Here is the code for multiplying a number by 2.
,[>++<-]>.
Hope this helps.