Charles Babbage's Difference Engine
InterviewTime limit1sMemory limit128 MB
Given the first values of a low-degree polynomial, extend its finite-difference table forward and report the value at x = 50.
- Level
Easy3 of 10
- Topics
- Simulation, Math, Array
- Solved
- No attempts yet
Problem
A difference engine is an automatic mechanical calculator that tabulates the values of a polynomial. Its name comes from the method of divided differences, which interpolates or tabulates a function from a small set of polynomial coefficients. Logarithmic and trigonometric functions, both used constantly by navigators and scientists, can be approximated by polynomials, so one difference engine can compute many useful sets of numbers.
Teams of mathematicians and the human clerks known as computers kept letting errors into printed tables. That difficulty pushed Charles Babbage to automate the work with a machine.
The engine runs on Newton's method of divided differences, also called the method of finite differences. Once the value of a polynomial and of its finite differences is known at one value of , the engine can calculate any number of nearby values. Take the quadratic and the goal of tabulating and onward. In the table below the second column holds the values of the polynomial, the third column holds the difference of the two neighboring entries in the second column, and the fourth column holds the difference of the two neighboring entries in the third column.
The numbers in the third value column are constant. Starting from any polynomial of degree , value column number is always constant, and the whole method rests on that fact.
The first entry of each column comes from consecutive values of the function. Compute those values, then work backwards and take the differences you need.
Write a program that reproduces Babbage's difference engine. Given through for a polynomial of degree at most , run the table forward to and report .
Input
The first line contains a single positive integer , the number of test cases ().
Each of the next lines holds one test case in the form
n p(0) p(1) p(2) ... p(n)
where is the degree of the polynomial with , followed by the integers through . Every value satisfies .
Output
For each test case print one line of the form Case #x: M, where x is the case number starting from 1 and M is for that polynomial. The answer fits in a signed 64-bit integer.