A Scatter Plot With a Fitted Trend Line
Meridian Insight has a client's weekly advertising spend alongside that week's sales, one dot per week. The deck needs a single sentence: how much extra sales arrive with each extra pound spent. That number is the steepness of the best straight line through the cloud of dots, which is drawn over them in red so the reader can see how honest the claim is.
Task: Print the steepness of the best-fitting straight line through the points, taking the first number on each line as the across value and the second as the up value. Best-fitting means the line whose up-and-down gaps to the dots are as small as possible taken together. Steepness is how far the line rises for every 1 it moves across.
Input
The first line holds an integer n, the number of weeks. Each of the next n lines holds two numbers separated by a single space. n is at least 2, and the across values are never all identical.
Output
One line with exactly 2 digits after the decimal point. A line sloping downwards prints with a minus sign, as in -2.00.
Example:
Input:
3
1 2
2 4
3 6
Output:
2.00
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in