Пример #1
0
""")

# load the program
ae.load_program(program)

# indices B[k]
k = 1
# input data, initially empty, but each result is added after computation
data = []
# instruction to start execution at, initially 0, but subsequently 4
start = labels['init']
# run the program
while True:
    # load the data and run the program
    ae.load_data(data)
    ae.run(start)
    # get the computed result from the output transcript
    r = (ae.output[-1] if ae.output else None)

    # display the computed result
    printf("B[{k}] = {r}")

    # run the program again?
    try:
        raw_input('\n[paused] >>> ')
    except EOFError:
        printf()
        break

    # add the result to the data and run it again (from instruction 4)
    data.append(r)
Пример #2
0
  ['SET', 13, 0],
  # end
  ['HALT'],
])

# indices B[k]
k = 1
# input data, initially empty, but each result is added after computation
data = []
# instruction to start execution at, initially 0, but subsequently 4
start = 0
# run the program
while True:
  # load the data and run the program
  ae.load_data(data)
  ae.run(start)
  # get the computed result from the output transcript
  r = (ae.output[-1] if ae.output else None)

  # display the computed result
  printf("B[{k}] = {r}")

  # run the program again?
  try:
    raw_input('\n[paused] >>> ')
  except EOFError:
    printf()
    break

  # add the result to the data and run it again (from instruction 4)
  data.append(r)
from sys import argv
n = (40 if len(argv) < 2 else int(argv[1]))

# initialise the engine
ae = AnalyticalEngine(vars=3, number=Column(digits=50), warn=1, trace=0)

(program, _) = ae.assemble("""
  :init
  SET v0 <- {n}
  SET v1 <- 1
  SET v2 <- 1
  :loop
  # operation 1: v2 = v0 * v2
  MUL v0 v2. -> v2
  # operation 2: v0 = v0 - 1
  SUB v0. 1 -> v0
  # branch if non-zero to operation 1
  BRN loop
  # end
  HALT
""".format(n=n))

# load the program to compute factorial(n)
ae.load_program(program)

# run the program
ae.run()

# the result is in v2
print("factorial({n}) = {r}".format(n=n, r=ae.v[2]))
Пример #4
0
p = AnalyticalEngine(vars=14, number=Column(digits=10, dp=40), trace=0)

# load the program
p.load_program(program)

# indices B[k]
k = 1
# input data, initially empty, but each result is added after computation
data = []
# instruction to start execution at, initially 0, but subsequently 4
start = labels['init']
# run the program
while True:
  # load the data and run the program
  p.load_data(data)
  p.run(start)
  # get the computed result from the output transcript
  r = (p.output[-1] if p.output else None)

  # display the computed result
  printf("B[{k}] = {r}")

  # run the program again?
  try:
    raw_input('\n[paused] >>> ')
  except EOFError:
    printf()
    break

  # add the result to the data and run it again (from instruction 4)
  data.append(r)
Пример #5
0
ae = AnalyticalEngine(vars=6, warn=1, trace=0)

(program, _) = ae.assemble("""
  :init
  SET v0 <- 0
  SET v1 <- 1/2
  SET v2 <- {n}
  # initial guess: x = n / 2
  MUL v1 v2 -> v3
  :loop
  # save current guess
  ADD v3 0 -> v4
  # x = (n / x + x) / 2
  DIV v2 v3 -> v5
  ADD v5. v3. -> v5
  MUL v5. v1 -> v3
  # test against previous value
  SUB v3 v4.
  BRN loop
  HALT
""".format(n=n))

# load the program
ae.load_program(program)

# run the program
ae.run()

# the result is in v3
print("sqrt({n}) = {r}".format(n=n, r=ae.v[3]))
Пример #6
0
import sys
n = (40 if len(sys.argv) < 2 else int(sys.argv[1]))

(program, _) = assemble("""
  :init
  SET 0 <- {n}
  SET 1 <- 1
  SET 2 <- 1
  :loop
  # operation 1: v[2] *= v[0]
  MUL 0 2 -> 2
  # operation 2: v[2] -= 1
  SUB 0 1 -> 0
  # branch if non-zero to operation 1
  BRN loop
  # end
  HALT
""".format(n=n))

# initialise the engine
p = AnalyticalEngine(vars=3, number=Column(digits=50), trace=1)

# load the program to compute factorial(n)
p.load_program(program)

# run the program
p.run()

# the result is in v[2]
print("factorial({n}) = {f}".format(n=n, f=p.v[2]))