#include <stdio.h>
int main() {
// Given data
int x[] = {-2, -1, 0, 1, 2};
int y[] = {1, 2, 3, 4, 5};
int n = 5; // Number of data points
// Variables to calculate sums
int sum_x = 0, sum_y = 0, sum_xy = 0, sum_x2 = 0;
// Calculate sums
for (int i = 0; i < n; i++) {
sum_x += x[i];
sum_y += y[i];
sum_xy += x[i] * y[i];
sum_x2 += x[i] * x[i];
}
// Calculate slope (b) and intercept (a)
float b = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x);
float a = (sum_y - b * sum_x) / n;
// Output the results
printf("Least square line: y = %.2f + %.2fx\n", a
, b
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKICAgIC8vIEdpdmVuIGRhdGEKICAgIGludCB4W10gPSB7LTIsIC0xLCAwLCAxLCAyfTsKICAgIGludCB5W10gPSB7MSwgMiwgMywgNCwgNX07CiAgICBpbnQgbiA9IDU7ICAvLyBOdW1iZXIgb2YgZGF0YSBwb2ludHMKCiAgICAvLyBWYXJpYWJsZXMgdG8gY2FsY3VsYXRlIHN1bXMKICAgIGludCBzdW1feCA9IDAsIHN1bV95ID0gMCwgc3VtX3h5ID0gMCwgc3VtX3gyID0gMDsKCiAgICAvLyBDYWxjdWxhdGUgc3VtcwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBzdW1feCArPSB4W2ldOwogICAgICAgIHN1bV95ICs9IHlbaV07CiAgICAgICAgc3VtX3h5ICs9IHhbaV0gKiB5W2ldOwogICAgICAgIHN1bV94MiArPSB4W2ldICogeFtpXTsKICAgIH0KCiAgICAvLyBDYWxjdWxhdGUgc2xvcGUgKGIpIGFuZCBpbnRlcmNlcHQgKGEpCiAgICBmbG9hdCBiID0gKG4gKiBzdW1feHkgLSBzdW1feCAqIHN1bV95KSAvIChuICogc3VtX3gyIC0gc3VtX3ggKiBzdW1feCk7CiAgICBmbG9hdCBhID0gKHN1bV95IC0gYiAqIHN1bV94KSAvIG47CgogICAgLy8gT3V0cHV0IHRoZSByZXN1bHRzCiAgICBwcmludGYoIkxlYXN0IHNxdWFyZSBsaW5lOiB5ID0gJS4yZiArICUuMmZ4XG4iLCBhLCBiKTsKCiAgICByZXR1cm4gMDsKfQoK