- #1
mathmari
Gold Member
MHB
- 5,049
- 7
Hey!
Write a RAM program of uniform-cost time complexity $O(\log n)$ to compute $n^n$.Prove that your program is correct.
(Under the uniform cost criterion each RAM instruction requires one unit of time.)
So that the time complexity is $O(\log n)$ the size of the problem should get divided by $2$ each time, right?? (Wondering)
I have done the following:
Is this correct?? (Wondering)
The instructions of the RAM machine are the following:
The RAM program would be as followed:
Is this correct?? (Wondering)
Write a RAM program of uniform-cost time complexity $O(\log n)$ to compute $n^n$.Prove that your program is correct.
(Under the uniform cost criterion each RAM instruction requires one unit of time.)
So that the time complexity is $O(\log n)$ the size of the problem should get divided by $2$ each time, right?? (Wondering)
I have done the following:
Code:
Read n from input
result=n
i=⌊n/2⌋
j=⌊n/2⌋
while i>0
result=result*result
i=⌊i/2⌋
if j+j-n=0
Write result to output
else
result=result*n
Write result to output
Is this correct?? (Wondering)
The instructions of the RAM machine are the following:
LOAD a ; c(0) ← v(a), c(i) is the integer stored in register i (the contents of register i), v(a) is the value of operand a
(v(=i)=i, v(i)=c(i), v(*i)=c(c(i)) )
STORE i ; c(i) ← c(0)
STORE *i ; c(c(i)) ←c(0)
ADD a ; c(0) ← c(0)+v(a)
SUB a ; c(0) ← c(0)-v(a)
MULT a ; c(0) ← c(0) × v(a)
DIV a ; c(0) ← ⌊c(0)/v(a)⌋
READ i ; c(i) ← current input symbol
READ *i ; c(c(i)) ← current input symbol. The input tape head moves one square right in either case.
WRITE a ; v(a) is printed on the square of the output tape currently under the output tape head. Then the tape head is moved one square right.
JUMP b ; The location counter is set to the instruction labeled b.
JGTZ b ; The location counter is set to the instruction labeled b if c(0)>0; otherwise, the location counter is set to the next instruction.
JZERO b ; The location counter is set to the instruction labeled b if c(0)=0; otherwise, the location counter is set to the next instruction.
HALT ; Execution ceases.
(v(=i)=i, v(i)=c(i), v(*i)=c(c(i)) )
STORE i ; c(i) ← c(0)
STORE *i ; c(c(i)) ←c(0)
ADD a ; c(0) ← c(0)+v(a)
SUB a ; c(0) ← c(0)-v(a)
MULT a ; c(0) ← c(0) × v(a)
DIV a ; c(0) ← ⌊c(0)/v(a)⌋
READ i ; c(i) ← current input symbol
READ *i ; c(c(i)) ← current input symbol. The input tape head moves one square right in either case.
WRITE a ; v(a) is printed on the square of the output tape currently under the output tape head. Then the tape head is moved one square right.
JUMP b ; The location counter is set to the instruction labeled b.
JGTZ b ; The location counter is set to the instruction labeled b if c(0)>0; otherwise, the location counter is set to the next instruction.
JZERO b ; The location counter is set to the instruction labeled b if c(0)=0; otherwise, the location counter is set to the next instruction.
HALT ; Execution ceases.
The RAM program would be as followed:
Code:
READ 1
LOAD =n
STORE 2
LOAD =n
DIV =2
STORE 3
LOAD =n
DIV =2
STORE 4
while: LOAD 3
JZERO endwhile
LOAD 2
MULT 2
STORE 2
LOAD 3
DIV =2
STORE 3
JUMP while
endwhile: LOAD 4
ADD 4
SUB =n
JZERO res
LOAD 2
MULT =n
STORE 2
WRITE 2
res: WRITE 2
HALT
Is this correct?? (Wondering)
Last edited by a moderator: